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.

其他提示

It's impossible to be sure based on the information you've presented. Your description doesn't match the code-- despite what you say, visitedByHumans is most definitely not an instance variable in that code. This then makes me wonder what the code looked like back before you switched it away from using Core Data for visitedByHumans. Basically, you explain that the code wasn't working right when you were using Core Data, but then present entirely different code that doesn't actually use Core Data. I don't know what you were doing when you were trying to use Core Data for this property so I can't tell what you might have been doing wrong. If that declaration of visitedByHumans was in the code at that time, it was almost certainly screwing things up for you. Likewise, if you had setters in a managed object subclass that didn't call super's implementation, you'd get bad results. But if you want to know why your Core Data code wasn't working, paste that code, not some completely different code. Don't make people guess what you're up to when you ask for help.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top