Swift 如何调用协议提供的静态方法
How to call static method provided by protocol in Swift
如何在实例中访问 static
协议方法
我有一个 Contact
的列表,联系人可以是继承自 Contact
和 GroupStatus protocol
的 FamilyContact
我想从 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
键,我可以通过这种方式完成:
if let c = contact as? GroupStatus {
if !c.isPrivate() {
nonPrivateContact.append(contact)
}
}
但我想保留 static
关键字
这看起来像是错误或不受支持的功能。我希望
以下作品:
if let gsType = contact.dynamicType as? GroupStatus.Type {
if gsType.isPrivate() {
// ...
}
}
然而,它没有编译:
error: accessing members of protocol type value 'GroupStatus.Type' is unimplemented
使用 FamilyContact.Type
而不是 GroupStatus.Type
进行编译。这里报告了类似的问题:
使 isPrivate()
成为实例方法而不是 class 方法是
我目前能想到的唯一解决方法,也许有人会来
有更好的解决方案...
Swift 2 / Xcode 7 的更新: 正如@Tankista 在下面指出的那样,这有
已修复。上面的代码在 Xcode 7 beta 3.
中按预期编译和工作
type(of: contact).isPrivate()
这应该在最近 Swift.
有效
如何在实例中访问 static
协议方法
我有一个 Contact
的列表,联系人可以是继承自 Contact
和 GroupStatus protocol
FamilyContact
我想从 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
键,我可以通过这种方式完成:
if let c = contact as? GroupStatus {
if !c.isPrivate() {
nonPrivateContact.append(contact)
}
}
但我想保留 static
关键字
这看起来像是错误或不受支持的功能。我希望 以下作品:
if let gsType = contact.dynamicType as? GroupStatus.Type {
if gsType.isPrivate() {
// ...
}
}
然而,它没有编译:
error: accessing members of protocol type value 'GroupStatus.Type' is unimplemented
使用 FamilyContact.Type
而不是 GroupStatus.Type
进行编译。这里报告了类似的问题:
使 isPrivate()
成为实例方法而不是 class 方法是
我目前能想到的唯一解决方法,也许有人会来
有更好的解决方案...
Swift 2 / Xcode 7 的更新: 正如@Tankista 在下面指出的那样,这有 已修复。上面的代码在 Xcode 7 beta 3.
中按预期编译和工作type(of: contact).isPrivate()
这应该在最近 Swift.
有效