1

after many hour of tutorials and googling i knew that when we define a property inside the header file the compiler automatically generate the setters and getters here is an example:
"example.h"

@property(strong,nonatomic) NSNumber *test;

example.m

[self.test setTest [@"3"]];

so what i want is to access this method to check the method declaration and here is the reason that i want

Full Scenario

@interface exampleController ()  
{

    int example;
}

// here is a instance variable used to save a value while the app running i am getting this value from a dictionary and then save the value of the example variable to the core data

here is the entity class

exampleEntity.h

 @property(strong,nonatomic) NSNumber *example;

exampleEntity.m

@dynamic example;

// since the instance variable is int we need to cast it to NSNumber so i follow this link

[entry setExample : [NSNumber numberWithInt:example]];

Finally after running this code i go into this exception:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException'

Full Code

    #import "viewController.h"
    @interface viewController ()  
   {

         int example;
    }


- (void)viewDidLoad {
gettingTheExampleValueFromDictionary();
}



-(void)gettingTheExampleValueFromDictionary()


 {

                NSDictionary *dictionary = [NSJSONSerialization    JSONObjectWithData:requestHandler options:0 error:&jsonParsingError];
       example = [[dictionary objectForKey:@"exampleId"] intValue];


 }



- (IBAction)doneButton:(id)sender {

  [self insertDataToCoreData];
  [self dismissSelf];
}

      -(void)insertDataToCoreData   {

   CoreData *coreDataStack = [CoreData defaultStack];
        ExampleEntity *entry = [NSEntityDescription insertNewObjectForEntityForName:
                                    @"ExampleEntity" inManagedObjectContext:coreDataStack.managedObjectContext];

            [entry setExample :[NSNumber numberWithInt:example]];
// here we go into the error //



    [coreDataStack saveContext];
    }





  @end
// and if you ask me for entity class here is:

 #import <Foundation/Foundation.h>
 @interface ReservationEntity : NSObject
 @property (nonatomic,strong) NSNumber *example;
   @end



 #import "exampleEntity.h"
 @implementation exampleEntity
 @dynamic example;
 @end
 // and if you ask me for core data here is the pic needed //

core data

   // the configuration of xcdatamodel

configuration of xcdatamodel

Community
  • 1
  • 1
Zakaria Darwish
  • 358
  • 5
  • 22
  • 1
    Could you please paste the full example code? – Sulthan Aug 03 '15 at 14:00
  • example = [[dictionary objectForKey:@"id"]intValue]; [entry setExample [ NSNumber numberWithInt:example]; – Zakaria Darwish Aug 03 '15 at 14:20
  • That's really not full code. Also, please, to add information, edit the question. – Sulthan Aug 03 '15 at 14:29
  • 1) It's not Xcode but the compiler that generates the setters and getters. Xcode is just an IDE. 2) `example` is not a global variable. It's an instance variable for the class. Huge difference. 3) Update your question with the complete error and point out which line of code causes the error. – rmaddy Aug 03 '15 at 15:12
  • @maddy i just updated my answer check it up – Zakaria Darwish Aug 04 '15 at 06:50
  • Your have your `ReservationEntity` as a subclass of `NSObject`. Try changing it to a subclass of `NSManagedObject`, and then change `@synthesize example;` to `@dynamic example;` and tell me what happens. – cjwirth Aug 04 '15 at 07:21
  • @cjwirth still having the same error – Zakaria Darwish Aug 04 '15 at 07:31
  • OK, usually it will give you a "reason" after "NSInvalidArgumentException", what does it say? – cjwirth Aug 04 '15 at 07:40
  • @cjwirth -[NSManagedObject setExample:]: unrecognized selector sent to instance 0x7faf0d132400 – Zakaria Darwish Aug 04 '15 at 08:02
  • The problem is that you're not getting the right kind of object. Something is definitely wrong. Look when you are creating the new entry, you are asking for `ExampleEntity` yet your actual class is called `ReservationEntity`. With this, I can't guarantee that there aren't _other_ places that are screwed up too. – cjwirth Aug 04 '15 at 09:23
  • @cjwirth no i already implement here a general code for my special code ... – Zakaria Darwish Aug 04 '15 at 11:07
  • because i could not post the code due to some organisation policy ... – Zakaria Darwish Aug 04 '15 at 11:10
  • Show me more of what's in the `xcdatamodel` file -- mainly when you are in the `Default` configuration, not the `ExampleEntity`. Do you have the class set correctly? – cjwirth Aug 04 '15 at 11:17
  • @cjwirth check my answer there is a pic for xcdatamodel – Zakaria Darwish Aug 04 '15 at 11:27
  • Yeah I know, that's why I said "show me _more_". http://i.imgur.com/qe9RdHo.png <-- that is what I want you to click and show me what the screen looks like when you do – cjwirth Aug 04 '15 at 12:43
  • @cjwirth i just updated my answer check it up – Zakaria Darwish Aug 04 '15 at 12:50
  • I'm pretty sure that's your problem -- your `ExampleEntity` has no `Class` assigned to it. See it in the middle pane there? You should set it to be the name of the class you want to use (either `ExampleEntity` or `ReservationEntity`, depending on your project, i'm guessing) – cjwirth Aug 04 '15 at 13:39
  • no because i just inserted a string value inside the core data and my code run smoothly so does core data does not accept **NSNumber** thats impossible right ??? and one more question does the size of integer inside the xcdatamodel file could make this issue because i tried to make it 32 and 64 and the same error persist and even i just enter hard coded value and i got the same error ... – Zakaria Darwish Aug 05 '15 at 05:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/85208/discussion-between-zakaria-darwish-and-cjwirth). – Zakaria Darwish Aug 05 '15 at 14:03

1 Answers1

2

In your case you should change:

@dynamic example;

to

@synthesize example;

or just delete the line of code @dynamic example.

Because @synthesize generates the getter&setter for the property, while @dynamic means the getter&setter are created in runtime(e.g. NSManagedObject's subclasses)

Kevin
  • 1,147
  • 11
  • 21
  • 1
    `@dynamic` actually means "don't synthesize". The getter & setter don't have to be generated at runtime. They can just be invisible in the current compilation unit. – Sulthan Aug 03 '15 at 13:58
  • @kevin still cannot access the setter method and the error still the same any other solutions? – Zakaria Darwish Aug 03 '15 at 13:59
  • @ZakariaDarwish what's the type of example you pass to numberWithInt:? – Kevin Aug 03 '15 at 14:04
  • @Kevin is an int for sure – Zakaria Darwish Aug 03 '15 at 14:05
  • @ZakariaDarwish as I saw from you comment in the question, the "example" is get from a dictionary, that means "example" is already a NSNumber. So just pass it to setExample: – Kevin Aug 03 '15 at 14:17
  • @Kevin as i mention in my question that is an int value and when i get it from the dictionary i simply cast it to int using intValue method let me update my comment – Zakaria Darwish Aug 03 '15 at 14:19