ios - Swift 字典的 Swift 数组

标签 ios arrays xcode swift dictionary

我将这个数组初始化为一个空数组:

var dataArray:[Any] = []

我将字典添加到 dataArray,它看起来像这样:

[[onlineStatus: Offline, whatDoing: , gamerTag: KgLui720, id: 2533274926939486], [onlineStatus: Offline, whatDoing: , gamerTag: BigBear46, id: 2533274801083843], [onlineStatus: Online, whatDoing: Watching TV, gamerTag: PeeMilroy12, id: 2533274975595658], [onlineStatus: Offline, whatDoing: , gamerTag: BR0, id: 2533275031633640], [onlineStatus: Offline, whatDoing: , gamerTag: jerrycia224, id: 2533274959352491]]

字典数组:

var index = 0

while index < dataArray.count{
    let row = table.rowControllerAtIndex(index) as tableRowController
    let theScreenName:NSString = dataArray[index]["gamerTag"]
    row.screenNameLabel.setText(theScreenName)
    index++
}

这给出了错误:

'Any' does not have a member named subscript

如有任何帮助,我们将不胜感激。

最佳答案

您应该更具体地说明数组可以存储什么,即具有 String 键和 AnyObject 值的对象。因此,该数据类型的语法为 [String: AnyObject]。这应该允许您使用下标符号:

var dataArray: [[String: AnyObject]] = []

你也可以施放它:

let theScreenName: NSString = (dataArray[index] as [String: AnyObject])["gamerTag"]

但是,如果您从一开始就表明这是您的意图,那么您的代码会更加连贯。

关于ios - Swift 字典的 Swift 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28932218/

相关文章:

javascript用不同的单位对距离进行排序

ios - 当针对 iOS 进行 Xcode 调试时,GStreamer 出现不必要的启动延迟和单元测试崩溃

iphone - 为 App Store 和 Enterprise Distribution 构建应用程序

ios - initWithDelegate问题-Blackraccoon

iphone - UIScrollView 更新 contentSize 导致滚动

JavaScript,从原始对象的副本中删除属性,实际上影响了原始对象

Javascript - 如何读取文件

objective-c - self.TableView 不适用于 ViewController 内的 TableViewController?

iOS 使用多个滑动识别器

ios - iOS 设备上未显示开发人员菜单