我有一本字典,我需要变成一个数组,但无法完成实现。
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])
故事板:
答案 0 :(得分:1)
下面的代码做了一些假设并且不会直接相关,因为我对WatchKit和其他代码不是很熟悉。
我认为你真的不需要eventListFromValues方法,我是在XCode的一个游乐场里完成的。
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)
}
}
}