Python Learner
Python Learner

Reputation: 185

Swift : Core Data Integer weird values

Just started learning Swift and have a question around Core Data. I have a very simple entity named "Score" with one attribute "points" which is of type Int16.

Here's my data model class:

class Score: NSManagedObject {
    @NSManaged var points: Int}

Here's the code on my View Controller:

    override func viewDidLoad() {
        super.viewDidLoad()

        let entity = NSEntityDescription.entityForName("Score", inManagedObjectContext: managedObjectContext!)
        let score = Score(entity: entity!, insertIntoManagedObjectContext: managedObjectContext)

        println(score.points)
}

What prints in the console is this number: -5764607523034234879

I don't understand what I'm doing wrong. I looked elsewhere and perhaps I need to do something with NSNumber, but I'm unfamiliar with that class.

Shouldn't score.points be empty right now?

Upvotes: 1

Views: 1400

Answers (1)

skim
skim

Reputation: 2327

It looks like you've got your questions answered, but you CAN set a default value for attributes in NSManagedObject. Just not via code.

enter image description here

The default value is set when you save without setting a value, so you still need to deal with the uninitialized pointer reference to a NSNumber value until it is saved.

Upvotes: 1

Related Questions