Assume the following Dictionary:
var example: [String: (identifier: String, regex: NSRegularExpression)] = ["test": (identifier: "example", regex: try! NSRegularExpression(pattern: "test", options: []))]
And I want to store it as follows:
let keyStore = NSUbiquitousKeyValueStore.default()
keyStore.set(example, forKey: "ex")
My problem is that when I try to access it:
let test: [String: (identifier: String, regex: NSRegularExpression)] = keyStore.dictionary(forKey: "ex") as! [String: (identifier: String, regex: NSRegularExpression)]
I receive the following error:
Unwrapped optional value
Why is this?
You are trying to hand your dictionary across to Objective-C, which requires an Objective-C NSDictionary; but you cannot store a Swift tuple as a value in an Objective-C NSDictionary. Moreover, the rules for NSUbiquitousKeyValueStore are even more stringent: not only must this be an NSDictionary, you can only use property list types, which are extremely limited. You would need to do something like wrap a CGSize in an NSValue and archive that to an NSData in order to use it here:
To get the value back out, reverse that procedure.