0

I got some problems with my properties of NSManagedObject(UserInformations)

In the second method I request the value of the userGender which I set in the method before. But why doesn't userGender keep his value?

UserInformations.h

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface UserInformations : NSManagedObject
...
@property (nonatomic) int32_t userGender;

UserInformations.m

@dynamic userGender;

Thats my 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];


}
Limo
  • 1
  • 2
  • Have you checked if `item != nil`? – trojanfoe Jul 21 '14 at 11:48
  • Just checked it: if (item != nil) { NSLog(@"yolo"); } Gives out yolo – Limo Jul 21 '14 at 11:56
  • Your code seems a bit unclear to me. Why do you use key value coding? Just do `item.userGender = 3;`. Also, your `UserInformation` model expects an `int`. What you are passing in is an `NSNumber`. And you should save your `NSManagedObjectContext`. – Marc Jul 21 '14 at 12:39
  • Thats what i wrote before but then I tried to do it with NSNumber. So it doesn't change anything – Limo Jul 21 '14 at 12:42
  • Can you upload your source somewhere? Github? – Marc Jul 21 '14 at 12:46

1 Answers1

0

I think problem is in your code.You have used @dynamic userGender;replace it with @synthesize userGender and use (nonatomic). Please go through that will help you to understand the difference.

@synthesize vs @dynamic, what are the differences?

Community
  • 1
  • 1
Mohit
  • 516
  • 7
  • 24
  • its a integer i can't use strong – Limo Jul 21 '14 at 11:57
  • use @synthesize in place of dynamic. – Mohit Jul 21 '14 at 11:59
  • if(!item) { item = [[UserInformations alloc]init]; } item = [NSEntityDescription insertNewObjectForEntityForName:@"UserInfos" inManagedObjectContext:context]; – Mohit Jul 21 '14 at 13:22
  • Why should it? What's the difference? – Marc Jul 21 '14 at 13:28
  • I thought its no possible to use init with a NSManagedObject. I think you could only use `initWithEntity:insertIntoManagedObjectContext:` and thats what i did with NSEntityDescription. Your code gives an error: `CoreData: error: Failed to call designated initializer on NSManagedObject class 'UserInformations'` – Limo Jul 21 '14 at 14:18
  • Ok i solved my problem by choosing a strong reference for `@property (nonatomic, strong) NSManagedObjectContext *context;` Thanks guys! – Limo Jul 21 '14 at 15:15