Converting struct so it can be sent from watch to phone

I want to send struct data from my watchOS-app to my iOS-app using WatchConnectivity. As I have understood, you can only send property list objects, hence I must convert my struct somehow. I tried converting it into a NSDictionary, and also make it a Codable struct, but it doesn't work since I have properties that does not conform to the Codable prototocols. Any suggestions on how this can be solved in a good way?


I'm using XCode Version 10.1 and Swift 4.2.


Here is my struct:

struct Storage {
    
    var acc: Acc
    struct Acc  {
        var x: [Double] = []
        var y: [Double] = []
        var z: [Double] = []
    }
    
    
    var gyro: Gyro
    struct Gyro {
        var x: [Double] = []
        var y: [Double] = []
        var z: [Double] = []
    }
    
    var mag: Mag
    struct Mag {
        var x: [Double] = []
        var y: [Double] = []
        var z: [Double] = []
    }
    
    var pulse: [Double] = []
    
    var quaternion: Quaternion
    struct Quaternion {
        var w: [Double] = []
        var x: [Double] = []
        var y: [Double] = []
        var z: [Double] = []
    }
}

Accepted Reply

I tried [making] it a

Codable
struct, but it doesn't work since I have properties that does not conform to the
Codable
prototocols

Right, but these properties are composed of types that are easy to make codable. If you add a

Codable
conformance to
Storage
and all its nested typed (
Acc
,
Gyro
,
Mag
and
Quaternion
), then the compiler will synthesise the entire implementation.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
  • wow, this explanation is awful. No code example, just random words that may be understood by experts only. It looks like this is written by Apple. Wait!

  • That was visibly easily understood by the OP writer, @Karonsbo. It simply means adding Codable to each struct declaration:  such as: struct Storage : Codable { var acc: Acc struct Acc : Codable {        Straightforward, isn't it ?

Add a Comment

Replies

Did you try converting to a dictionary of [String:String].


You could add a converter in your struct


struct Acc  {
        var x: [Double] = []
        var y: [Double] = []
        var z: [Double] = []

    var asString : String {
        return String(format: "%.2f", x[0]) + ";" + String(format: "%.2f", y[0]) + ";" + String(format: "%.2f", z[0])
    }
}


Use as

let acc = Acc (x: [1.0], y: [3.0], z: [-2.5])
print(acc.asString)


That's just to give the idea, of course you would have to elaborate, add some test to make it robust…

I tried [making] it a

Codable
struct, but it doesn't work since I have properties that does not conform to the
Codable
prototocols

Right, but these properties are composed of types that are easy to make codable. If you add a

Codable
conformance to
Storage
and all its nested typed (
Acc
,
Gyro
,
Mag
and
Quaternion
), then the compiler will synthesise the entire implementation.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
  • wow, this explanation is awful. No code example, just random words that may be understood by experts only. It looks like this is written by Apple. Wait!

  • That was visibly easily understood by the OP writer, @Karonsbo. It simply means adding Codable to each struct declaration:  such as: struct Storage : Codable { var acc: Acc struct Acc : Codable {        Straightforward, isn't it ?

Add a Comment

Thank you both for enlightening me on different ways to do this! I added Codable to my properties also and now it works. 🙂