Swift 3 无法将符合协议的对象数组附加到该协议的集合 [英] Swift 3 unable to append array of objects, which conform to a protocol, to a collection of that protocol

查看:19
本文介绍了Swift 3 无法将符合协议的对象数组附加到该协议的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我粘贴了代码,您应该可以将这些代码粘贴到 Swift 3 playground 并查看错误.

Below I have pasted code which you should be able to paste into a Swift 3 playground and see the error.

我定义了一个协议并创建了一个该类型的空数组.然后我有一个符合协议的类,我尝试将其附加到数组,但出现以下错误.

I have a protocol defined and create an empty array of that type. I then have a class which conforms to the protocol which I try to append to the array but I get the below error.

protocol MyProtocol {
    var text: String { get }
}

class MyClass: MyProtocol {
    var text = "Hello"
}
var collection = [MyProtocol]()
var myClassCollection = [MyClass(), MyClass()]
collection.append(myClassCollection)

argument type '[MyClass]' does not conform to expected type 'MyProtocol'

注意 collection += myClassCollection 返回以下错误:

Note that collection += myClassCollection returns the following error:

error: cannot convert value of type '[MyProtocol]' to expected argument type 'inout _'

这适用于早期版本的 Swift.

This was working in earlier versions of Swift.

到目前为止我发现的唯一解决方案是迭代并将每个元素添加到新数组中,如下所示:

The only solution I have found so far is to iterate and add each element to the new array like so:

for item in myClassCollection {
    collection.append(item)
}

感谢任何帮助,谢谢!

编辑

解决方法如下:

collection.append(contentsOf: myClassCollection as [MyProtocol])

当您缺少as [MyProtocol]"时,真正的问题是一个误导性的编译器错误

The real issue is a misleading compiler error when you are missing "as [MyProtocol]"

编译器错误如下:

error: extraneous argument label 'contentsOf:' in call
collection.append(contentsOf: myClassCollection)

此错误导致用户从代码中删除 contentsOf:,然后导致我第一次提到的错误.

This error causes users to remove contentsOf: from the code which then causes the error I first mentioned.

推荐答案

append(_ newElement: Element) 追加单个元素.你想要的是 append(contentsOf newElements: C).

append(_ newElement: Element) appends a single element. What you want is append(contentsOf newElements: C).

但是你有将转换 [MyClass] 数组显式地转换为[MyProtocol]:

But you have to convert the [MyClass] array to [MyProtocol] explicitly:

collection.append(contentsOf: myClassCollection as [MyProtocol])
// or:
collection += myClassCollection as [MyProtocol]

在Swift中使用协议时的类型转换中所述,这个将每个数组元素包装到一个盒子中,其中包含符合 MyProtocol 的东西",这不仅仅是一种重新解释的阵列.

As explained in Type conversion when using protocol in Swift, this wraps each array element into a box which holds "something that conforms to MyProtocol", it is not just a reinterpretation of the array.

编译器会自动为单个值执行此操作(这就是为什么

The compiler does this automatically for a single value (that is why

for item in myClassCollection {
    collection.append(item)
}

编译)但不适用于数组.在早期的 Swift 版本中,您甚至不能用 as [MyProtocol] 来转换整个数组,你必须强制转换每个单独的元素.

compiles) but not for an array. In earlier Swift versions, you could not even cast an entire array with as [MyProtocol], you had to cast each individual element.

这篇关于Swift 3 无法将符合协议的对象数组附加到该协议的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆