提供的靜態方法如何在實例中訪問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