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] = []
    }
}
Answered by DTS Engineer in 354377022

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"

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…

Accepted Answer

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"

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

Converting struct so it can be sent from watch to phone
 
 
Q