我的情况,我有一个字典@H_502_2@
[String: ???]@H_502_2@
??? :可以是Int,Double,Float,String,Array,Dictionary@H_502_2@
有人可以解释一下Any和AnyObject之间的区别
和女巫使用在我的情况。@H_502_2@
Alak@H_502_2@
所以你应该去[String:Any]。@H_502_2@
Type Casting for Any and AnyObject@H_502_2@
Swift provides two special types for working with nonspecific types:@H_502_2@
Any
can represent an instance of any type at all,including function
types.AnyObject
can represent an instance of any class type.NOTE:@H_502_2@
Use
Any
andAnyObject
only when you explicitly need the behavior and
capabilities they provide. It is always better to be specific about
the types you expect to work with in your code.@H_502_2@
从Swift编程语言:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html#//apple_ref/doc/uid/TP40014097-CH22-ID342@H_502_2@
– – @H_502_2@
还要注意,当您使用Cocoa API时,通常接收AnyObject数组,这是因为Objective-C数组不是典型的。
所以你需要将它们转换为你期望的数组类型。@H_502_2@
– – @H_502_2@
编辑:(2015年12月22日)
在最后一条语句中,注意这是随着Swift 2.0和Xcode 7而改变。
苹果在Objective-C中引入了‘Lightweight’ generics,所以很多Cocoa API现在已经返回了正确的类型。@H_502_2@
编辑:(2016年10月18日)注意,从Swift 3.0开始,Objective-C id现在被导入为Any,而不是AnyObject。@H_502_2@