Search code examples
iosswiftwatchkit

Why method updateApplicationContext returns "Payload contains unsupported type."


I am calling method:

 open func updateApplicationContext(_ applicationContext: [String : Any]) throws

That takes [String : Any].

I have method that returns :

 func watchData() -> [String: Any] {
        var dictionary = [String: Any]()
        let wallets = Database.sharedInstance.database.objects(Wallet.self)
        for wallet in wallets {
            if let qrCode = QRCode(wallet.address) {
                 let watchWallet = WatchWallet(fund: wallet.fund, avaliable: wallet.avaliable, address: wallet.address, imageData: qrCode.PNGData)
                  dictionary[wallet.fund] = watchWallet
            }
        }
        return dictionary
    }

And data that is returned from watchData() is not valid.

WatchWallet is strcut.

struct WatchWallet {
    let fund: String
    let avaliable: String
    let address: String
    let imageData: Data
}

I am wondering what i am missing.

Error from the method:

Unable to send application context: Error Domain=WCErrorDomain Code=7010 "Payload contains unsupported type." UserInfo={NSLocalizedDescription=Payload contains unsupported type., NSLocalizedRecoverySuggestion=Only pass valid types.}


Solution

  • From: https://developer.apple.com/library/content/documentation/General/Conceptual/WatchKitProgrammingGuide/SharingData.html

    For most types of transfers, you provide an NSDictionary object with the data you want to send. The keys and values of your dictionary must all be property list types, because the data must be serialized and sent wirelessly. (If you need to include types that are not property list types, package them in an NSData object or write them to a file before sending them.)

    So, the fact that the dictionary is [String: Any] is misleading, because the Any values are restricted to valid property list types. The full list of types is in the article below, but as you might expect) include primitive types, arrays, dictionaries, and NSData:

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html