【发布时间】:2018-01-19 04:01:01
【问题描述】:
我正在将我的代码从 Swift 2.3 更新到 Swift 3,我在 Swift 中从循环中获取数组键值时遇到了这些困难:
var countryarray = NSMutableArray()
self.GetCountriesResult = (responseJSON.objectForKey("GetCountriesResult") as? NSArray)!
for i in 0 ..< self.GetCountriesResult.count {
self.countryarr = self.GetCountriesResult.objectAtIndex(i).objectForKey("countryname") as? String ?? ""
self.countryarray.addObject(self.countryarr)
}
【问题讨论】:
-
您使用的是 Swift 3?摆脱 NSArray 的东西(几乎所有以“NS”开头的东西)并改用 Swift 类型。你得到了什么错误?我猜,Any 没有
objectForKey()或类似的东西。这是一个常见的问题。你需要告诉编译器self.GetCountriesResult.objectAtIndex(i)是一个Dictionary并且你可以在上面调用objectForKey。 -
错误提示?
-
如果可以,请提供 JSON 响应示例。
标签: ios arrays for-loop swift3 swift2.3