SwiftyJSON - 可以检查对象类型吗?
SwiftyJSON - is possible to check object type?
例如我有一个json
var json = JSON(data: data!)
在里面我引用了对象
var list = json["OBJECT"]
有没有办法检查它是对象、数组还是字符串以及 return 布尔值?
This 没有帮助。 var list
将始终是 JSON
的类型。我想找到一种方法来检查里面的东西。
看这个例子:
//let json = ["OBJECT":"stringvalue"]
let testArray = [1,2,3]
let json = ["OBJECT":testArray]
if let element = json["OBJECT"] {
if element is String {
print("yes")
}
switch element {
case is String:
print("is string")
case is Array<Int>:
print("is array of int")
default:
print("is something else")
}
}
SwiftyJSON 中的 JSON 对象有一个 type
属性 类型是 enum
public enum Type: Int {
case number
case string
case bool
case array
case dictionary
case null
case unknown
}
例如
var list = json["OBJECT"]
switch list.type {
case .array: print("list is Array")
case .dictionary: print("list is Dictionary")
default: break
}
例如我有一个json
var json = JSON(data: data!)
在里面我引用了对象
var list = json["OBJECT"]
有没有办法检查它是对象、数组还是字符串以及 return 布尔值?
This 没有帮助。 var list
将始终是 JSON
的类型。我想找到一种方法来检查里面的东西。
看这个例子:
//let json = ["OBJECT":"stringvalue"]
let testArray = [1,2,3]
let json = ["OBJECT":testArray]
if let element = json["OBJECT"] {
if element is String {
print("yes")
}
switch element {
case is String:
print("is string")
case is Array<Int>:
print("is array of int")
default:
print("is something else")
}
}
SwiftyJSON 中的 JSON 对象有一个 type
属性 类型是 enum
public enum Type: Int {
case number
case string
case bool
case array
case dictionary
case null
case unknown
}
例如
var list = json["OBJECT"]
switch list.type {
case .array: print("list is Array")
case .dictionary: print("list is Dictionary")
default: break
}