[英]'Object' is not convertible to 'T' using Generics in Swift
I'm getting an error message when trying to use Generics in Swift. 尝试在Swift中使用Generics时收到错误消息。 I'm trying to create a function that takes any object that inherits from
Parent
, and modify an inout
reference which just so happens to be an Array
of T: Parent
我正在尝试创建一个函数,它接受从
Parent
继承的任何对象,并修改一个inout
引用,恰好是一个T: Parent
的Array
T: Parent
func myFunction<T: Parent>(data: NSDictionary, inout objects: Array<T>) {
let myArray = data.valueForKey("the_array") as NSArray
for object in myArray {
var addMe: Object = Object()
//...
objects.append(addMe) //'Object' is not convertible to 'T'
}
}
I'm passing this into the function higher up the stack. 我将它传递到堆栈上方的函数中。
var objects: [Object] = []
myClass.myFunction(data, objects: &objects)
Even though Object
is defined as follows, inheriting from Parent
即使
Object
定义如下,继承自Parent
class Object: Parent {
//...
}
It gives me an error that it can't convert Object
to T
.. although I'm probably mis-interpreting the error message. 它给了我一个错误,它无法将
Object
转换为T
..虽然我可能错误地解释了错误消息。 Does anyone see what I'm doing wrong? 有谁看到我做错了什么?
You're trying to add an Object
to an array of T : Parent
. 您正在尝试将
Object
添加到T : Parent
数组中。 But just because Object
is a subclass of Parent
doesn't mean T
is Object
. 但是因为
Object
是Parent
的子类并不意味着T
是Object
。 At runtime, T
could be Dinosaur
, another subclass of Parent
, and adding addMe
to that array would be invalid. 在运行时,
T
可能是Dinosaur
, Parent
另一个子类,并且addMe
数组添加addMe
将无效。
One solution is to initialize addMe
as T()
, and optionally cast to Object
, like so: 一种解决方案是将
addMe
初始化为T()
,并可选择转换为Object
,如下所示:
for object in myArray {
var addMe = T()
if let addMe = addMe as? Object {
// do Objecty stuff here
}
//...
objects.append(addMe)
}
I believe Aaron has you on the right track, but note that this probably means a misuse of generics. 我相信Aaron让你走在正确的轨道上,但请注意,这可能意味着滥用泛型。 You more likely meant:
你更有可能意味着:
func myFunction(data: NSDictionary, inout objects: [Parent]) { ... }
There is no reason to say "T, where T is a subclass of Parent." 没有理由说“T,其中T是Parent的子类。” That is just "Parent" in most OOP systems (including Swift).
在大多数OOP系统(包括Swift)中,这只是“父”。
This is also a very surprising way to handle your return. 这也是处理回报的一种非常令人惊讶的方式。 The normal approach in Swift would be:
Swift中的常规方法是:
func myFunction(data: [String : [Object]) -> [Parent] { ... }
It is extremely uncommon in Swift to pass an empty array and have the function fill it in. You generally generate the array and return it. 在Swift中传递一个空数组并让函数填充它是非常罕见的。你通常生成数组并返回它。 Swift has excellent copy-on-write semantics that make this cheap (unlike C++ per-move-semantics, where this pattern is more common).
Swift具有出色的写时复制语义,这使得这种语法更加便宜(不像C ++ per-move-semantics,这种模式更常见)。
Wherever possible, try to use Swift Arrays and Dictionaries rather than NSArray
and NSDictionary
. 尽可能尝试使用Swift Arrays和Dictionaries而不是
NSArray
和NSDictionary
。 You will save a lot of complicated as?
你会节省很多复杂
as?
conversions. 转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.