From 74adf5386dffc8da819b6e9037263c156ca1105c Mon Sep 17 00:00:00 2001 From: dawand Date: Tue, 5 Apr 2016 00:55:12 +0100 Subject: [PATCH] Updated the code for Swift 2 version --- .../project.pbxproj | 8 ++- .../CloudKitDictionarySyncer.swift | 52 +++++++++++-------- CloudKitDictionarySyncer/Info.plist | 2 +- CloudKitDictionarySyncer/ViewController.swift | 18 +++---- CloudKitDictionarySyncerTests/Info.plist | 2 +- 5 files changed, 48 insertions(+), 34 deletions(-) diff --git a/CloudKitDictionarySyncer.xcodeproj/project.pbxproj b/CloudKitDictionarySyncer.xcodeproj/project.pbxproj index 8df53be..a8171c1 100644 --- a/CloudKitDictionarySyncer.xcodeproj/project.pbxproj +++ b/CloudKitDictionarySyncer.xcodeproj/project.pbxproj @@ -170,7 +170,8 @@ E505A25E1A87DC08004DCFAB /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0610; + LastSwiftUpdateCheck = 0730; + LastUpgradeCheck = 0730; ORGANIZATIONNAME = Baresi; TargetAttributes = { E505A2651A87DC08004DCFAB = { @@ -297,6 +298,7 @@ "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_DYNAMIC_NO_PIC = NO; GCC_OPTIMIZATION_LEVEL = 0; @@ -363,6 +365,7 @@ "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; INFOPLIST_FILE = CloudKitDictionarySyncer/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "se.baresi.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE = ""; }; @@ -377,6 +380,7 @@ "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; INFOPLIST_FILE = CloudKitDictionarySyncer/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "se.baresi.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE = ""; }; @@ -396,6 +400,7 @@ ); INFOPLIST_FILE = CloudKitDictionarySyncerTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "se.baresi.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/CloudKitDictionarySyncer.app/CloudKitDictionarySyncer"; }; @@ -411,6 +416,7 @@ ); INFOPLIST_FILE = CloudKitDictionarySyncerTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "se.baresi.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/CloudKitDictionarySyncer.app/CloudKitDictionarySyncer"; }; diff --git a/CloudKitDictionarySyncer/CloudKitDictionarySyncer.swift b/CloudKitDictionarySyncer/CloudKitDictionarySyncer.swift index e1ae8c6..70aef2b 100644 --- a/CloudKitDictionarySyncer/CloudKitDictionarySyncer.swift +++ b/CloudKitDictionarySyncer/CloudKitDictionarySyncer.swift @@ -30,14 +30,14 @@ class CloudKitDictionarySyncer { self.dictname = dictname self.debugflag = debug let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray - let documentsDirectory = paths[0] as! String - self.plistpath = documentsDirectory.stringByAppendingPathComponent("\(dictname).plist") + let documentsDirectory = NSURL(fileURLWithPath:paths[0] as! String) + self.plistpath = documentsDirectory.URLByAppendingPathComponent("\(dictname).plist").absoluteString self.container = CKContainer.defaultContainer() } // MARK: - Public methods - func loadDictionary(#onComplete: (LoadResult) -> ()) { + func loadDictionary(onComplete: (LoadResult) -> ()) { load { @@ -50,7 +50,7 @@ class CloudKitDictionarySyncer { } else { // try to get dict from icloud - self.fetchDictionaryFromiCloud(onComplete: { + self.fetchDictionaryFromiCloud({ _,dictFromiCloud in var icloudTimestamp:UInt32 = 0 @@ -96,7 +96,7 @@ class CloudKitDictionarySyncer { func saveDictionary(dictionary:NSDictionary, onComplete: (String) -> ()) { - load { + // load { var mutableDict:NSMutableDictionary = dictionary.mutableCopy() as! NSMutableDictionary // Add save timestamp @@ -115,7 +115,7 @@ class CloudKitDictionarySyncer { } else { onComplete("CDS: saveDictionary complete, only saved to plist file") } - } + // } } @@ -125,10 +125,10 @@ class CloudKitDictionarySyncer { if !self.debugflag { return } - println(msg) + print(msg) } - private func load(#onComplete: () -> ()) { + private func load(onComplete: () -> ()) { if self.loaded { onComplete() return @@ -171,7 +171,7 @@ class CloudKitDictionarySyncer { // MARK: - Private CloudKit methods private func saveDictionaryToiCloud(dict: NSDictionary, onComplete: (String) -> ()) { - fetchDictionaryFromiCloud(onComplete: { + fetchDictionaryFromiCloud({ fetchedRecord,_ in var record:CKRecord if fetchedRecord != nil { @@ -180,7 +180,8 @@ class CloudKitDictionarySyncer { record = CKRecord(recordType: "Plists", recordID: CKRecordID(recordName: self.dictname)) } var plisterror:NSError? - if let data:NSData = NSPropertyListSerialization.dataWithPropertyList(dict, format:NSPropertyListFormat.XMLFormat_v1_0, options:0, error:&plisterror) { + do{ + if let data:NSData = try NSPropertyListSerialization.dataWithPropertyList(dict, format:NSPropertyListFormat.XMLFormat_v1_0, options:0) { if let datastring = NSString(data:data, encoding:NSUTF8StringEncoding) { record.setValue(self.dictname, forKey: "dictname") record.setValue(datastring, forKey: "plistxml") @@ -193,7 +194,9 @@ class CloudKitDictionarySyncer { if plisterror != nil { onComplete("CDS: Error saving in cloudkit. Searialize error: \(plisterror!)") } - + }catch{ + print(plisterror) + } self.privateDB!.saveRecord(record, completionHandler: { (record, error) -> () in if error != nil { onComplete("CDS: Error saving in cloudkit \(error!)") @@ -204,13 +207,13 @@ class CloudKitDictionarySyncer { }) } - private func fetchDictionaryFromiCloud(#onComplete: (CKRecord?, NSMutableDictionary?) -> ()) { + private func fetchDictionaryFromiCloud(onComplete: (CKRecord?, NSMutableDictionary?) -> ()) { let recordId = CKRecordID(recordName: self.dictname) self.privateDB!.fetchRecordWithID(recordId, completionHandler: { - (record:CKRecord!, error:NSError!) -> () in + (record:CKRecord?, error:NSError?) -> () in if error != nil { self.debug("CDS: Status fetching cloudkit record \(error)") - switch error.code { + switch error!.code { case CKErrorCode.UnknownItem.rawValue: self.debug("CDS: Record did not exist, CK throws error for this, but is normal for new records, ignore and move on") onComplete(nil, nil) @@ -220,19 +223,24 @@ class CloudKitDictionarySyncer { onComplete(nil, nil) } - + + + } else { - if let obj:AnyObject = record.objectForKey("plistxml") { + if let obj:AnyObject = record!.objectForKey("plistxml") { var dict:NSMutableDictionary? if let str = obj as? String { if let data:NSData = str.dataUsingEncoding(NSUTF8StringEncoding) { var plisterror:NSError? - dict = NSPropertyListSerialization.propertyListWithData(data, - options:Int(NSPropertyListMutabilityOptions.MutableContainersAndLeaves.rawValue), - format: nil, error: &plisterror) as? NSMutableDictionary - if plisterror != nil { - self.debug("CDS: Error serializing cloudkit xml into dict. Error: \(plisterror)") - } + do{ + dict = try NSPropertyListSerialization.propertyListWithData(data, options:NSPropertyListMutabilityOptions.MutableContainersAndLeaves, + format: nil) as? NSMutableDictionary + }catch{ + if plisterror != nil { + self.debug("CDS: Error serializing cloudkit xml into dict. Error: \(plisterror)") + } + } + } } if dict != nil { diff --git a/CloudKitDictionarySyncer/Info.plist b/CloudKitDictionarySyncer/Info.plist index 6cb4ec0..6905cc6 100644 --- a/CloudKitDictionarySyncer/Info.plist +++ b/CloudKitDictionarySyncer/Info.plist @@ -7,7 +7,7 @@ CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier - se.baresi.$(PRODUCT_NAME:rfc1034identifier) + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName diff --git a/CloudKitDictionarySyncer/ViewController.swift b/CloudKitDictionarySyncer/ViewController.swift index 2a1cc7a..354c693 100644 --- a/CloudKitDictionarySyncer/ViewController.swift +++ b/CloudKitDictionarySyncer/ViewController.swift @@ -17,12 +17,12 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega override func viewDidLoad() { super.viewDidLoad() - self.syncer.loadDictionary(onComplete: { + self.syncer.loadDictionary({ loadResult in switch loadResult { case .Dict(let loadeddict): self.dict = loadeddict - println("EXAMPLE: Dict loaded dict = \(loadeddict)") + print("EXAMPLE: Dict loaded dict = \(loadeddict)") if let rowlabels = self.dict["rowlabels"] as? [String] { // Yes, we already got the rowlabel key, do nothing } else { @@ -31,13 +31,13 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega } case .Conflict(let localdict, let clouddict, let latest): // Handle conflict. In this example, we are merging all unique rowlabels from both dicts. - println("EXAMPLE: Conflict detected") + print("EXAMPLE: Conflict detected") var localrows = localdict["rowlabels"] as? [String] var cloudrows = clouddict["rowlabels"] as? [String] if localrows != nil && cloudrows != nil { - println("Both dicts have rowlabels array, will merge cloud array into local array") + print("Both dicts have rowlabels array, will merge cloud array into local array") for label in cloudrows! { - if !contains(localrows!, label) { + if localrows!.contains(label) { localrows!.append(label) } } @@ -46,7 +46,7 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega // The dict has changed, thanks to the merge, so we need to resave it self.syncer.saveDictionary(self.dict, onComplete: { status in - println("Resaved merged dict. Save status = \(status)") + print("Resaved merged dict. Save status = \(status)") }) } else if let rows = localrows { // We only have rows in localdict @@ -87,7 +87,7 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { - println("tapped row \(indexPath.row)") + print("tapped row \(indexPath.row)") let numrows = self.tableView(tableView, numberOfRowsInSection: 1) if indexPath.row == numrows - 1 { if var tablerows = self.dict["rowlabels"] as? [String] { @@ -101,7 +101,7 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega }) self.syncer.saveDictionary(self.dict, onComplete: { status in - println("Save status = \(status)") + print("Save status = \(status)") }) } @@ -119,7 +119,7 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega cell?.textLabel?.text = "Tap here to add row" } else { if let tablerows = self.dict["rowlabels"] as? [String] { - println("\(tablerows[indexPath.row])") + print("\(tablerows[indexPath.row])") cell?.textLabel?.text = tablerows[indexPath.row] } } diff --git a/CloudKitDictionarySyncerTests/Info.plist b/CloudKitDictionarySyncerTests/Info.plist index 7131db6..ba72822 100644 --- a/CloudKitDictionarySyncerTests/Info.plist +++ b/CloudKitDictionarySyncerTests/Info.plist @@ -7,7 +7,7 @@ CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier - se.baresi.$(PRODUCT_NAME:rfc1034identifier) + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName