在Swift中转换为泛型可选 [英] Casting to generic optional in Swift
问题描述
我在Swift中寻找泛型,并且遇到了一些我无法弄懂的东西:如果我将一个值转换为泛型参数的类型,则不会执行转换。
class SomeClass< T>如果我尝试使用静态类型, {
init?(){
如果让_ = 4 as? T {
println(should work)
} else {
return nil
}
}
}
if让_ = SomeClass< Int?>(){
println(not called)
}
如果让_ = 4 as?诠释? {
println(works)
}
任何人都可以解释这种行为?不应该是两种情况都是相同的吗?
更新
上面的例子被简化为最大值。下面的例子说明需要一个更好的转换
class SomeClass< T> {
@ b
init?(v:[String:AnyObject]){
如果让_ = v [k]为? T' {
print(should work)
} else {
print(does not)
return nil
}
}
}
if _ = SomeClass< Int?>(v:[k:4]){
print(not called)
} $ b $如果让_ = SomeClass< Int>(v:[k:4]){b $ b print(called)
}
$ b第二次更新
在@matt让我了解了
AnyObject 和
任何
和@Darko在他的评论中指出字典如何让我的例子太复杂,这是我的下一个改进class SomeClass< T> {
private var value:T!
init?< U>(param:U){
如果让casted = param as? T {
值=已铸造
}其他{
返回零
}
}
}
if让_ = SomeClass< Int?>(param:Int(4)){
println(not called)
}
如果让_ = SomeClass< Int> (param:Int(4)){
println(called)
}
如果让_ = Int(4)as?诠释? {
println(works)
}
如果让_ =(Int(4)as Any)as?诠释? {
println(不能从Any被降级为更多可选类型'Int?')
}
我之前尝试过使用
init?(param:Any)
,但是产生了上一个中所示的相同问题if if
这是在其他地方讨论 。
因此,最重要的是:有没有人真的将任何东西投射到一个通用的可选类型?任何状况之下?我很高兴接受任何有效的例子。
/ p>I'm fiddling around with generics in Swift and hit something I can't figure out: If I cast a value into the type of a generic parameter, the cast is not performed. If I try the same with static types, it works.
class SomeClass<T> { init?() { if let _ = 4 as? T { println("should work") } else { return nil } } } if let _ = SomeClass<Int?>() { println("not called") } if let _ = 4 as? Int? { println("works") }
Can anybody explain this behavior? Shouldn't be both cases equivalent?
Update
The above example is simplified to the max. The following example illustrates the need for a cast a little better
class SomeClass<T> { init?(v: [String: AnyObject]) { if let _ = v["k"] as? T? { print("should work") } else { print("does not") return nil } } } if let _ = SomeClass<Int?>(v: ["k": 4]) { print("not called") } if let _ = SomeClass<Int>(v: ["k": 4]) { print("called") }
2nd Update
After @matt made me learn about
AnyObject
andAny
and @Darko pointed out in his comments how dictionaries make my example too complicated, here's my next refinementclass SomeClass<T> { private var value: T! init?<U>(param: U) { if let casted = param as? T { value = casted } else { return nil } } } if let _ = SomeClass<Int?>(param: Int(4)) { println("not called") } if let _ = SomeClass<Int>(param: Int(4)) { println("called") } if let _ = Int(4) as? Int? { println("works") } if let _ = (Int(4) as Any) as? Int? { println("Cannot downcast from Any to a more optional type 'Int?'") }
I tried using
init?(param: Any)
before, but that yields the same problem illustrated in the lastif
which is discussed elsewhere.So all it comes down to: Has anyone really been far as to ever cast anything to a generic optional type? In any case? I'm happy to accept any working example.
解决方案This works as expected now - in Playgrounds as well as in projects.
这篇关于在Swift中转换为泛型可选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!