Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a generated Core Data NSManagedObject in Swift

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

like image 519
TooManyEduardos Avatar asked Mar 26 '15 16:03

TooManyEduardos


Video Answer


2 Answers

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.

like image 58
Tom Harrington Avatar answered Oct 14 '22 00:10

Tom Harrington


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.

like image 42
gregheo Avatar answered Oct 13 '22 22:10

gregheo