Swift + CoreData: Can not set a Bool on NSManagedObject subclass - Bug?

前端 未结 6 1308
谎友^
谎友^ 2020-12-16 13:02

I have a little strange issue which I can\'t seem to figure out, I have a simple entity with a custom NSManagedObject subclass:

@objc(EntityTest) class Entit         


        
相关标签:
6条回答
  • 2020-12-16 13:03

    In XCode 8 Just set :

    user.isLoggedIn = true
    

    its works like a charm

    0 讨论(0)
  • 2020-12-16 13:03

    I haven't touched swift, but in Objective-C, a BOOL is not an object, and cannot be an object. It's a primitive, and it looks like you are attempting to tell an Objective-C class to treat a BOOL like an object. But I could be making that up, as I'm not familiar with what @NSManaged does under the hood.

    https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/FoundationTypesandCollections/FoundationTypesandCollections.html

    0 讨论(0)
  • 2020-12-16 13:08

    I found that it works fine if you specify the class name and module in the data model (instead of leaving the default NSManagedObject).

    Once I set that, I can use Bool, Int16, Int32, etc., without any problems.

    0 讨论(0)
  • 2020-12-16 13:19

    If you let Xcode 6 Beta 3 create the Swift files for your entities, it will create NSNumber properties for CoreDatas Boolean type.

    enter image description here

    You can however just use Bool as a Swift type instead of NSNumber, that worked for me without using the dot syntax though. It will set the Swift Bool with a NSNumber, that maybe leads to a bug in the dot syntax.


    To make it explicit you should use the type NSNumber for attributes in the entity with the Boolean type. Then create a computed property (in iBook The Swift programming language under Language Guide -> Properties -> Computed Properties) to return you a Swift Bool. So would never really store a Bool.

    Like so:

    @NSManaged var snack: NSNumber
    var isSnack: Bool {
        get {
            return Bool(snack)
        }
        set {
            snack = NSNumber(bool: newValue)
        }
    }
    

    Of course it would be cool to hide the other (NSNumber attribute), but be patient and Apple will implement private attributes in the future.


    Edit:

    If you check the box create skalar types it will even use the type Bool in the automatically created Swift file!

    So I think it is a bug.

    0 讨论(0)
  • 2020-12-16 13:20

    You can downcast your property from NSNumber to Bool type like this:

    var someBoolVariable = numberValue as Bool    
    

    It works for me in this way:

    self.twoFactorAuthEnabledSwitch.enabled = userProfile?.twoFactorEnabled as Bool
    
    0 讨论(0)
  • 2020-12-16 13:22

    Following on from CH Buckingham who is entirely correct. You are attempting to store a primitive type in core data where it is expecting an NSNumber.

    The correct usage would be entity.completed = NSNumber.numberWithBool(false)

    This is also why you cannot retrieve this completed value as a bool directly and thus you would need to write:

    var: Bool? = entity.completed.boolValue()
    
    0 讨论(0)
提交回复
热议问题