1

我有一些問題,當使用MKAnnotation, 我想在一個MapView 所以我創建了一個名爲AdoptingAnAnnotation類添加註釋, 的.h文件遵循 #進口 #進口如何使用MKAnnotation

@interface AdoptingAnAnnotation: NSObject { 
} 

@synthesize latitude; 
@synthesize longitude; 

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 
- (NSString *) title; 
- (NSString *) subtitle; 
@end 

和.m文件就是按照

#import "AdoptingAnAnnotation.h" 

@implementation AdoptingAnAnnotation 

@synthesize latitude; 
@synthesize longitude; 

- (id) initWithLatitude:(CLLocationDegrees) lat longitude:(CLLocationDegrees) lng { 
    latitude = lat; 
    longitude = lng; 
    return self; 
    } 
- (CLLocationCoordinate2D) coordinate { 
    CLLocationCoordinate2D coord = {self.latitude, self.longitude}; 
    return coord; 
    } 
- (NSString *) title { 
    return @"217 2nd St"; 
    } 
- (NSString *) subtitle { 
    return @"San Francisco CA 94105"; 
    } 
@end 

收到錯誤消息像illegal interface qualifier 是我的語法錯誤還是其他有關MKAnnotation的錯誤?

回答

2

您的@synthesize語句屬於類實現,而不是在接口中。這就是爲什麼你得到「非法接口限定符」錯誤。最後,你的班級應該採用MKAnnotation協議。

+0

但是當我從.h文件中刪除@synthesize時,我收到了其他錯誤消息,如http://commondatastorage.googleapis.com/haibo/temp/Scr​​een%20Shot%202012-02-25%20at%202.08。 54%20 PM.png這意味着我應該聲明一些東西 – timger 2012-02-25 06:11:43

+0

如果你要@synthesize'lattitude'和'longitude',你需要對@interface中的屬性聲明@property聲明。同樣,既然你已經有了一個@comperty的'coordinate'聲明,你也應該爲這個屬性指定一個@synthesize指令,或者你應該自己提供這個訪問器。 – Caleb 2012-02-25 06:16:56

0

您發佈.h兩次......請編輯。你的.h中也有合成器,他們屬於.m。並確保你實現了這些MKAnnotation方法。

+0

感謝您的提醒 – timger 2012-02-25 06:15:02