致命错误:在将NSData保存到Realm时解除可选值时意外发现nil

时间:2016-10-19 13:25:25

标签: swift realm nsdata

我想从带有Realm的imagePicker中保存图像。它在我选择图片时有效,但在我不选择图片时则无效。我没有选择图像时试图保存标准图像。 但我收到错误:致命错误:在展开Optional值时意外发现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!
}

添加新条目控制器:

 //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
    }
}

编辑: 我用一个从查询生成的字符串替换了Category Model。

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

}

2 个答案:

答案 0 :(得分:0)

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

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

self.selectedCategorynil在分配给newLocation.category属性的位置可能是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? } ?鉴于您未在样本中包含与此值相关的任何内容,因此很难说。

如果您无法保证在访问时将这些IUO设置为非零值,我建议您将它们定义为可选项,并专门处理它们的nil案例。

col

答案 1 :(得分:0)

我解决了这个问题:

class Location: Object{
dynamic var name = ""
dynamic var locationDescription = ""
dynamic var latitude = 0.0
dynamic var longitude = 0.0
dynamic var category = ""
dynamic var imageData: NSData? = nil

}


 //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 imageData: NSData = UIImagePNGRepresentation(self.imageView.image!)! as NSData






    let realm = try! Realm()

    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 = imageData

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