after checking all the answers of "Unrecognised selector sent" questions such as unrecognized selector sent to instance, and Unrecognized selector sent to instance? did not satisfy my situation so for that here is my full scenario:
Description:
in my app there exist a settings View which contain a UISwitch either to add all his reservation to the phone calendar or not.
so I need to save the choice of the user inside CoreData to add reservations to calendar or not i.e the state of the UISwitch.
initially when the app start for the first time the UISwitch will be ON and his state will be saved inside the CoreData with fixed ID 1 because I don`t want to add many Objects I need to keep only one object and when the user change the value of the UISwitch the app should update the object with new state I try this solution How to update existing object in Core Data? also I got an error
Full Code:
SwitchStateEntity.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface switchState : NSManagedObject
@property(strong,nonatomic) NSNumber *switchId;
@property (nonatomic,strong) NSNumber *switchState;
@property (nonatomic,strong) NSNumber *showReminderAlert;
@end
SwitchStateEntity.m
#import "switchState.h"
@implementation switchState
@dynamic switchId;
@dynamic switchState;
@dynamic showReminderAlert;
@end
SettingsViewController.h
#import <UIKit/UIKit.h>
#import "MakeReservationViewController.h"
@interface SettingsViewController : UIViewController
@property (weak, nonatomic) IBOutlet UISwitch *isAdedToCalendarOrNot;
@property (nonatomic) Boolean isSwitchOnOrOf;
@property (nonatomic,strong) NSString *savedEventId;
@end
SettingsViewController.m
- (IBAction)DoneButtonPressed:(id)sender {
// check the state of the switch //
// save this state //
NSError *error;
CoreData *coreDataStack = [CoreData defaultStack];
NSArray *fetchedObjects;
NSFetchRequest *fetchRequest;
NSEntityDescription *entity;
// setting up the variable needed //
fetchRequest = [[NSFetchRequest alloc] init];
entity = [NSEntityDescription entityForName:@"SwitchState" inManagedObjectContext:coreDataStack.managedObjectContext];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"remindMeSwitchId== %d", 1]];
[fetchRequest setEntity:entity];
fetchedObjects = [coreDataStack.managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSLog(@"%@",fetchedObjects);
for( NSEntityDescription *name in fetchedObjects)
{
NSLog(@"Switch Id is: %@",[name valueForKey:@"remindMeSwitchId"]);
NSLog(@"Switch State is: %@",[name valueForKey:@"remindMeSwitchState"]);
SwitchStateEntity *h = [ fetchedObjects firstObject];
[h setSwitchState:[NSNumber numberWithBool:self.isSwitchOnOrOf]];
// after this statement i go into Unrecognised selector had been sent//
[coreDataStack saveContext];
NSLog(@"Switch Id is: %@",[name valueForKey:@"remindMeSwitchId"]);
NSLog(@"Switch State is: %@",[name valueForKey:@"remindMeSwitchState"]);
}
[self dismissSelf];
}
- (IBAction)isAdedToCalendarValueChanged:(id)sender {
if ([self.isAdedToCalendarOrNot isOn]) {
self.isSwitchOnOrOf = YES;
}
else
{
self.isSwitchOnOrOf = NO;
}
}
and for the exception that the app goes through:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject setSwitchState:]: unrecognised selector sent to instance 0x7fb6f3411d20'
and for my xcdatamodel: