Ios 快速泛型数组
如何创建泛型数组?例如:Ios 快速泛型数组,ios,swift,generics,Ios,Swift,Generics,如何创建泛型数组?例如: struct Thing<Any> { } let intThing = Thing<Int>() let stringThing = Thing<String>() // This line doesn't compile // Cannot convert value of type 'Thing<Int>' to expected type 'Thing' let things: [Thing] = [intTh
struct Thing<Any> {
}
let intThing = Thing<Int>()
let stringThing = Thing<String>()
// This line doesn't compile
// Cannot convert value of type 'Thing<Int>' to expected type 'Thing'
let things: [Thing] = [intThing, stringThing]
struct东西{
}
让intThing=Thing()
让stringThing=Thing()
//这行代码无法编译
//无法将“Thing”类型的值转换为预期的“Thing”类型
让事物:[事物]=[intThing,stringThing]
如何声明任何类型的泛型(如Thing
或Thing
)?您可以这样做:let things:[Any]=[intThing,stringThing]
Thing
本身不是有效的类型Thing
是一种类型,Thing
是另一种类型,不能在一个数组中混合使用不同的类型
您是否注意到,即使让things:[Thing]
也无法编译
我认为您可能正在尝试的是使用带有关联值的枚举的服务器
struct MyStruct{
出租物业:T
init(属性:T){
self.property=属性
}
}
枚举事物{
案例int(int)
大小写字符串(字符串)
案例intStruct(MyStruct)
}
//创作
设myInt=Thing.int(2)
让myString=Thing.string(“string”)
让myIntStruct=Thing.intStruct(MyStruct(属性:3))
//缺点是您需要一个“case”子句来检索数据
开关myIntStruct{
case let.int(值):
打印(“具有int:\(值)”)
case let.string(值):
打印(“具有字符串:\(值)”)
case let.intStruct(值):
打印(“具有intStruct\(值)”)
}
//一系列事物
让事物:[事物]=[myInt,myString,myIntStruct]
在这篇关于将Plist数据包装到单个结构中的文章中,EntityType使用了枚举
可以将任何内容与枚举值关联,但它必须是完全限定类型。因此,您不能使用MyStruct,因为它不是完全限定的,您需要使用
MyStruct
来完全限定。这意味着您需要为要与MyStruct一起使用的每个泛型类型创建一个案例
这是您的通用方法所能做到的。我不知道你想做什么。但是如果你想实现某种多态性,这种多态性依赖于在
T
上操作的方法,但不使用T
作为输入或输出,那么你应该让你的东西实现一个协议,并为该协议创建一个数组,然后在该协议上调用你的多态方法。你可以使用枚举
enum Thing {
case Text(String)
case Integer(Int)
}
现在您可以创建一个包含字符串或Int的对象
let thingWithText = Thing.Text("Hello world")
let thingWithInt = Thing.Integer(123)
您可以将它们放入事物的数组中
(不涉及泛型)
最后,您可以通过这种方式处理数组中的值
things.forEach { (thing) -> () in
switch thing {
case .Text(let text): print(text)
case .Integer(let integer): print(integer)
}
}
这会有帮助的
protocol Six {}
struct Thing<Any> :Six {}
let intThing = Thing<Int>()
let stringThing = Thing<String>()
let things: [Six] = [intThing, stringThing]
protocol Six{}
结构物:六{}
让intThing=Thing()
让stringThing=Thing()
让事物:[六]=[intThing,stringThing]
您可以使用一个协议,该协议包含您想要使用的所有相关属性和功能:
protocol ThingProtocol {
func doSomething()
// if you want to expose generic functions/properties
// use functions/properties which have the most specific type
// for Thing<T: AProtocol> it should be `var anyValue: AProtocol { get }`
// here: `Any` since T in Thing<T> can be anything
var anyValue: Any { get }
}
struct Thing<T>: ThingProtocol {
var value: T
// ThingProtocol implementation
var anyValue: Any { return value as Any }
func doSomething() { ... }
}
let intThing = Thing<Int>(value: 42)
let stringThing = Thing<String>(value: "101010")
let things: [ThingProtocol] = [intThing, stringThing]
things[0].doSomething()
things[0].anyValue // returns a value of type Any
// you cannot call `value` since it is not specified in the protocol. If you do it, it would result in an error in `Thing<T>`
things[0].value // error
协议ThingProtocol{
func doSomething()
//如果要公开泛型函数/属性
//使用具有最特定类型的函数/属性
//例如,它应该是'var anyValue:AProtocol{get}`
//这里:`Any`因为事物中的T可以是任何东西
var anyValue:Any{get}
}
结构物:ThingProtocol{
var值:T
//ThingProtocol实现
var anyValue:Any{返回值为Any}
func doSomething(){…}
}
让intThing=Thing(值:42)
让stringThing=Thing(值:“101010”)
let things:[ThingProtocol]=[intThing,stringThing]
事物[0]。doSomething()
things[0]。anyValue//返回类型为Any的值
//无法调用'value',因为协议中未指定它。如果你这样做,它会导致一个错误的东西`
事物[0]。值//错误
谢谢,这样的枚举是什么样子的?我的东西
结构实际上有一些属性,据我所知,这在枚举中是不可行的。@SteveKuo,我已经更新了代码,这样你就可以看到它如何与泛型一起工作,但是你需要为每种t
@Vincembernier,对于具有类型约束的对象,您将如何使其工作?假设我限制了我的泛型,使它们必须符合协议,现在我不能使用任何协议,因为它不符合协议。不幸的是,泛型还不能与这种模式一起工作。
protocol Six {}
struct Thing<Any> :Six {}
let intThing = Thing<Int>()
let stringThing = Thing<String>()
let things: [Six] = [intThing, stringThing]
protocol ThingProtocol {
func doSomething()
// if you want to expose generic functions/properties
// use functions/properties which have the most specific type
// for Thing<T: AProtocol> it should be `var anyValue: AProtocol { get }`
// here: `Any` since T in Thing<T> can be anything
var anyValue: Any { get }
}
struct Thing<T>: ThingProtocol {
var value: T
// ThingProtocol implementation
var anyValue: Any { return value as Any }
func doSomething() { ... }
}
let intThing = Thing<Int>(value: 42)
let stringThing = Thing<String>(value: "101010")
let things: [ThingProtocol] = [intThing, stringThing]
things[0].doSomething()
things[0].anyValue // returns a value of type Any
// you cannot call `value` since it is not specified in the protocol. If you do it, it would result in an error in `Thing<T>`
things[0].value // error