为什么一个斯威夫特阵列<&诠释GT;与AnyObject兼容? [英] Why is a Swift Array<Int> compatible with AnyObject?

查看:95
本文介绍了为什么一个斯威夫特阵列<&诠释GT;与AnyObject兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑code:

struct S {
    var f : Int64 = 0
}

...

let coder : NSCoder = someCoder ...
let a : [Int] = []
coder.encodeObject(a)  // compiles
let b : [Int64] = []
coder.encodeObject(b)  // doesn't compile: not AnyObject
let s : [S] = []
coder.encodeObject(s)  // doesn't compile: not AnyObject

注意内部定义为结构

所以 [INT] 为对象,但 [Int64的]​​ 是不是也不是我的简单结构的阵列

So [Int] is object, but [Int64] is not and neither is my array of simple structures.

有什么特别之处内部

推荐答案

如果你输入基金(你一定是,因为你引用 NS codeR ),那么 [INT] 被隐式桥接到的NSArray ,因为诠释被隐式桥接到的NSNumber 的Int64 和你的非objc结构都不会隐桥接到ObjC类型,所以那些数组不桥接的NSArray

If you import Foundation (which you must be, because you reference NSCoder) then [Int] is implicitly bridged to NSArray because Int is implicitly bridged to NSNumber. Int64 and your non-objc structs are not implicitly bridged to ObjC types, so arrays of those are not bridged to NSArray.

这篇关于为什么一个斯威夫特阵列<&诠释GT;与AnyObject兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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