How to initialize a generated Core Data NSManagedObject in Swift

15,265

Solution 1

Your have this:

var card : Card

That declares that card is of type Card but does not create an instance. You need to allocate and initialize an instance before using it. The initialization rule here is the same as in Objective-C, i.e. that you must call a designated initializer on the object. You'd do something like this:

var card = NSEntityDescription.insertNewObjectForEntityForName("Card", inManagedObjectContext: self.managedObjectContext) as! Card

For what it's worth, your Objective-C snippet is incorrect, because init is not a designated initializer for NSManagedObject.

Solution 2

You declare the variable card but never initialize it.

You'll need the Core Data context and then you can create an instance:

let entity = NSEntityDescription.entityForName("Card", inManagedObjectContext: managedObjectContext)
let card = Card(entity: entity!, insertIntoManagedObjectContext: managedObjectContext)

That'll set up an empty Card instance in the context. When you save the context, your new card will be there in the store.

Share:
15,265
TooManyEduardos
Author by

TooManyEduardos

Updated on July 08, 2022

Comments

  • TooManyEduardos
    TooManyEduardos almost 2 years

    I know obj-c but I'm learning swift.

    In obj-c when using core data you model your data and tell xcode to generate a nsmanageobject subclass of your model. Then in code you initialize it as

    #import MyObject
    - (void) someMethod
    {
        MyObject *my = (Card *) [NSEntityDescription insertNewObjectForEntityForName:@"Card" inManagedObjectContext:[self managedObjectContext]];
    
        my.name = @"some name";
    }
    

    In swift I'm trying to do the same thing but I can't seem to figure out how to initialize my custom object. This is what I have:

    The generated NSManagedObject subclass:

    import Foundation import CoreData

    class Card: NSManagedObject
    {
        @NSManaged var card_name: String
        @NSManaged var card_id: String
        @NSManaged var card_issuer: String
        @NSManaged var card_type: String
    }
    

    Then I'm trying to use it on another class, like this:

    var card : Card
    card.card_name = "Some Name"
    card.card_issuer = "HOA"
    card.card_type = "Rec Center"
    card.card_id =  "123"
    

    But the error I get is:

    Variable 'card' used before being initialized

    I'm obviously missing a step but I can't point to what it is.

    Also, as mention by mentioned by several iOS instructors you shouldn't mess with the generated NSManagedObject subclass.

    Any suggestions?

    Edit I get an error now of: (should probably be a new SO question...)

    CoreData: warning: Unable to load class named 'Card' for entity 'Card'. Class not found, using default NSManagedObject instead.

    Here are the screenshots to show how this Class exists in the build phase and the entity name has been set in the xcdatamodeld file enter image description here

    enter image description here Thanks

  • TooManyEduardos
    TooManyEduardos about 9 years
    1. I fixed the obj-c initialization. 2. This is approach still uses KVC though. I dont want to do "card.setValue("HOA", forKey: "card_issuer")". I want to do "card.card_issuer = "HOA". Is this possible in swift?
  • Srivathsalachary Vangeepuram
    Srivathsalachary Vangeepuram about 9 years
    I don't know why you think this requires KVC. Assuming that your model is configured correctly, this will create an instance of Card, and you can call any methods that exist on `Card.
  • TooManyEduardos
    TooManyEduardos about 9 years
    Sorry I just realized you had the "as Card" at the end and you're using the EntityDescription directly instead of a 2 step I used on obj-c. Let me see how this works. Thanks!
  • TooManyEduardos
    TooManyEduardos about 9 years
    Weird, I keep getting a "CoreData: warning: Unable to load class named 'Card' for entity 'Card'. Class not found, using default NSManagedObject instead."
  • Srivathsalachary Vangeepuram
    Srivathsalachary Vangeepuram about 9 years
    That sounds like class Card is not included in the target that you're building.
  • TooManyEduardos
    TooManyEduardos about 9 years
    I just added screenshots to show its in build phases. By the way, thanks for your help
  • TooManyEduardos
    TooManyEduardos about 9 years
    Screw it. Now I have interface builder bugs and I can't compile some of the code until I restart Xcode. I'm done with swift for a while. Thanks your help, but I'm back to obj-c again