Any和AnyObject之间的Swift异常

时间:2014-07-24 19:15:50

标签: arrays swift

我一直在玩Swift。我正在尝试创建一个包含已知不同类型的Swift变量的数组。

我可以使用Any of Array或AnyObject数组获得大部分内容 - 但完整的工作也没有。我发现了一些异常 - 但我不知道它们是否是错误或功能。

以下是创建和测试数组的代码:

import UIKit
import Foundation

var aA: Array = [Any]()
aA.append("a String")          // Defaults to String
aA.append(123)                 // Defaults to Int
aA.append(-456.00)             // Defaults to Double
aA.append(0.33 as Float)       // Must cast as Float
aA.append(false)               // Defaults to Bool
aA.append("C" as Character)    // Must cast as Character

println("\naA: \(aA)\n")
(aA[0] is String)    ? println("aA[0] is String:    \(aA[0])") : println("aA[0] is Not String: \(aA[0])")
(aA[1] is Int)       ? println("aA[1] is Int:       \(aA[1])") : println("aA[1] is Not Int:    \(aA[1])")
(aA[2] is Double)    ? println("aA[2] is Double:    \(aA[2])") : println("aA[2] is Not Double: \(aA[2])")
(aA[3] is Float)     ? println("aA[3] is Float:     \(aA[3])") : println("aA[3] is Not Float:  \(aA[5])")
(aA[4] is Bool)      ? println("aA[4] is Bool:      \(aA[4])") : println("aA[4] is Not Bool:   \(aA[4])")
(aA[5] is String)    ? println("aA[5] is String:    \(aA[5])") : println("aA[5] is Not String: \(aA[5])")
(aA[5] is Character) ? println("aA[5] is Character: \(aA[5])") : println("aA[5] is Not Character: \(aA[5])")

var aO = [AnyObject]()
aO.append("a String")          // Defaults to String
aO.append(123)                 // Defaults to Int
aO.append(-456.00)             // Defaults to Double
aO.append(0.33)                // Defaults as Float
aO.append(false)               // Defaults to Bool
aO.append("C")                 // Defaults to String -- Cannot cast as Character

println("\naO: \(aO)\n")
(aO[0] is String) ? println("aO[0] is String: \(aO[0])") : println("aO[0] is Not String: \(aO[0])")
(aO[1] is Int)    ? println("aO[1] is Int:    \(aO[1])") : println("aO[1] is Not Int:    \(aO[1])")
(aO[2] is Double) ? println("aO[2] is Double: \(aO[2])") : println("aO[2] is Not Double: \(aO[2])")
(aO[3] is Float)  ? println("aO[3] is Float:  \(aO[3])") : println("aO[3] is Not Float:  \(aO[5])")
(aO[4] is Bool)   ? println("aO[4] is Bool:   \(aO[4])") : println("aO[4] is Not Bool:   \(aO[4])")
(aO[5] is String) ? println("aO[5] is String: \(aO[5])") : println("aO[5] is Not String: \(aO[5])")


/*

Anomalies between Any and AnyObject

aA[2] -456.00 Double returns -456.0 ................... aO[2] -456.00 Double returns -456
aA[3] 0.33 Must cast as Float ......................... aO[3] 0.33 defaults to Float
aA[3] 0.33 Float returns 0.330000013113022 ............ aO[3] 0.33 Float returns 0.33
aA[4] false Bool returns false ........................ aO[4] false Bool returns 0
aA[5] "C" Defaults to String must cast as Character ... aO[5] "C" Defaults to String -- Cannot cast as Character
aA[5] "C" Character returns Character ................. aO[5] "C" Defaults to String -- Cannot Test for Character

*/

异常显示为注释(上图)。

这是Playground输出:

aA: [a String, 123, -456.0, 0.330000013113022, false, C]

aA[0] is String:    a String
aA[1] is Int:       123
aA[2] is Double:    -456.0
aA[3] is Float:     0.330000013113022
aA[4] is Bool:      false
aA[5] is Not String: C
aA[5] is Character: C

aO: [a String, 123, -456, 0.33, 0, C]

aO[0] is String: a String
aO[1] is Int:    123
aO[2] is Double: -456
aO[3] is Float:  0.33
aO[4] is Bool:   0
aO[5] is String: C

1 个答案:

答案 0 :(得分:2)

您看到的异常是因为在基本Swift类型和基本Objective-C类型之间发生了一些隐式转换。例如,在Swift中,String是一个结构,因此无法添加到AnyObject数组中。但是,因为您要导入Foundation,所以它被隐式转换为NSString,这是一个类,因此可以添加到AnyObject数组中。

另一方面,角色没有转换为Foundation中可用的类。

我在blog post一段时间后写了更多关于此的内容。