错误:“线程 1:EXC_BAD_ACCESS(代码=EXC_I386_GPFLT)

Posted

技术标签:

【中文标题】错误:“线程 1:EXC_BAD_ACCESS(代码=EXC_I386_GPFLT)【英文标题】:error: "Thread 1: EXC_BAD_ACCESS(Code=EXC_I386_GPFLT) 【发布时间】:2016-08-20 12:31:41 【问题描述】:

我在这里需要帮助。我正在尝试使用核心数据来实现应用程序。但是在第 52 行 -

myHood.setMyHoodImg(addHoodImg.image!))

每次单击按钮创建新引擎盖时都会出错。错误:

“线程1:EXC_BAD_ACCESS(代码=EXC_I386_GPFLT)

代码如下:

import UIKit
import CoreData

class CreateHoodVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate 

@IBOutlet weak var hooddesc: UITextField!
@IBOutlet weak var addHoodImgBtn: UIButton!
@IBOutlet weak var addHoodImg: UIImageView!

var imagePicker: UIImagePickerController!

override func viewDidLoad() 
    super.viewDidLoad()

    imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    addHoodImg.layer.cornerRadius = 4.0
    addHoodImg.clipsToBounds  = true



func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) 

    imagePicker.dismissViewControllerAnimated(true, completion: nil)
    addHoodImg.image = image


@IBAction func addImage(sender: AnyObject!)

    presentViewController(imagePicker, animated: true, completion: nil)




@IBAction func createHood()

    if let hoodDescription = hooddesc.text where hoodDescription  != ""

        let app = UIApplication.sharedApplication().delegate as! AppDelegate
        let  context  = app.managedObjectContext
        let entity = NSEntityDescription.entityForName("MyHood", inManagedObjectContext: context)!
        let myHood = MyHood(entity: entity, insertIntoManagedObjectContext: context)

        myHood.myHoodDescription = hoodDescription
        myHood.setMyHoodImg(addHoodImg.image!)

        context.insertObject(myHood)

        do 

            try context.save()
         catch 
            print("Could not save new Hood")
        
    




【问题讨论】:

【参考方案1】:

其实很简单:

如果你写:

myHood.setMyHoodImg(addHoodImg.image!)

您假设 addHoodImg.image 确实存在,但在第一次运行时,如果您没有选择图像,则为 NIL。 所以有两种方法:

1) 简单地写:

if let hoodDescription = hooddesc.text where hoodDescription  != "" , let img = addHoodImg.image 

2) 开始时禁用按钮并在选择和图像后启用。

希望对您有所帮助。

ps 我在这里有一个功能齐全的原型,其中包含您使用的(减少的!)类。

【讨论】:

我已经实现了第一个选项。但我仍然收到错误请参阅此处的错误:”,2016-08-20 06:14:42.924 My-Hood[5076:250083] _BSMachError: (os/kern) invalid capability (20) 2016-08-20 06: 14:42.928 My-Hood[5076:250083] _BSMachError: (os/kern) invalid name (15)?

以上是关于错误:“线程 1:EXC_BAD_ACCESS(代码=EXC_I386_GPFLT)的主要内容,如果未能解决你的问题,请参考以下文章

Informix 错误:发生了语法错误。错误代码:-201

我收到一个错误:“MetaMask - RPC 错误:错误:错误:[ethjs-rpc] rpc 错误与有效负载”

错误精灵错误跟踪器错误

网页打开显示错误500是啥意思

PHP错误处理

PHP的错误处理