【问题标题】:[NSManagedObject setSwitchState:]: unrecognized selector sent in Objective-C[NSManagedObject setSwitchState:]:Objective-C 中发送的无法识别的选择器
【发布时间】:2015-11-11 06:17:32
【问题描述】:

在检查了诸如unrecognized selector sent to instanceUnrecognized selector sent to instance? 之类的“Unrecognized selector sent”问题的所有答案后,我的情况并不满足,因此这是我的完整方案:

说明:

在我的应用程序中存在一个设置视图,其中包含一个 UISwitch 将他的所有预订添加到电话日历或不添加。

所以我需要将用户的选择保存在 CoreData 中以将预订添加到日历或不添加,即 UISwitch 的状态。

当应用程序第一次启动时,UISwitchON 并且他的状态将保存在 CoreData 中并具有固定 ID 1 因为我不想添加很多对象我只需要保留一个对象,当用户更改 UISwitch 的值时,应用程序应该用新状态更新对象我尝试这个解决方案@987654323 @我也有错误

完整代码:

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;
    }
}

对于应用程序通过的例外情况:

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

对于我的 xcdatamodel:

【问题讨论】:

  • 你的实体名和属性名是一样的..你是怎么创建那个类的???你是如何创建以小写字母开头的实体名称
  • 我只是尝试重命名也同样的错误
  • 我通过复制必要的代码进行了测试,效果很好..
  • 这很奇怪吧?
  • 你没有名为 switchState 的属性

标签: ios objective-c core-data


【解决方案1】:
SwitchStateEntity *h = [ fetchedObjects firstObject]; 
//try this code
NSLog(@"it should not be nil = %@",self.isSwitchOnOrOf); 

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

【讨论】:

    【解决方案2】:

    更新此类文件中的代码

    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
    

    同时更新 .xcdatamodeld 文件中的实体名称和类名称

    【讨论】:

    • 我只是做了你所做的,我在 xcdatamodel 中创建了新实体,并且出现了同样的错误
    • 你的班级名称是什么?和模型中的实体名称?
    • 类名是SwitchStateEntity,模型中的实体名是SwitchStateEntity
    • 在 SwitchStateEntity.h 检查接口定义如“@interface SwitchStateEntity : NSManagedObject”
    • 已经完成了,这个错误真的让我抓狂:S
    【解决方案3】:

    执行NSLog("%@", NSStringFromClass(h.class)) 以查看 SwitchState 对象真正属于哪种类。您可能在核心数据模型文件中错误地配置了某些内容。

    另外,说真的,你所有的类名都应该是大写单词...

    【讨论】:

    • 看看我的代码我只是更新它日志给我“SwitchStateEntity”
    • 有些人如何测试我拥有并且工作正常的代码,当我在我的应用程序上运行它时它不起作用:S
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多