2014-09-25 71 views
-1

我想将使用Obj-C编写的自定义MKAnnotationView转换为Swift,并且当我想要initWithAnnotation时出现错误。尝试初始化注释时出现的问题查看

下面我将同时提供了ObjC和雨燕代码:

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

有没有办法让我自己在我的情况下或者我应该只是创建一个类型MKAnnotationView的变量分配给它的初始化的结果,并更改函数的返回类型或是否有另一种方法,我可以做到这一点?

回答

0

你这样做是错误的init方法,你应该调用父类的初始化是这样的,

func initWithAnnotation(annotation : MKAnnotation) { 

    super.init(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) 
} 
+0

对不起,愚蠢的问题,我要问你,但是这如何初始化自我对象和一些原因init函数没有出现,我真的不知道为什么 – tudoricc 2014-09-25 08:31:18

+0

任何想法,为什么我没有得到这种形式的初始化? – tudoricc 2014-09-25 11:53:02