【发布时间】:2014-09-17 22:17:49
【问题描述】:
问题
是否可以在我的设置中使用Array 调用countElements()?
问题详解
countElements() 与 String 配合得很好。但是我不知道如何将thing 转换为Array,因此不调用countElements()。
请注意,方法签名必须是func myCount(thing: Any?) -> Int,因为这是used in my open source project。
func myCount(thing: Any?) -> Int {
if thing == nil {
return -1
}
if let x = thing as? String {
return countElements(x)
}
if let y = thing as? Array<Any> {
return countElements(y) // this if is never taken
}
return -1
}
myCount(nil) // -1
myCount("hello") // 5
myCount([1, 2, 3]) // BOOM, returns -1, I'm expecting 3 returned
【问题讨论】: