1
我在Item類中有一個類型爲ProtocolItem1協議的變量。我想要做的就是能夠使用這個類,並在這個變量中保存符合ProtocolItem1協議的對象,這可能嗎?我知道在Java中你可以創建一個接口,並以這種方式實現它。在目標C我見過ID,請讓我知道。符合協議的變量接受符合協議的變量
代碼:
class Item : NSObject {
var obj: ProtocolItem1?
//Other functions and variables
}
class Item2: ProtocolItem1 {
//variables and methods
}
class OtherClass: UIViewController {
var dataSource: Item
//other functions
override func viewDidLoad(){
var object1: Item2 = Item2()
dataSource.obj = object1
}
}
非常感謝您的幫助! – paul590