9月 172019
 

ios 12后 swift中一些方法被废弃掉了,代以其它的方法,有些xcode提示直接使用fix就完成,有些变化比较大,收集了下自己碰到的,举几个例子:

1、***NSKeyedArchiver.archiveRootObject***
老方法:
NSKeyedArchiver.archiveRootObject(allItems, toFile: itemArchiveURL.path)

新方法:
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: allItems, requiringSecureCoding: false)
try data.write(to: itemArchiveURL)
} catch {
print(error)
}

2、***NSKeyedUnarchiver.unarchiveObject***
老方法:
if let archivedItems = NSKeyedUnarchiver.unarchiveObject(withFile: itemArchiveURL.path) as? [Item] {
allItems = archivedItems
}
新方法:
do {
let data = try Data(contentsOf: itemArchiveURL)
if let archivedItems = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? [Item] {
allItems = archivedItems
}
} catch {
print(“unarchive failure in init”)
}

3、**UIImageJPEGRepresentation**
老方法:
let imageData = UIImageJPEGRepresentation(image, 0.75)

新方法:
let imageData = image.jpegData(compressionQuality: 0.75)
类似的, UIImagePNGRepresentation 替换成 pngData().

Sorry, the comment form is closed at this time.