在我的应用程序中,用户现在可以在其图库中选择图像并将其保存在UIMAGEVIEW中,问题是当我将该图像保存在cloudkit中时,出现以下错误 “呼叫可能引发,但未标记为“ try”,并且未处理错误”
@IBAction func Save(_ sender: Any) {
let codig = code.text
let precio = price.text
let imagen = imageCover.image
let record = CKRecord(recordType: "Productos", zoneID: zona.zoneID)
record.setObject(codig as __CKRecordObjCValue?, forKey: "code")
record.setObject(precio as __CKRecordObjCValue?, forKey: "costo")
let mngr = FileManager.default
let dir = mngr.urls(for: .documentDirectory, in: .userDomainMask)
let file = dir[0].appendingPathComponent("myimage").path
imagen?.jpegData(compressionQuality: 0.5)?.write(to: file as! URL)
let imgURL = NSURL.fileURL(withPath: file)
let imageAsset = CKAsset(fileURL: imgURL)
record.setObject(imageAsset, forKey: "imagecover")
self.navigationItem.backBarButtonItem?.isEnabled = false
database.save(record) { (record, error) in
DispatchQueue.main.async {
self.navigationItem.backBarButtonItem?.isEnabled = true
if let error = error {
print("Error \(error.localizedDescription)")
} else {
print("Save")
self.navigationController?.popViewController(animated: true)
}
}
}
答案 0 :(得分:0)
您的问题是以下行:
imagen?.jpegData(compressionQuality: 0.5)?.write(to: file as! URL)
正如您在文档(https://developer.apple.com/documentation/foundation/data/1779858-write中所看到的那样,函数write(to:)
可能会引发错误。您需要通过忽略错误来处理错误
try? <function that throws error>
或通过使用do..catch
块来捕获它:
do {
try <function that throws error>
} catch {
// Handle error
}