检查数据类型Type


1年前 737次点击 来自 移动端

标签: Swift

检查一个数组是否为[String]:

if let stringArray = obj as? [String] {
    // obj is a string array. 
}
else {
    // obj is not a string array.
}

检查一个Object是否为String

if let str = obj as? String {
    // obj is a string . 
}
else {
    // obj is not a string .
}

或者更简单的写法:

if obj is String{
}

多种类型检查可使用switch

switch object
{
case is String:
    ...

case is OtherClass:
    ...

default:
    ...
}

Made with in Shangrao,China By Devler.

Copyright © Devler 2012 - 2022

赣ICP备19009883号-1

Top ↑