从__NSArrayM中提取值

时间:2017-07-30 16:28:22

标签: swift nsarray

我已经打印了我想要获得的值,就像这样:

a =  Optional(<__NSArrayM 0x1704494b0>(9))

如何提取值9? 我写了这个但是错了:let a = (parseJSON[index] as AnyObject).object(forKey: "subjects_count")

2 个答案:

答案 0 :(得分:1)

试试这个: -

guard let a = (parseJSON[index] as AnyObject).object(forKey: "subjects_count"), let value = a.first else {return}
let stringValue = String(value)
print(stringValue)

答案 1 :(得分:0)

使用Swift本机类型和密钥/索引订阅

if let dict = parseJSON[index] as? [String:Any],
   let subjectsCount = dict["subjects_count"] as? [Int], !subjectsCount.isEmpty {
   print(subjectsCount[0])
}