0

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:

xcdatamodel

Cœur
  • 37,241
  • 25
  • 195
  • 267
Zakaria Darwish
  • 358
  • 5
  • 22

3 Answers3

1

Update Code in Such File

SwitchStateEntity.h

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface SwitchStateEntity : NSManagedObject
@property(strong,nonatomic) NSNumber *switchId;
@property (nonatomic,strong) NSNumber *switchState;
@property (nonatomic,strong) NSNumber *showReminderAlert;
@end

SwitchStateEntity.m

#import "SwitchStateEntity.h"
@implementation SwitchStateEntity
@dynamic switchId;
@dynamic   switchState;
@dynamic showReminderAlert;
@end

Also Update Entity name and class name in .xcdatamodeld file

enter image description here

Gaurav Patel
  • 957
  • 1
  • 6
  • 16
1
SwitchStateEntity *h = [ fetchedObjects firstObject]; 
//try this code
NSLog(@"it should not be nil = %@",self.isSwitchOnOrOf); 

[h setSwitchState:[NSNumber numberWithBool:self.isSwitchOnOrOf]];
Shebin Koshy
  • 1,182
  • 8
  • 22
0

Do a NSLog("%@", NSStringFromClass(h.class)) to see what kind of class the SwitchState object really is. Chances are you've incorrectly configured something in your Core Data Model file.

Also, seriously, all your class names should be UpperCaseWords...

Danny Bravo
  • 4,534
  • 1
  • 25
  • 43