简体   繁体   English

如何在Swift中调用协议提供的静态方法

[英]How to call static method provided by protocol in Swift

How to access to static protocol method within a instance如何访问实例内的static协议方法

I have a list of Contact , the contact can be a FamilyContact that inherit from Contact and the GroupStatus protocol我有一个列表Contact ,联系人可以是FamilyContact从继承ContactGroupStatus protocol

I want to call the static method from GroupStatus but in vain...我想从GroupStatus调用静态方法但徒劳无功...

Here is my code这是我的代码

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
    }

}

I can't compile Contact.Type does not have a member named 'isPrivate'我无法编译Contact.Type does not have a member named 'isPrivate'

How can I call it ?我怎么称呼它? It works if I delete the static keyword, but I think is more logical to define it static.如果我删除static关键字,它会起作用,但我认为将其定义为 static 更合乎逻辑。

If I replace如果我更换

let isPrivate = contact.dynamicType.isPrivate()

by经过

let isPrivate = FamilyContact.isPrivate()

It works, but I can have more than 1 subclasses它有效,但我可以有 1 个以上的子类

If I remove the static keywork I can do it by this way :如果我删除static键工作,我可以通过这种方式做到这一点:

if let c = contact as? GroupStatus {
    if !c.isPrivate() {
        nonPrivateContact.append(contact)
    }
}

But I want to keep the static keyword但我想保留static关键字

This looks like a bug or a non-supported feature.这看起来像是一个错误或不受支持的功能。 I would expect that the following works:我希望以下工作:

if let gsType = contact.dynamicType as? GroupStatus.Type {
    if gsType.isPrivate() {
        // ...
    }
}

However, it does not compile:但是,它不会编译:

error: accessing members of protocol type value 'GroupStatus.Type' is unimplemented

It does compile with FamilyContact.Type instead of GroupStatus.Type .确实使用FamilyContact.Type而不是GroupStatus.Type编译。 A similar problem is reported here:这里报告了一个类似的问题:

Making isPrivate() an instance method instead of a class method is the only workaround that I currently can think of, maybe someone comes with a better solution ...使isPrivate()成为实例方法而不是类方法是我目前能想到的唯一解决方法,也许有人提出了更好的解决方案......

Update for Swift 2 / Xcode 7: As @Tankista noted below, this has been fixed. Swift 2 / Xcode 7 的更新:正如@Tankista 在下面提到的,这已经得到修复。 The above code compiles and works as expected in Xcode 7 beta 3.上面的代码在 Xcode 7 beta 3 中按预期编译和工作。

type(of: contact).isPrivate()

这应该适用于最近的 Swift。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM