IOS 5 only, with ARC. In my Core Data model class:
// Planet.h //
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Planet : NSManagedObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSNumber *diameter_km;
@property (nonatomic, retain) NSNumber *mass_kg;
-(void) setVisited:(BOOL)flag;
-(BOOL) isVisited;
@end
// Planet.m //
//#import "Planet.h"
@implementation Planet
@dynamic name;
@dynamic diameter_km;
@dynamic mass_kg;
BOOL visitedByHumans; // not a core data entity; just an ivar
-(void)setVisited:(BOOL)flag {
visitedByHumans = flag;
}
-(BOOL)isVisited {
return visitedByHumans;
}
@end
I use MagicalRecord to create "Venus" and "Mars". In my view controller, I use labels and buttons to test the above. Testing shows that when I "visit" Mars, Venus also becomes visited. If I switch the ivar visitedByHumans into a non-Core-Data property, it works as expected. So I'm no longer 'stuck', but I want to understand the ivar thing.
vistedByHumans
is not actually an ivar, but a global variable of your subclass Planet
. So, any and every "planet" instance will appear to be visited regardless of which instance is actually visited. If you want to make it an actual ivar, you need to add a @property
to your @interface
much like name
, diameter_km
, and mass_kg
(although, those three of course were generated for your models). e.g.:
@property (nonatomic,assign,getter=isVisited) BOOL visited;
and in your implementation:
@synthesize visited=visitedByHumans;
or just
@synthensize visited;
Since you appear to be using those methods (visited
and setVisited:
) anyhow, and not really directly accessing visitedByHumans
.
Also, be sure to remove the line of code
BOOL visitedByHumans;
and the two method definitions isVisited
and setVisited:
. They will be generated for you when you @synthesize
'd them.