我正在向现有数据集添加新属性。如果我从 Parse.com 后端获得的该属性的值不存在,我需要采取纠正措施(例如将其设置为默认值)。我已经查看了此处发布的各种解决方案,但它们不起作用,我已将 Xcode 错误消息作为注释放在下面的代码中。我在 Xcode 6.4 和 Xcode 7.0 beta 5 中都尝试过,因为另一篇文章建议在 Xcode 7.0 Beta 2 中正确处理可选的 NSManaged。
class MyObject: PFObject, PFSubclassing {
@NSManaged private(set) var newPoperty: Bool // No probs here but cannot check nil, see later on..
// @NSManaged private(set) var newProperty: Bool? // ! Property cannot be marked NSManaged as its type cannot be represented in Objective-C
private func initLaterProperties(object: MyObject) {
if (object.newProperty == nil ) {//Does not work with following messages
// Xcode 7.0 Beta 5: ! Binary operator '==' cannot be applied to operands of type Bool and nil
// Xcode 6.4: No operator found matching the operands
// Do something
}
if let test = object.newProperty { // Does not work with following message
//! Initializer for conditional binding must have Optional type, not 'Bool'
}
else {
//Do something
}
}
}
最佳答案
因为这必须是一个与 Objective-C 兼容的属性,所以如果您在 Objective-C 中编写这段代码,您会做些什么!在 Objective-C 中,Bool 不能是 nil
- 它不是对象。相反,您将使用 NSNumber,它具有保存 bool 值 (init(bool:)
) 和提取 bool 值 (boolValue
) 的方法。因此,将其设为 NSNumber?
并相应地调整其余代码。
关于ios - 如何快速检查@NSManaged 属性是否为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31867662/