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

Kev*_*ado 8 static protocols ios swift

如何访问static实例中的协议方法

我有一个列表Contact,联系人可以是一个FamilyContact继承ContactGroupStatus 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
    }

}
Run Code Online (Sandbox Code Playgroud)

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

我怎么称呼它?如果我删除static关键字,它会起作用,但我认为将其定义为静态更合乎逻辑.

如果我更换

let isPrivate = contact.dynamicType.isPrivate()
Run Code Online (Sandbox Code Playgroud)

通过

let isPrivate = FamilyContact.isPrivate()
Run Code Online (Sandbox Code Playgroud)

它工作,但我可以有超过1个子类

如果我删除了static键盘,我可以这样做:

if let c = contact as? GroupStatus {
    if !c.isPrivate() {
        nonPrivateContact.append(contact)
    }
}
Run Code Online (Sandbox Code Playgroud)

但我想保留static关键字

Mar*_*n R 10

这看起来像一个错误或不受支持的功能.我希望以下工作:

if let gsType = contact.dynamicType as? GroupStatus.Type {
    if gsType.isPrivate() {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,它不编译:

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

确实编译FamilyContact.Type而不是GroupStatus.Type.这里报告了一个类似的问题:

制作isPrivate()实例方法而不是类方法是我目前唯一能想到的解决方法,也许有人会提供更好的解决方案......

Swift 2/Xcode 7的更新:正如@Tankista在下面提到的那样,这已得到修复.上面的代码在Xcode 7 beta 3中编译并按预期工作.