2015-04-12 52 views
1

這個Objective-C協議用於在Swift 1.1中工作,但現在在Swift 1.2中出現錯誤。使用getter的Objective-C協議的Swift 1.2錯誤

Objective-C的協議剝離下來到一個問題的性質:

@protocol ISomePlugin <NSObject> 
@property (readonly, getter=getObjects) NSArray * objects; 
@end 


class SomePlugin: NSObject, ISomePlugin { 
    var objects: [AnyObject]! = nil 
    func getObjects() -> [AnyObject]! { 
     objects = ["Hello", "World"]; 
     return objects; 
    } 
} 

以下是雨燕1.2的錯誤:

Plugin.swift:4:1: error: type 'SomePlugin' does not conform to protocol 'ISomePlugin' 
class SomePlugin: NSObject, ISomePlugin { 
^ 
__ObjC.ISomePlugin:2:13: note: protocol requires property 'objects' with type '[AnyObject]!' 
    @objc var objects: [AnyObject]! { get } 
      ^
Plugin.swift:6:9: note: Objective-C method 'objects' provided by getter for 'objects' does not match the requirement's selector ('getObjects') 
    var objects: [AnyObject]! = nil 
     ^

如果我改變協議(這我真的不能,因爲它做的來自第三方)到以下代碼編譯罰款。

// Plugin workaround 
@protocol ISomePlugin <NSObject> 
@property (readonly) NSArray * objects; 
- (NSArray *) getObjects; 
@end 

在此先感謝。

回答

3

您可以實現

@property (readonly, getter=getObjects) NSArray * objects; 

與一個@objc屬性 指定Objective-C的名稱只讀屬性計算。 如果您需要儲存的財產作爲支持商店 那麼您必須單獨定義。例如:

class SomePlugin: NSObject, ISomePlugin { 

    private var _objects: [AnyObject]! = nil 

    var objects: [AnyObject]! { 
     @objc(getObjects) get { 
      _objects = ["Hello", "World"]; 
      return _objects; 
     } 
    } 
}