如何在Swift 4.1中迭代类型数组?
我在ios11.4.1上使用Xcode 9.4.1和swift4.1 我有一些协议,比如:如何在Swift 4.1中迭代类型数组?,swift,conditional,protocols,Swift,Conditional,Protocols,我在ios11.4.1上使用Xcode 9.4.1和swift4.1 我有一些协议,比如: protocol Bla { // some stuff } protocol Gorp { // some other stuff } typealias MyType = Bla & Gorp struct Hello: MyType { var ID: Int var name: String } struct Goodbye: MyType {
protocol Bla {
// some stuff
}
protocol Gorp {
// some other stuff
}
typealias MyType = Bla & Gorp
struct Hello: MyType {
var ID: Int
var name: String
}
struct Goodbye: MyType {
var percentage: Double
var hairbrush: String
}
func doSomething<T: MyType>(_ theType: T.Type) {
// some stuff
print("Got called... \(theType)")
}
doSomething(Hello.self)
doSomething(Goodbye.self)
我有一些结构符合这两个协议,比如:
protocol Bla {
// some stuff
}
protocol Gorp {
// some other stuff
}
typealias MyType = Bla & Gorp
struct Hello: MyType {
var ID: Int
var name: String
}
struct Goodbye: MyType {
var percentage: Double
var hairbrush: String
}
func doSomething<T: MyType>(_ theType: T.Type) {
// some stuff
print("Got called... \(theType)")
}
doSomething(Hello.self)
doSomething(Goodbye.self)
然后我得到了一个func,它接受一个参数,类型,它同时符合Bla和Gorp。在本例中,我只是打印一个描述——如下所示:
protocol Bla {
// some stuff
}
protocol Gorp {
// some other stuff
}
typealias MyType = Bla & Gorp
struct Hello: MyType {
var ID: Int
var name: String
}
struct Goodbye: MyType {
var percentage: Double
var hairbrush: String
}
func doSomething<T: MyType>(_ theType: T.Type) {
// some stuff
print("Got called... \(theType)")
}
doSomething(Hello.self)
doSomething(Goodbye.self)
这非常有效,我得到了以下输出,正如预期的那样:
Got called... Hello
Got called... Goodbye
然而,我真正想做的是迭代一组,而不是单独调用它们
这种方式会给我一个错误“注意:应该是类型为“(T.type)”的参数列表”:
如果我添加一个作为![MyType.Type]或作为![MyType],我得到了相同的错误。我也试过:
for thingy in [Hello.self as MyType.Type, Goodbye.self as MyType.Type] {
doSomething(thingy)
}
for thingy in [Hello.self, Goodbye.self] as! [(Bla & Gorp).Protocol] {
doSomething(thingy)
}
与其他错误相同
我也尝试过不使用类型别名
如果我开始输入,自动完成程序会说,doSomething需要类型为(Bla&Gorp).Protocol的参数。所以我也试过这个,
for thingy in [Hello.self as MyType.Type, Goodbye.self as MyType.Type] {
doSomething(thingy)
}
for thingy in [Hello.self, Goodbye.self] as! [(Bla & Gorp).Protocol] {
doSomething(thingy)
}
在这种情况下,我得到的信息是:
In argument type '(Bla & Gorp).Protocol', 'Bla & Gorp' does not conform to expected type 'Bla'
还尝试了此类操作,但出现了一个错误,“无法使用类型为“(MyType.type)”的参数列表调用'doSomething':
struct AnyBlaGorp{
让blaGorp:MyType.Type
init(类型:T.Type){
self.blaGorp=类型
}
}
感谢[AnyBlaGorp(Hello.self),AnyBlaGorp(再见.self)]{
doSomething(东西blaGorp)
}
如能提供正确的语法,我们将不胜感激。:) 您可以将
doSomething
方法设置为非泛型,并接受MyType.Type
。只有当协议没有Self
或相关类型时,才能执行此操作
func doSomething(_ theType: MyType.Type) {
// some stuff
print("Got called... \(theType)")
}
接下来,将数组强制转换为[MyType.Type]
:
for thingy in [Hello.self, Goodbye.self] as [MyType.Type] {
doSomething(thingy)
}
这利用了这样一个事实,即如果
A
继承了B
,那么A.self
可以转换为类型B.type
,如果A
继承了B协议中是否有任何相关的类型或self
呢?不,没有——我使用它们与给出的示例完全相同。对于[Hello.self,再见.self]中的内容{doSomething(thingy)}
无法工作,因为Swift是一种编译语言。在编译时,它需要知道它在那行代码上调用的是哪个doSomething
。比较一下超级棒!!不知何故,我从未想过要废除通用的。