我的NSManagedObject(UserInformations)属性出现了一些问题
在第二种方法中,我请求之前在方法中设置的userGender
的值。但为什么不userGender
保持他的价值?
UserInformations.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface UserInformations : NSManagedObject
...
@property (nonatomic) int32_t userGender;
UserInformations.m
@dynamic userGender;
这就是我的Viewcontroller:
GenderViewController.h
#import <UIKit/UIKit.h>
@class UserInformations;
...
@property (nonatomic, strong) UserInformations *item;
GenderViewController.m
...
@synthesize item;
...
- (IBAction)pickedFemale:(id)sender {
StoreThem *st = [[StoreThem alloc] init];
NSManagedObjectContext *context = [st managedObjectContext];
item = [NSEntityDescription insertNewObjectForEntityForName:@"UserInfos"
inManagedObjectContext:context];
NSNumber *someNumber = [NSNumber numberWithInt:3];
[item setValue:someNumber forKey:@"userGender"];
NSLog(@"%d", [item userGender]);
//NSLog says 3
}
- (IBAction)nextView:(id)sender {
NSLog(@"%d", [item userGender]);
//NSLogs says 0
if ([item userGender] == 0) {
return;
}
InfoViewController *mvc = [[InfoViewController alloc] init];
[[self navigationController] pushViewController:mvc animated:YES];
}
答案 0 :(得分:0)
我认为问题出在您的代码中。您使用了@dynamic userGender;将其替换为@synthesize userGender并使用(非原子)。请仔细阅读,以帮助您了解其中的差异。