Hullo, I am trying to implement iCloud support for a Dictionary in Swift without resorting to CloudKit in oder to retain compatibility with iOS 7. This is the Swift code I employ in a UIDocument subclass:
override init(fileURL: NSURL){
super.init(fileURL: fileURL)
}
convenience init() {
let ubiq = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil)
var finalURL:NSURL?
if (ubiq != nil){
let ubiquitousPackage = ubiq!.URLByAppendingPathComponent("Documents")
finalURL = ubiquitousPackage.URLByAppendingPathComponent("favorite")
self.init(fileURL: finalURL!)
self.loadDocumentFromiCloud()
} else {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0];
let filePath = String(format:"%@/%@", documentsDirectory, "favorite")
finalURL=NSURL(string: filePath)
self.init(fileURL: finalURL!)
let dict=NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? Dictionary<String, palinaModel>
if (dict != nil) {
favoriteStops=dict!
}
}
}
Yet, when I execute it on the simulator, where of course iCloud is not available and the control goes to the 'else' case, I get a crash when calling super.init(fileURL: fileURL) with the value obtained in that function that in a test case is:
/Users/.../Library/Developer/CoreSimulator/Devices/B5469736-F27B-4E14-837A-7B9771D9A1A7/data/Containers/Data/Application/B2F7AADB-CF71-4E27-AAD7-5F1455C5DD9E/Documents/favorite
That does in fact seem not much of a url; how do I create a URL to pass to the UIDocument init(fileUrl:) in the case iCloud is not available? Otherwise is there some technique by which to store a Dictionary on iCloud without using Cloudkit?