CptLeChuck70
CptLeChuck70

Reputation: 105

Swift array in dictionary leads to NSCFArray

I prepare a swift Array in my Watch Interface and send it to the iOS App:

@IBAction func buttonGeklickt() {

if WCSession.isSupported() {
    let session = WCSession.defaultSession()
    session.delegate = self
    session.activateSession()

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "hh:mm"
    let datumString = dateFormatter.stringFromDate(NSDate())

    var swiftArray = [String]()
    swiftArray.append(datumString)

    var swiftDict = ["a":swiftArray]

    session.transferUserInfo(swiftDict)

}

so far so good, on the iOS App the dictionary arrives, but there seems to be something wrong with the Array in the Dictionary:

func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {

    print ("seems to be the same Dict = \(userInfo)")

    if let vw = userInfo["a"] as? [String: String] {
        print ("Never called! Here I would expect my array from the watch \(vw)")
    }
}

I would expect and like vw to hold the same array as swiftArray in the watchApp. However it seems to be of type __NSCFArray: screenshot So what I'm doing wrong here?

I'm new to Swift, however I'm experienced with Objective C to solve actually every problem I faced in the past years, but this issue seems to be so basic and it's embarrassing that I'm not able to solve it on my own. So help is much appreciated

Upvotes: 0

Views: 660

Answers (1)

MirekE
MirekE

Reputation: 11555

If I understand your code correctly, you are saving "a" as value of type [String]. But you are trying to read it as [String:String]. Instead of

if let vw = userInfo["a"] as? [String: String] 

try

if let vw = userInfo["a"] as? [String]

Upvotes: 3

Related Questions