2015-02-10 50 views
4

我昨晚升級到了Swift 1.2,並且發現了一個我實際弄不懂的bug。下面的代碼在以前版本的Xcode和Swift中運行良好。MKAnnotation子類問題

//MARK: Annotation Object 
class PointAnnotation : NSObject, MKAnnotation { 
    var coordinate: CLLocationCoordinate2D 
    var title: String 
    var subtitle: String 
    var point: Point 
    var image: UIImage 
    var md: String 

    init(point: Point) { 
     self.coordinate = point.coordinate 
     self.title = point.title 
     self.subtitle = point.teaser 
     self.image = UIImage(named: "annotation.png")! 
     self.point = point 
     self.md = point.content 
    } 
} 

在第3行,我得到的有些難以理解的錯誤 Objective-C method 'setCoordinate:' provided by the setter for 'coordinate' conflicts with the optional requirement method 'setCoordinate' in protocol 'MKAnnotation'我試圖改變變量名稱等,但沒有幫助。有沒有人有任何想法如何解決這個問題?

該類用於我的mapview上的註釋。

回答

7

如果您不需要初始化之後改變座標,那麼你可以使用這種方式。這對我的作品與雨燕1.2:

class CustomAnnotation : NSObject, MKAnnotation { 
    let coordinate: CLLocationCoordinate2D 
    var title: String 

    init(coordinate: CLLocationCoordinate2D, title: String) { 
     self.coordinate = coordinate 
     self.title = title 
    } 
} 
0

您正在使用readwrite重寫MKAnnotation中的只讀ivar。

var coordinate: CLLocationCoordinate2D { get }

+0

好的。我該如何解決它?如果我更改變量,它會抱怨我不符合協議。 – 2015-02-10 10:00:23

+0

如何將座標定義爲'var coordinate:CLLocationCoordinate2D {get}'。對不起,我還沒有升級自己。 – Onato 2015-02-10 10:15:28

0

我有同樣的問題,所以我只是做了這一點:

private var _coordinate: CLLocationCoordinate2D 
var coordinate: CLLocationCoordinate2D { 
    return _coordinate 
} 

init(coordinate: CLLocationCoordinate2D) { 
    self._coordinate = coordinate 
    super.init() 
} 

func setCoordinate(newCoordinate: CLLocationCoordinate2D) { 
    self._coordinate = newCoordinate 
} 

這樣coordinate仍然是隻讀的,你可以使用setCoordinate方法。