2015-06-07 70 views
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' 
+2

糟糕的主意,有一個強大的'id 委託'。 – Mundi

+0

@Mundi不確定你的意思......爲什麼它是一個壞主意,會改變它解決我的問題? – Carlo

+0

它很複雜。簡而言之,它可能導致循環強引用,從而導致內存泄漏。使用'assign'。 - 你也沒有提到什麼是行不通的。請編輯您的問題,並更明確地說明您的期望和結果。 – Mundi

回答

0

晴這個錯誤將消失當你實現採用所有必需的協議方法。只需在您的WordPressParser類中編寫所有必需的方法並嘗試構建應用程序。它應該是成功的。

+0

是的,我試過了,但它不起作用。問題是我不知道如何準確地實現這兩個協議屬性:(委託和blogSocialNetworkUserNames)。我將粘貼整個代碼,以便您可以看到它。 – Carlo