本文介绍了尝试更改 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.
note
属性保存良好,但 completed
属性抛出 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
推荐答案
遇到同样的问题,解决方法确实是在@NSManaged属性中使用NSNumber.此外,您可以定义一个计算的 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 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!