I am trying to migrate an existing project from Swift 4 to Swift 4.2, and don´t know how to modify some piece of the code.
I have the following class to keep track of the different CKRecordZone names and their CKServerChangeTokens.
import Foundation
import CloudKit
class CloudKitFetchChangeToken{
private var dictionaryFetchChangeToken : [String : CKServerChangeToken]?{
get {
guard let defaults = UserDefaults(suiteName: "group.com.company.AppName") else { fatalError("Can not get UserDefaults for suiteName" ) }
guard let tokenData = defaults.dictionaryFetchChangeToken else { return nil }
// Using SWIFT 4
// Warning SWIFT 4.2: 'unarchiveObject(with:)' was deprecated in iOS 12.0: Use +unarchivedObjectOfClass:fromData:error: instead
return NSKeyedUnarchiver.unarchiveObject(with: tokenData) as? [String : CKServerChangeToken]
}
set {
guard let newValue = newValue else {
guard let defaults = UserDefaults(suiteName: "group.com.company.AppName") else { fatalError("Can not get UserDefaults for suiteName" ) }
defaults.dictionaryFetchChangeToken = nil
return
}
// Using SWIFT 4
//let data = NSKeyedArchiver.archivedData(withRootObject: newValue)
// Using SWIFT 4.2
let coder = NSKeyedArchiver(requiringSecureCoding: true)
coder.encode(newValue, forKey: NSKeyedArchiveRootObjectKey)
let data = coder.encodedData
guard let defaults = UserDefaults(suiteName: "group.com.company.AppName") else { fatalError("Can not get UserDefaults for suiteName" ) }
defaults.dictionaryFetchChangeToken = data
}
}
private let queue = DispatchQueue(label: "com.AppName.CloudKitFetchChangeToken.queue")
public subscript(key: String) -> CKServerChangeToken? {
get {
return queue.sync {
return dictionaryFetchChangeToken?[key]
}
}
set(newValue) {
queue.sync {
dictionaryFetchChangeToken?[key] = newValue
}
}
}
var isEmpty: Bool {
return dictionaryFetchChangeToken?.count == 0
}
var existStorage : Bool{
return dictionaryFetchChangeToken != nil
}
func createSorage(){
dictionaryFetchChangeToken = [:]
}
}
My problem is with var dictionaryFetchChangeToken, I think I managed ok the change of the sintax from Swift 4 to Swift 4.2 in the set (), but
I don´t know how to do it in the get() part of the code.
Warning SWIFT 4.2: 'unarchiveObject(with:)' was deprecated in iOS 12.0: Use +unarchivedObjectOfClass:fromData:error: instead.
In Swift Dictionary is a Generic Structure, and the previous method is waiting for a class.