gpt4 book ai didi

swift - fatal error : unexpectedly found nil while unwrapping an Optional value when saving NSData to Realm

转载 作者:行者123 更新时间:2023-11-30 12:58:30 24 4
gpt4 key购买 nike

我想用 Realm 保存来自 imagePicker 的图像。当我选择一张图片时它有效,但当我不选择一张图片时它不起作用。我尝试在未选择图像时保存标准图像。但我收到错误: fatal error :在展开可选值时意外发现 nil地点:

class Location: Object{
dynamic var name = ""
dynamic var locationDescription = ""
dynamic var latitude = 0.0
dynamic var longitude = 0.0
dynamic var created = NSDate ()
dynamic var category: Category!
dynamic var imageData: NSData!
}

添加新的入口 Controller :

 //Image Picker
@IBOutlet var imageView: UIImageView!
let imagePicker = UIImagePickerController()
var selectedImage = UIImage()


@IBAction func loadImage(_ sender: AnyObject) {
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
present(imagePicker, animated: true, completion: nil)
}
//MARK: - Delegates Image Picker
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : Any])
{
selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

imageView.image = selectedImage

// Dismiss the picker.
self.dismiss(animated: true, completion: nil)

}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}

func addNewLocation() {
let realm = try! Realm()
//convert image to NSData
let standardData: NSData = UIImagePNGRepresentation(self.imageView.image!)! as NSData
let selctedImageData = NSData(data: UIImageJPEGRepresentation(selectedImage, 0.9)!)
let emptyData = NSData()
// let data = NSData(data: UIImageJPEGRepresentation(selectedImage, 0.9)!)
var data = NSData()

if selctedImageData == emptyData{
data = standardData
}
else{
data = selctedImageData
}

try! realm.write {
let newLocation = Location()


newLocation.name = self.nameTextField.text!
newLocation.category = self.selectedCategory
newLocation.locationDescription = self.descriptionTextField.text
newLocation.latitude = self.selectedAnnotation.coordinate.latitude
newLocation.longitude = self.selectedAnnotation.coordinate.longitude
newLocation.imageData = data


realm.add(newLocation)
self.locations = newLocation
}
}

编辑:我用查询生成的字符串替换了类别模型。

class Location: Object{
...
dynamic var category = ""
dynamic var imageData: NSData!

}

最佳答案

您的 Location 模型有两个隐式解包选项 (IUO) 属性:categoryimageData。当您知道该值可能必须存储 nil 时,Swift 中的 IUO 非常有用,但您可以保证它永远不会在该状态下被访问,而编译器无法自动暗示。

在这种情况下,您在将其中一个属性的值设置为 nil 以外的值之前先访问该属性。

也许self.selectedCategory在分配给newLocation.category属性时是nil?鉴于您的示例中没有包含与此值相关的任何内容,所以很难说。

如果您不能保证这些 IUO 在访问时会被设置为非零,我建议您将它们定义为可选值,并专门处理它们的零情况。

class Location: Object {
dynamic var name = ""
dynamic var locationDescription = ""
dynamic var latitude = 0.0
dynamic var longitude = 0.0
dynamic var created = NSDate()
dynamic var category: Category?
dynamic var imageData: NSData?
}

关于swift - fatal error : unexpectedly found nil while unwrapping an Optional value when saving NSData to Realm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40132737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com