swiftwatchkitwatchconnectivity

transferUserInfo(_:) doesn't support sending classes


I'm trying to use the WatchConnectivity library to transfer data from my watch to my iPhone. Using the method transferUserInfo(_:), I found out that I can transfer any struct, being an Int, Double or String, but I can't transfer a class with data.

What is so interesting, is that the documentation describes the function as:

func transferUserInfo(_ userInfo: [String : Any] = [:]) -> WCSessionUserInfoTransfer

I would, therefore, see Any as being anything a struct or a class. I don't read specifically that the method doesn't support transferring classes. Should I have read this from the sentence in the Parameter description: "A dictionary of property list values that you want to send"?

Does anybody has some better insight on this method than I can get from the information?


Solution

  • The documentation specifies that the userInfo parameter is:

    A dictionary of property list values that you want to send

    The "property list" bit of this is important. If you look at the Property List Programming Guide we can see that the data types for property list values are:

    CFString, CFNumber, CFBoolean, CFDate, and CFData

    (These get bridged to the appropriate Objective-C and Swift types when we use them in those contexts. Also, dictionaries and arrays of these types work.)

    Trying to include your own class types as values (although it doesn't throw a compile-time error) in the userInfo should not be expected to transfer at runtime.