mardi 4 août 2015

accessing the setMethod of a @property in ios

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

Aucun commentaire:

Enregistrer un commentaire