gpt4 book ai didi

swift - 声明符合协议(protocol)的类数组

转载 作者:IT王子 更新时间:2023-10-29 05:50:13 25 4
gpt4 key购买 nike

假设我已经创建了这个协议(protocol)和几个类

import UIKit

protocol ControllerConstructorProtocol {
class func construct() -> UIViewController?
}

class MyConstructor: ControllerConstructorProtocol {
class func construct() -> UIViewController? {
return UIViewController()
}
}

class MyOtherConstructor: ControllerConstructorProtocol {
class func construct() -> UIViewController? {
return UITableViewController(style: .Grouped)
}
}

现在我想声明一个数组,其中包含符合此类协议(protocol)的对象类。我该如何申报?理想情况下,我希望编译器检查数组是否正确填充(在编译时),而不是在(运行时)使用 as 运算符自己检查它。

这是我尝试过但没有成功的:(

  1. 这会导致编译错误:

    'Any Object does not have a member named 'construct'

    var array = [
    MyConstructor.self,
    MyOtherConstructor.self,
    ]

    var controller = array[0].construct() // << ERROR here
  2. 写这个更糟,因为类本身不符合到协议(protocol)(他们的实例做)

    Type 'MyConstructor.Type' does not conform to protocol 'ControllerConstructorProtocol'

    var array: Array<ControllerConstructorProtocol> = [
    MyConstructor.self, // << ERROR here
    MyOtherConstructor.self,
    ]

编辑 2016/04/23:在 Swift 2.2 (Xcode 7.3) 中可以编写 @rintaro's original idea :)

let array: Array<ControllerConstructorProtocol.Type> = [
MyConstructor.self,
MyOtherConstructor.self,
]
let viewController = array[0].construct()

最佳答案

“符合协议(protocol)的类数组”可以声明为 Array<TheProtocol.Type> .

您可以:

var array: Array<ControllerConstructorProtocol.Type> = [
MyConstructor.self,
MyOtherConstructor.self,
]

但是...,

    array[0].construct()
// ^ error: accessing members of protocol type value 'ControllerConstructorProtocol.Type' is unimplemented

项目上的调用方法“未实现”。

截至目前,您必须将协议(protocol)声明为 @objc , 并通过 AnyClass 调用该方法.此外,由于某些原因,我们不能直接转换 array[0]AnyClass ,相反,我们必须将其转换为 Any , 然后 AnyClass .

@objc protocol ControllerConstructorProtocol {
class func construct() -> UIViewController?
}

var array: Array<ControllerConstructorProtocol.Type> = [
MyConstructor.self,
MyOtherConstructor.self,
]

let vc = (array[0] as Any as AnyClass).construct()

注意:转换问题已在 Swift 1.2/Xcode 6.3 中修复。但是“未实现”是“未实现”:(


只是随意的想法:

这取决于您的实际用例,但在这种特殊情况下,()-> UIViewController? 的数组闭包就足够了:

var array: [() -> UIViewController?] = [
MyConstructor.construct,
MyOtherConstructor.construct,
]

let vc = array[0]()

如果您有多种方法,您可能希望使用协议(protocol)的类型删除包装器。

protocol ControllerConstructorProtocol {
class func construct() -> UIViewController?
class func whoami() -> String
}

struct ControllerConstructorWrapper {
private let _construct: () -> UIViewController?
private let _whoami: () -> String
init<T: ControllerConstructorProtocol>(_ t:T.Type) {
_construct = { t.construct() }
_whoami = { t.whoami() }
}
func construct() -> UIViewController? { return _construct() }
func whoami() -> String { return _whoami() }
}

var array: [ControllerConstructorWrapper] = [
ControllerConstructorWrapper(MyConstructor),
ControllerConstructorWrapper(MyOtherConstructor),
]

let who = array[0].whoami()
let vc = array[0].construct()

关于swift - 声明符合协议(protocol)的类数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28690399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com