本文介绍了Swift提供错误线程1:访问NSManagedObject的属性时,EXC_BAD_ACCESS(代码= 1,地址= 0x0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建NSManagedObject子类对象的函数是:

  func loginMoldelWithName(name:NSString,atIndexPath indexPath:NSIndexPath) ; LoginModel {

let entity = NSEntityDescription.entityForName(LoginModel,inManagedObjectContext:managedObjectContext!)

var login = LoginModel(entity:entity !, insertIntoManagedObjectContext:managedObjectContext)

login.name = name
login.date = NSDate()
login.rowIndex = indexPath.row as NSNumber

var error:NSError?
if!managedObjectContext!.save(& error){
println(\(TAG)Could not save \(error),\(error?.userInfo))
}

返回登录为LoginModel

}



而要删除的那个;

  func deleteLoginModel(loginModel:LoginModel){
println(\(TAG)\(loginModel.name))
managedObjectContext?.deleteObject(loginModel)

var error:NSError?
if(managedObjectContext!.save(& error)){
println(\(TAG)\(error?.localizedDescription))
}
}

问题出现在



<$> p $ p> let loginModel = loginModels [indexPath.row] as LoginModel
println(\(TAG)\(loginModel.name))
// remove from核心数据
deleteLoginModel(loginModel)
...

删除loginModel对象后当我尝试访问loginModel.name,我得到错误线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)



写入无效的表达式对于变量视​​图中的loginModel,请参阅下面的snaphsot ...



我找不到我做错了什么以及loginModel实例可能发生了什么? p>

>

解决方案

从您附加的屏幕截图,它看起来像是错误发生,因为您正在尝试访问之后该对象已被删除。此时,对象的属性将不再是有效的内存地址。


My function to create NSManagedObject's subclass object is;

func loginMoldelWithName(name: NSString, atIndexPath indexPath: NSIndexPath) -> LoginModel {

    let entity = NSEntityDescription.entityForName("LoginModel", inManagedObjectContext: managedObjectContext!)

    var login = LoginModel(entity: entity!, insertIntoManagedObjectContext: managedObjectContext)

    login.name = name
    login.date = NSDate()
    login.rowIndex = indexPath.row as NSNumber

    var error: NSError?
    if !managedObjectContext!.save(&error) {
        println("\(TAG) Could not save \(error), \(error?.userInfo)")
    }

    return login as LoginModel 

}

And the one to delete;

func deleteLoginModel(loginModel: LoginModel) {
    println("\(TAG)  \(loginModel.name)")
    managedObjectContext?.deleteObject(loginModel)

    var error : NSError?
    if(managedObjectContext!.save(&error) ) {
        println("\(TAG) \(error?.localizedDescription)")
    }
}

And where the issue occurs is at;

let loginModel = loginModels[indexPath.row] as LoginModel
println("\(TAG) \(loginModel.name)")
// remove from Core Data
deleteLoginModel(loginModel)
...

After removing loginModel object when i try to access the loginModel.name, i'm getting the error "Thread 1: EXC_BAD_ACCESS(code=1, address=0x0)"

Invalid expression is written for loginModel in the variables view, see the snaphsot below...

I can't find where i did wrong and what could have happened to the loginModel instance ?

解决方案

From the screenshot you attached, it looks like the error is occurring because you are trying to access the property of an object after that object has been deleted. At that point, the properties of the object will no longer be valid memory addresses.

这篇关于Swift提供错误线程1:访问NSManagedObject的属性时,EXC_BAD_ACCESS(代码= 1,地址= 0x0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 00:34