为什么对象不保留其价值?

时间:2014-07-21 11:07:32

标签: ios objective-c properties

我的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];


}

1 个答案:

答案 0 :(得分:0)

我认为问题出在您的代码中。您使用了@dynamic userGender;将其替换为@synthesize userGender并使用(非原子)。请仔细阅读,以帮助您了解其中的差异。

@synthesize vs @dynamic, what are the differences?