尝试更改Bool属性时发生EXC_BAD_ACCESS错误 [英] EXC_BAD_ACCESS error when trying to change Bool property

查看:138
本文介绍了尝试更改Bool属性时发生EXC_BAD_ACCESS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改Bool属性,并收到一个EXC_BAD_ACCESS错误。

I'm trying to change a Bool property and am receiving an EXC_BAD_ACCESS error.

我使用XCode 6和Swift。

I'm using XCode 6 and Swift.

注释属性保存正常,但完成属性引发 EXC_BAD_ACCESS 错误

The note property saves fine but the completed property throws the EXC_BAD_ACCESS error

import Foundation
import CoreData

class Task: NSManagedObject
{

    @NSManaged var note: String!
    @NSManaged var completed: Bool

}

属性例程

    // taskObject is an instance of Task()

    // Set the completed flag
    taskObject.completed = true // EXC_BAD_ACCESS


推荐答案

p>有同样的问题,解决方案的确是使用NSNumber在@NSManaged属性。此外,您可以定义计算Bool属性,以便您可以在业务逻辑中使用标量布尔值,而不使用NSNumber。

Had the same problem, the solution is indeed to use NSNumber in the @NSManaged property. Additionally you could define a computed Bool property so that you can work with the scalar Boolean in your business logic and not with the NSNumber.

var isCompleted: Bool {
get {
    return completed == NSNumber(bool: true)
}
set {
    completed = NSNumber(bool: newValue)
}
}

这篇关于尝试更改Bool属性时发生EXC_BAD_ACCESS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆