2
votes

My data model

My data model

I generated to subclasses Pack and Class. In Pack.m I can see the following

#import "Pack.h"
#import "Card.h"

@implementation Pack
@dynamic packTitle;
@dynamic card;

- (void)addCardObject:(Card *)value {    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"card" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"card"] addObject:value];
    [self didChangeValueForKey:@"card" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)removeCardObject:(Card *)value {
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"card" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"card"] removeObject:value];
    [self didChangeValueForKey:@"card" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)addCard:(NSSet *)value {    
    [self willChangeValueForKey:@"card" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"card"] unionSet:value];
    [self didChangeValueForKey:@"card" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}

- (void)removeCard:(NSSet *)value {
    [self willChangeValueForKey:@"card" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"card"] minusSet:value];
    [self didChangeValueForKey:@"card" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
}


@end

All those methods were generated by xcode as not public. I tried to make the first one to be a public method and write to the database this way in my viewController where inputCardPack is array of arrays.

Pack* pack = [NSEntityDescription insertNewObjectForEntityForName:@"Pack" inManagedObjectContext:self.context];
pack.packTitle = self.inputCardPackTitle;


for (int i = 0; i< [self.inputCardPack count]; i++ ){

    NSNumber *level = [NSNumber numberWithInt:0];

    Card *card = [NSEntityDescription insertNewObjectForEntityForName:@"Card" inManagedObjectContext:self.context];

    card.term = [[self.inputCardPack objectAtIndex:i] objectAtIndex:0];

    card.def = [[self.inputCardPack objectAtIndex:i] objectAtIndex:1];

    card.level = level;
    //I'm trying to add a card to pack
    [pack addCardObject:card];

}

It does not work, I'm getting an error on the last line. If I remove the last line, it works. Questions: is it right approach to use those generated methods? How to get all cards belonging to defined Pack?

1
yes, it is recommended to use generated method by xcode. What error are you getting ? - Leonardo
I'm getting Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject addCardObject:]: unrecognized selector sent to instance 0x4f42f60' - Michael
The problem was with model. I deleted it before, named ii and afterwords renamed. Now I just made product -> clean and it works! The second question is still hanging on. - Michael

1 Answers

1
votes

Did you try to instantiate a new card object like this?

NSEntityDescription * description = [NSEntityDescription 
          entityForName:@"Card" inManagedObjectContext:self.context];

Card *card = [[Card alloc]initWithEntity:description 
          insertIntoManagedObjectContext:self.context];

Then once you have that temp card object, you can set values to it like so:

[card setValue: [[self.inputCardPack objectAtIndex:i] objectAtIndex:0]
      forKey: "term"];

You can go about this other ways to be sure, but because of that pesky little @dynamic identifer, the compiler takes issue with you not setting the values in the "CoreData" prescribed way. Basically, you are trying to set the value of a property that does not have space allocated for it yet because you did not @synthesize and alloc it.

I am not 100% on the inner workings of clang (the objective-c compiler) so if anyone sees problems with what I am saying, please let me know. This methodology just seems to work for me.

Hope this helps