Search code examples
iosswiftuitableviewnsdictionarywatchkit

Get data from Dictionary for Table


I have a WatchKit table that I need populated with the data I received (from iOS to WatchKit). I can't figure out how to unwrap the data in the Dictionary for use in the table.

I have the data in the 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 (subclass of main object)

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
}

}

I can't figure out what to do in the piece of the function class func eventsList() -> [Event] that I'm missing.

I need to get the information from the Dictionary I received in didReceiveUserInfo.

Or maybe there's some better/other way to solve this? Will post any extra code as needed.

EDIT: This is what I'm trying for didFInishUserInfoTransfer so far

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)

        }
    }

func 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)
    }
}

}


Solution

  • It's a little hard to tell from the code posted, but it looks like your InterfaceController is receiving the data for a single event in the form of two String values in the userInfo dictionary parameter. Presumably if you are expecting a list, this method is getting called multiple times.

    If the above is true, it would probably make sense to create a var property in InterfaceController of type Array<Dictionary<String,String>> (or the equivalent [[String : String]], which I personally find less readable), like so:

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

    Then in your implementation of session:didReceiveUserInfo: you would append the content to the receivedData property, like so:

    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
        }
    }
    

    Your eventsList method would then need to be changed to accept your Dictionary of values and process it, perhaps like so:

    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
    }
    

    With these things in place you now have a collection of your data values, and you would need to determine when it is an appropriate time to construct your Array of Event objects. Perhaps after every call to session:didReceiveUserInfo: with some code like so:

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

    Obviously I based this answer on some assumptions about what's going on, so it may not be correct for exactly what you've got going on, but hopefully will get you going in the right direction. Also, I wrote this code without a compiler handy, so it may need some minor tweaks.