0
Objective-C的協議,這個問題已經被問過,但從來沒有與我的具體問題:實現迅速
@property (readonly, copy, nonatomic) NSString *blogName;
@property (readonly, copy, nonatomic) NSString *blogEmail;
@property (strong, nonatomic) id<BlogParserDelegate> delegate; // This is my first problem
@property (strong, nonatomic) NSDictionary *blogSocialNetworkUserNames; // This is my second problem
@property (readonly, copy, nonatomic) NSString *blogLogoImageLink;
@property (readonly, copy, nonatomic) NSString *blogWebsite;
我不知道如何實現在斯威夫特兩個標性質,我已經試過幾件事情,這是我最後一次不成功的嘗試:
@NSCopying var blogSocialNetworkUserNames : NSDictionary!
var blogLogoImageLink : String!
我得到的錯誤是:
Type 'WordPressParser' does not conform to protocol 'BlogParserProtocol'
任何想法?
謝謝!
EDIT加入所有的代碼:
協議:
@protocol BlogParserProtocol <NSObject>
@required
#pragma mark Properties
@property (readonly, copy, nonatomic) NSString *blogName;
@property (readonly, copy, nonatomic) NSString *blogEmail;
@property (strong, nonatomic) id<BlogParserDelegate> delegate;
@property (strong, nonatomic) NSDictionary *blogSocialNetworkUserNames;
@property (readonly, copy, nonatomic) NSString *blogLogoImageLink;
@property (readonly, copy, nonatomic) NSString *blogWebsite;
#pragma mark init methods
-(instancetype)initWithBlogName:(NSString*)blogName;
#pragma mark SFARSSParserProtocol methods
-(BOOL)isReachable;
-(void)readRSSAtURL:(NSString *)URL;
-(void)getCommentsFromArticleId:(NSString*)articleId;
-(void)addComment:(NSString*)comment
toArticleId:(NSString*)articleId;
@end
類符合協議:
class WordPressParser: NSObject, BlogParserProtocol {
var blogName : String!
var blogEmail : String!
var delegate : AnyObject!
var blogSocialNetworkUserNames : NSDictionary!
var blogLogoImageLink : String!
var blogWebsite : String!
required init!(blogName: String!) {
}
func isReachable() -> Bool {
return true
}
func readRSSAtURL(URL: String!) {
}
func getCommentsFromArticleId(articleId: String!) {
}
func addComment(comment: String!, toArticleId articleId: String!) {
}
}
錯誤:
iPhone/ProjectName/WordPressParser.swift:11:1: Type 'WordPressParser' does not conform to protocol 'BlogParserProtocol'
糟糕的主意,有一個強大的'id委託'。 –
Mundi
@Mundi不確定你的意思......爲什麼它是一個壞主意,會改變它解決我的問題? – Carlo
它很複雜。簡而言之,它可能導致循環強引用,從而導致內存泄漏。使用'assign'。 - 你也沒有提到什麼是行不通的。請編輯您的問題,並更明確地說明您的期望和結果。 – Mundi