提供的静态方法如何在实例中访问static
协议方法如何调用由协议斯威夫特
我有Contact
列表,联系人可以是FamilyContact
从Contact
继承和GroupStatus protocol
我想从GroupStatus
却徒劳无功调用静态方法...
这里是我的代码
protocol GroupStatus {
static func isPrivate() -> Bool // static method that indicates the status
}
protocol IsBusy {
func wizzIt()
}
class AdresseBook {
private var contacts = [Contact]()
func addOne(c: Contact) {
contacts.append(c)
}
func listNonPrivated() -> [Contact]? {
var nonPrivateContact = [Contact]()
for contact in contacts {
// here is I should call the static method provided by the protocol
if self is GroupStatus {
let isPrivate = contact.dynamicType.isPrivate()
if !isPrivate {
nonPrivateContact.append(contact)
}
}
nonPrivateContact.append(contact)
}
return nonPrivateContact
}
}
class Contact : Printable {
var name: String
init(name: String) {
self.name = name
}
func wizz() -> Bool {
if let obj = self as? IsBusy {
obj.wizzIt()
return true
}
return false
}
var description: String {
return self.name
}
}
class FamilyContact: Contact, GroupStatus {
static func isPrivate() -> Bool {
return true
}
}
我无法编译Contact.Type does not have a member named 'isPrivate'
我该怎么称呼它?它可以工作,如果我删除static
关键字,但我认为更合理的定义它的静态。
如果我更换
let isPrivate = contact.dynamicType.isPrivate()
通过
let isPrivate = FamilyContact.isPrivate()
它的工作原理,但我可以有超过1子
如果我删除static
keywork我可以这样做:
if let c = contact as? GroupStatus {
if !c.isPrivate() {
nonPrivateContact.append(contact)
}
}
但我想保持static
关键字
您的实际问题是什么?你期望的结果是什么?你现在有什么结果? – ABakerSmith
我添加了错误。我无法编译。 – thedjnivek
我想打电话联系。isPrivate()'无论联系人的子类是什么 – thedjnivek