2015-02-10 45 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方法。