ios - 从表的字典中获取数据

标签 ios swift uitableview nsdictionary watchkit

我有一个 WatchKit table,我需要用我收到的数据(从 iOS 到 WatchKit)填充它。我不知道如何解包Dictionary 中的数据以便在 中使用。

我在 WatchKit InterfaceController.swift

中有数据
func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {

    let dic3 = userInfo["TColor"] as? String
    let dic4 = userInfo["Match"] as? String

    dispatch_async(dispatch_get_main_queue()) {
        //
    }
}

WatchKit Event.swift(主对象的子类)

class Event {

var eventTColor:String
var eventMatch:String

init(dataDictionary:Dictionary<String,String>) {
    eventTColor = dataDictionary["TColor"]!
    eventMatch = dataDictionary["Match"]!
}

class func newEvent(dataDictionary:Dictionary<String,String>) -> Event {
    return Event(dataDictionary: dataDictionary)
}

class func eventsList() -> [Event] {

    var array = [Event]()
    let dataPath = // Not sure what to do here

    let data = // Not sure what to do here either

    for e in data as! [Dictionary<String, String>] {
        let event = Event(dataDictionary: e)
        array.append(event)
    }

    return array
}

}

我不知道在我缺少的函数 class func eventsList() -> [Event] 中该做什么。

我需要从 didReceiveUserInfo 中收到的 Dictionary 中获取信息。

或者也许有更好的/其他方法来解决这个问题?将根据需要发布任何额外代码。

编辑:这就是我目前为 didFInishUserInfoTransfer 所做的尝试

func session(session: WCSession, didFinishUserInfoTransfer userInfoTransfer: WCSessionUserInfoTransfer, error: NSError?) {
        if let someError = error {
            print("error on didFinishUserInfoTransfer: %@", someError)
        } else {
            let eventsList = Event.eventsListFromValues(receivedData)
            NSLog("Events List: %@", eventsList)

        }
    }

函数 doTable() {

eventsListSO = Event.eventsListFromValues(receivedData)
rowTable.setNumberOfRows(eventsListSO.count, withRowType: "rows")
NSLog("Row count: %@", eventsListSO.count)

for var i = 0; i < self.rowTable.numberOfRows; i++ {
    let row = rowTable.rowControllerAtIndex(i) as? TableRowController
    print("Row")
    for eventm in eventsListSO {
        row!.mLabel.setText(eventm.eventMatch)
        NSLog("SetupTableM: %@", eventm.eventMatch)
    }
}

最佳答案

从发布的代码中很难分辨,但它看起来像你的 InterfaceController正在以两个 String 的形式接收单个事件的数据userInfo 中的值字典参数。据推测,如果您需要一个列表,则会多次调用此方法。

如果以上为真,那么创建一个 var 可能是有意义的属性(property) InterfaceController类型 Array<Dictionary<String,String>> (或等效的 [[String : String]] ,我个人觉得可读性较差),像这样:

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

然后在你执行session:didReceiveUserInfo:您会将内容附加到 receivedData属性(property),像这样:

func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
    if let tColorValue = userInfo["TColor"] as? String, let matchValue = userInfo["Match"] as? String {
        receivedData.append(["TColor": tColorValue, "Match": matchValue])
    }
    else {
        // Appropriate error handling here
    }
}

你的 eventsList然后需要更改方法以接受您的 Dictionary值并处理它,也许像这样:

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
}

有了这些,您现在就有了数据值的集合,您需要确定何时是构建 Array 的适当时机。的 Event对象。也许在每次调用 session:didReceiveUserInfo: 之后像这样的一些代码:

let eventsList = Event.eventsListFromValues(receivedData)
for event in eventList {
  // Do something with the events list (i.e., use event.eventTColor and event.eventMatch)
}

显然,我根据对正在发生的事情的一些假设得出了这个答案,因此它可能不完全符合您正在发生的事情,但希望能让您朝着正确的方向前进。另外,我在没有编译器的情况下编写了这段代码,因此可能需要进行一些小的调整。

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

相关文章:

ios - 在 SwiftUI 的 Picker 中添加新元素

ios - Swift 中购物应用程序的加号和减号按钮

ios - 在 UIView 框架内绘制圆圈

swift - 使用可重复使用的单元来保存数据

ios - 如何跳过动画的一部分?

iphone - 如何让 UIWebView 加载网页,但 URL 在 UILabel 上?

arrays - Swift 字典在设置键值对后返回 nil

swift - watch 操作系统 2 : Is it possible to get bluetooth signal strength of connectivity between paired iPhone and Apple Watch?

cocoa-touch - UISearchBar 未出现

ios - VoiceOver 不读取 UITextField subview