Ios [NSManagedObject setSwitchState:]:在Objective-C中发送了无法识别的选择器

Ios [NSManagedObject setSwitchState:]:在Objective-C中发送了无法识别的选择器,ios,objective-c,core-data,Ios,Objective C,Core Data,在检查了“未识别的选择器发送的所有答案后,”问题,例如,和不符合我的情况,因此这是我的完整场景: 说明: 在我的应用程序中,有一个设置视图,其中包含一个ui开关,可以将他的所有预订添加到手机日历中,也可以不添加 因此,我需要将用户的选择保存在CoreData中,以向日历添加保留或不添加保留,即ui开关的状态 最初,当应用程序第一次启动时,UISwitch将打开,他的状态将保存在CoreData中,固定ID为1,因为我不想添加多个对象,我只需要保留一个对象,当用户更改UISwitch的值时,应用程

在检查了“未识别的选择器发送的所有答案后,”问题,例如,和不符合我的情况,因此这是我的完整场景:

说明:

在我的应用程序中,有一个设置视图,其中包含一个ui开关,可以将他的所有预订添加到手机日历中,也可以不添加

因此,我需要将用户的选择保存在CoreData中,以向日历添加保留或不添加保留,即ui开关的状态

最初,当应用程序第一次启动时,UISwitch打开,他的状态将保存在CoreData中,固定ID为1,因为我不想添加多个对象,我只需要保留一个对象,当用户更改UISwitch的值时,应用程序应使用新状态更新对象我尝试了此解决方案,但也出现了错误

完整代码:

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
设置VIEWCONTROLLER.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
对于应用程序所经历的例外情况:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSManagedObject setSwitchState:]:发送到实例0x7fb6f3411d20的选择器无法识别”

对于我的xcdatamodel:

执行
NSLog(“%@”,NSStringFromClass(h.class))
以查看SwitchState对象实际上是什么类型的类。很可能您在核心数据模型文件中配置了错误的内容


而且,说真的,所有的类名都应该是大写的…

更新此类文件中的代码

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
同时更新.xcdatamodeld文件中的实体名称和类名


您的实体名称和属性名称相同。。你是如何创建这个类的???你是如何创建一个以小写字母开头的实体名称的?我只是尝试重命名相同的错误,我通过复制必要的代码测试过,效果很好。这很奇怪吧?你没有名为switchstate的属性看看我的代码我只是更新了日志给我“SwitchStateEntity”有些人怎么能测试我拥有的代码并且工作正常,而当我在我的应用程序上运行它时,它就不工作了:我只是做了你所做的,我在xcdatamodel中创建了新的实体,同样的错误出现了。你的类名是什么?模型中的实体名称是什么?类名是
SwitchStateEntity
,模型中的实体名称是
SwitchStateEntity
,在SwitchStateEntity中。检查接口定义,如“@interface SwitchStateEntity:NSManagedObject”已经完成,错误让我感到疯狂:s
- (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;
    }
}
#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
#import "SwitchStateEntity.h"
@implementation SwitchStateEntity
@dynamic switchId;
@dynamic   switchState;
@dynamic showReminderAlert;
@end
SwitchStateEntity *h = [ fetchedObjects firstObject]; 
//try this code
NSLog(@"it should not be nil = %@",self.isSwitchOnOrOf); 

[h setSwitchState:[NSNumber numberWithBool:self.isSwitchOnOrOf]];