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 subclassPlanet
. 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 likename
,diameter_km
, andmass_kg
(although, those three of course were generated for your models). e.g.:and in your implementation:
or just
Since you appear to be using those methods (
visited
andsetVisited:
) anyhow, and not really directly accessingvisitedByHumans
.Also, be sure to remove the line of code
and the two method definitions
isVisited
andsetVisited:
. They will be generated for you when you@synthesize
'd them.