Can Apple Watch workout metadata use JSON strings?

204 Views Asked by At

I've got a working Apple Watch workout app. My metadata saves and all workout data flows to iPhone. I'm also able retrieve and display the data. But when i try to add arrays converted to ... json strings ... to metadata, the app crashes on save. Every time. I've tried numerous variations, always it's the same. Here's the latest code to crash... and it's perfectly fine.


GOOD CODE that works, but return string...

CRASHES with every HKWorkoutSession save.

func toJSON(array: [[String: Any]]) throws -> String {
    let data = try JSONSerialization.data(withJSONObject: array, options: [])
    return String(data: data, encoding: .utf8)!
}

NOW ... when my configuration class is converted with strings created using the function below, metadata saves just fine... and i'm back where i started. Wondering how to restore the [[String:Any]] arrays from String.


SAVES on WatchOS 3

This code creates a string from array of dictionaries.

What I'm needing help with is function to restore strings created using this function back into original form of [ [ String : Any ] ]

func joinedRepresentationOfArrayOfArrays(newArray: [[String : Any]]) -> String {
    var newString = ""
    for dictionary in newArray {
        newString = newString.appending("[")
        for (key, value) in dictionary {
            newString = newString.appending("[\(key) : \(value), ")
        }
        newString = newString.appending("], ")
    }
    newString = newString.appending("], ")
    return newString
}
0

There are 0 best solutions below