1

I pass a dictionary of data from the iPhone to WatchKitExtension. What is the best way to turn the received Dictionary into two different item arrays?

iPhone Data:

let applicationDict = [“Item1” : data.item1, “Item2” : data.item2]
let transfer = WCSession.defaultSession().transferUserInfo(applicationDict)

Watch ExtensionDelegate:

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

func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
        if let item1Value = userInfo[“Item1”] as? String, let item2Value = userInfo[“Item2”] as? String {
            incomingData.append([“Item1” : item1Value , “Item2” : item2Value])
            // use incomingData to make two item arrays
        } 
}

I've looked at some other questions that seem similar, and it seems like something along the lines of componentArray = Array(incomingData.values) but I can't get that to work.

EXAMPLE:

Item1 is cities. Item2 is states. So the Item1 array would be ["Chicago", "San Francisco"], and the Item2 array would be ["Illinois", "California"].

victorpulak
  • 131
  • 2
  • 8
  • Can you explain what you mean by 2 item arrays? You seem to be making an array of dictionaries from an array of dictionaries again. – Karthik Mar 17 '16 at 07:01
  • @Karthik Sure. I want to make an array for every key passed to me that has "Item1" and an array for every key passed to me that has "Item2". And yeah I must not be doing the correct thing trying to do that, so sorry if that is confusing. Any more questions just let me know so I can make sure I'm being clear on what I'm going for even though I can't get there yet – victorpulak Mar 17 '16 at 13:24
  • @Karthik I added an example of what I'm talking about to make it clearer hopefully – victorpulak Mar 17 '16 at 13:51

1 Answers1

2

incomingData is an array of dictionaries. I don't believe this is what you actually want. If you're passing in a city / state pair with each transfer, then this should work in your ExtensionDelegate.

var cities = [String]()
var states = [String]()

func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
    if let item1Value = userInfo["Item1"] as? String, let item2Value = userInfo["Item2"] as? String {
        cities.append(item1Value)
        states.append(item2Value)
    }
}
Dion Larson
  • 868
  • 8
  • 14
  • So I did `let values1 = Array(incomingData[0].values)` but then `value1` ends up being an array of all the 0 index items from the `incomingData`. For example, based on my example I added in my question: I want `values1` to be `["Chicago", "San Francisco"]` not `["Chicago", "Illinois"]`. And `values2` to be `["Illinois", "California"]` not `["San Francisco", "California"]`. That make sense? – victorpulak Mar 17 '16 at 13:55
  • Are `data.item1` and `data.item2` on the iPhone side already arrays of strings? – Dion Larson Mar 17 '16 at 17:49
  • On the iPhone side, I run a for loop with `let data in results!` then loop thru the `item1`s with `data.item1 = data["Item1"] as! String`. Does that make sense? – victorpulak Mar 17 '16 at 22:11
  • I think I understand what you're looking for. Does the answer make sense now? You don't need an array of dictionaries (`incomingData`), you just need to add the strings as you receive them to String arrays. – Dion Larson Mar 17 '16 at 22:30
  • Yup thats perfect! – victorpulak Mar 17 '16 at 22:44