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