加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

通用数组的Swift错误

发布时间:2020-12-14 04:43:52 所属栏目:百科 来源:网络整理
导读:我有一个非常简单的游乐场: protocol MyProtocol {}struct MyType: MyProtocol {}class MyClass T: MyProtocol { func myFunction(array: [T]) { if let myArray = array as? [MyType] { println("Double!") } }}let instance = MyClassMyType()let array =
我有一个非常简单的游乐场:

protocol MyProtocol {}

struct MyType: MyProtocol {}

class MyClass <T: MyProtocol> {
    func myFunction(array: [T]) {
        if let myArray = array as? [MyType] {
            println("Double!")
        }
    }
}

let instance = MyClass<MyType>()

let array = [MyType(),MyType()]

instance.myFunction(array)

然后它在if let行上说“MyType不是’T’的子类型”.嗯,我认为,MyType和T兼容.

当我修改if let语句时,它实际上有效:

if let first = array.first as? MyType

但是现在我无法将数组转换为[MyType](当然,我知道它是Swift的静态类型规范.)

我想知道问题是什么.我对泛型的理解?或者是Swift语言的限制?如果是这样,有没有办法这样做?

提前致谢.

解决方法

Swift没有内置行为来推测性地将数组的内容从一种任意类型转换为另一种类型.它只会对它知道具有子类型/超类型关系的两种类型执行此操作:

class A { }
class B: A { }
let a: [A] = [B(),B()]
// this is allowed - B is a subtype of A
let b = a as? [B]

let a: [AnyObject] = [1,2,3]
// this is allowed - NSNumber is a subtype of AnyObject
let b = a as? [NSNumber]

struct S1 { }
struct S2 { }

let a = [S1(),S1()]
// no dice - S2 is not a subtype of S1
let b = a as? [S2]

该协议也无济于事:

protocol P { }
struct S1: P { }
struct S2: P { }

let a = [S1(),S1()]
// still no good – just because S1 and S2 both conform to P
// doesn’t mean S2 is a subtype of S1
let b = a as? [S2]

您的示例基本上是最后一个的变体.你有一个[T]类型的数组,并且你想把它转换为[MyType].重要的是要了解您没有[MyProtocol]类型的数组.您的泛型类型T是一种特定类型,它必须实现MyProtocol,但这不是一回事.

要了解为什么不能从任何类型转换为任何其他类型,请尝试以下代码:

protocol P { }
struct S: P { }

let a: [P] = [S(),S()]
let b = a as? [S]

这将生成运行时错误:“致命错误:不能在不同大小的类型之间进行unsafeBitCast”.这给出了一个提示,说明为什么只能将包含一个引用类型的数组转换为子类型 – 这是因为发生的事情只是从一个指针类型转换为另一个指针类型.这适用于超类/子类,但不适用于任意类,结构或协议,因为它们具有不同的二进制表示.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读