2014-07-23 13 views
0

我有一個對象,該對象是定義在夫特的協議如下一類的實例分配代表一個問題:編譯錯誤用於夫特iOS的一個協議分配委派時

我簡化的代碼裸露的骨頭舉例說明問題: 這是與協議

protocol TheProtocol { 
    func notifyDelegate() 
} 
class ClassWithProtocol: NSObject { 
    var delegate: TheProtocol? 
    fire() { 
     delegate?.notifyDelegate() 
    } 
} 

這是類的類符合協議

class ClassConformingToProtocol: NSObject, TheProtocol { 
     var object: ClassWithProtocol? 
     func notifyDelegate() { 
      println("OK") 
     } 
     init() { 
      object = ClassWithProtocol() 
      object?.delegate = self // Compiler error - Cannot assign to the result of this expression 
      object?.fire() 
     } 
    } 

我已嘗試各種替代方法來分配委託沒有成功。任何想法我失蹤?

回答

2

發行說明中的​​已知問題部分說:

無法有條件地分配給任意對象的屬性。 (16922562)

例如,這是不支持:

let window: NSWindow? = NSApplication.sharedApplication.mainWindow 
window?.title = "Currently experiencing problems" 

所以,你應該這樣做if let realObject = object { ... }

+0

真棒....它的工作...... – eharo2