arrays - 从值字典中获取数组

标签 arrays swift dictionary

我有一本字典,需要将其转换为数组,但无法完成实现。

InterfaceController 中的值字典...

var receivedData = Array<Dictionary<String, String>>()



func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {

    if let tColorValue = userInfo["TeamColor"] as? String, let matchValue = userInfo["Matchup"] as? String {
        receivedData.append(["TeamColor" : tColorValue , "Matchup" : matchValue])

        let eventsList = Event.eventsListFromValues(receivedData)

        for eventM in eventsList {
            NSLog("Event Match: %@", eventM.eventMatch)
        }

    } else {
        print("tColorValue and matchValue are not same as dictionary value")
    }

}

处理 Event 类中的值字典:

    class func eventsListFromValues(values: Array<Dictionary<String, String>>) -> Array<Event> {
        var array = Array<Event>()

        for eventValues in values {
            let event = Event(dataDictionary: eventValues)
            array.append(event)
        }

        return array
    }

}

无法弄清楚这部分...使用数组在setupTable中设置表格:

    func doTable() {

            // ...get array of `match`s for use in table set up

            // ...Then set number of Rows
            // ...Then iterate thru the array
                  for var i = 0; i < self.rowTable.numberOfRows; i++ {
                      var row = self.rowTable.rowControllerAtIndex(i)
                      // ...setup text label
                   }
    }

编辑:澄清

doTable 将获取收到的任何match 并将它们显示在表格中。所以我认为我应该做的是获取一个match数组,然后使用它们在表格中设置文本标签。

编辑2:这是我到目前为止所拥有的

class InterfaceController: WKInterfaceController, WCSessionDelegate {

    class EventSO {
        var teamColor:String!
        var matchup:String!

        init(dataDictionary:[String:String]) {
            teamColor = dataDictionary["teamColor"]
            matchup = dataDictionary["Matchup"]
        }
    }

    var receivedDataSO = [Event]()

    var tColorValueSO = "Red"
    var matchValueSO = "someString"

    var eventSO = EventSO(dataDictionary: ["teamColor": tColorValueSO, "Matchup": matchValueSO])

Storyboard:

enter image description here

最佳答案

下面的代码做了一些假设,不会直接相关,因为我对 WatchKit 和其余代码不太熟悉。

我认为您并不真正需要 eventListFromValues 方法,我在 XCode 的 Playground 中完成了此操作。

class Event {
    var teamColor:String!
    var matchup:String!

    init(dataDictionary:[String:String]) {
        teamColor = dataDictionary["teamColor"]
        matchup = dataDictionary["Matchup"]
    }
}

var receivedData = [Event]()

var tColorValue = "Red"
var matchValue = "someString"

var event = Event(dataDictionary: ["teamColor": tColorValue, "Matchup": matchValue])

receivedData.append(event)

func doTable(events: [Event]) {
    myTable.setNumberOfRows(events.count, withRowType: "someRowController")

    for (index, evt) in events {
            let row = myTable.rowControllerAtIndex(index) as someRowController
            row.interfaceLabel.setText(evt.teamColor)
    }
}

因此,使用上面的代码,您应该能够删除 eventListFromValues 函数,并且当您收到 userInfo 时,根据收到的值创建一个新的 Event 对象并将其添加到数组的末尾。

然后,您的 doTable 函数将迭代数组并根据行上的数据设置表的 UI 部分。

因此,要在您的具体情况下使用此代码,您可以执行以下操作:

class InterfaceController: WKInterfaceController, WCSessionDelegate {

    var evnts = [Event]()

    func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {

        if let tColorValue = userInfo["TeamColor"] as? String, let matchValue = userInfo["Matchup"] as? String {
            var event = Event(dataDictionary: ["teamColor": tColorValue, "Matchup": matchValue])
            events.append(event)
            doTable()
        } else {
            print("tColorValue and matchValue are not same as dictionary value")
        }

    }

    func doTable() {
        myTable.setNumberOfRows(events.count, withRowType: "someRowController")

        for (index, evt) in events {
            let row = myTable.rowControllerAtIndex(index) as someRowController
            row.interfaceLabel.setText(evt.teamColor)
        }
    }
}

关于arrays - 从值字典中获取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33054959/

相关文章:

java - 任何 Map 子类中是否存在原子 "set if empty"操作?

python - Pandas 匹配字典 'else'

javascript - 获取 MySql 数据到 PHP 数组并转换为 Javascript 数组

arrays - 运算符 Array#<< 以 reduce 的简写形式失败

arrays - Swift 2.2 中的多维数组 removeAtIndex

ios - Swift 中 headerView 中的动画

Python - 将数据中的代码映射到描述的最佳方法

php - 在 mysql 中使用 php 数组查询

java - 矩阵中的 ArrayIndexOutOfBoundsException

ios - 在警报中点击 Return Key Tap 时的操作