【发布时间】:2017-01-28 03:31:34
【问题描述】:
我正在使用 CloudKit 来保存用户输入的记录,但是我的应用程序崩溃了。
下面是我的代码:
func saveRecordToCloud(_ pinpoint:Details!) -> Void {
// Prepare the record to save
let record = CKRecord(recordType: "Details")
record.setValue(pinpoint.title, forKey: "title")
record.setValue(pinpoint.location, forKey: "location")
record.setValue(pinpoint.date, forKey: "date")
// Resize the image
let originalImage = UIImage(data: pinpoint.image as Data)!
let scalingFactor = (originalImage.size.width > 1024) ? 1024 / originalImage.size.width : 1.0
let scaledImage = UIImage(data: pinpoint.image as Data, scale: scalingFactor)!
// Write the image to local file for temporary use
let imageFilePath = NSTemporaryDirectory() + pinpoint.title
try? UIImageJPEGRepresentation(scaledImage, 0.8)?.write(to: URL(fileURLWithPath: imageFilePath), options: [.atomic])
// Create image asset for upload
let imageFileURL = URL(fileURLWithPath: imageFilePath)
let imageAsset = CKAsset(fileURL: imageFileURL)
record.setValue(imageAsset, forKey: "image")
// Get the Public iCloud Database
let publicDatabase = CKContainer.default().publicCloudDatabase
// Save the record to iCloud
publicDatabase.save(record, completionHandler: { (record:CKRecord?, error:NSError?) -> Void in
// Remove temp file
do {
try FileManager.default.removeItem(atPath: imageFilePath)
} catch {
print("Failed to save record to the cloud: \(error)")
}
} as! (CKRecord?, Error?) -> Void)
}
我收到的错误是:
线程 1:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)
这是} as! (CKRecord?, Error?) -> Void) 行,倒数第二行。
我正在使用 Swift 3.0
【问题讨论】:
-
哪一行导致崩溃?
-
@Pierce 最后一行写着
} as! (CKRecord?, Error?) -> Void)。我会将这个添加到问题中。 -
我知道它是什么 -> 我会发布答案。将您的项目从旧版本的 Swift 转换为 Swift 3 后是否发生这种情况?
-
@Pierce 好的,谢谢。不,我没有转换它。