我有一本字典,需要将其转换为数组,但无法完成实现。
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:
最佳答案
下面的代码做了一些假设,不会直接相关,因为我对 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/