【发布时间】:2014-09-25 08:25:27
【问题描述】:
我想将用 Obj-C 编写的自定义 MKAnnotationView 转换为 Swift,但当我想 initWithAnnotation 时出现错误。
下面我将提供 ObjC 和 Swift 代码:
class JPThumbnailAnnotationView: MKAnnotationView,JPThumbnailAnnotationViewProtocol {
var coordinate :CLLocationCoordinate2D
var imageView : UIImageView
var titleLabel : UILabel
var subtitleLabel : UILabel
var disclosureBlock : JPThumbnail.SequencerNext
func initWithAnnotation(annotation : MKAnnotation) {
self = MKAnnotationView(annotation: annotation, reuseIdentifier: kJPSThumbnailAnnotationViewReuseID) //HERE IS WHERE I GET THE ERROR
self.canShowCallout = true
self.frame = CGRectMake(0, 0, kJPSThumbnailAnnotationViewStandardWidth, kJPSThumbnailAnnotationViewStandardHeight)
self.centerOffset = CGPointMake(0, -kJPSThumbnailAnnotationViewVerticalOffset)
}
Objective-C 版本是
- (id)initWithAnnotation:(id<MKAnnotation>)annotation {
self = [super initWithAnnotation:annotation reuseIdentifier:kJPSThumbnailAnnotationViewReuseID];
if (self) {
self.canShowCallout = NO;
self.frame = CGRectMake(0, 0, kJPSThumbnailAnnotationViewStandardWidth, kJPSThumbnailAnnotationViewStandardHeight);
self.backgroundColor = [UIColor clearColor];
self.centerOffset = CGPointMake(0, -kJPSThumbnailAnnotationViewVerticalOffset);
_state = JPSThumbnailAnnotationViewStateCollapsed;
[self setupView];
}
return self;
}
我得到的错误是:Can not assign self in a method
有没有办法我仍然可以在我的情况下分配 self 或者我应该只创建一个类型为 MKAnnotationView 的变量,将它分配给初始化程序的结果并更改函数的返回类型,或者是否有另一种方法可以做到这一点?
【问题讨论】:
标签: swift mkannotationview self