ios - [NSManagedObject setSwitchState :]: unrecognized selector sent in Objective-C

标签 ios objective-c core-data

在检查了“Unrecognised selector sent”问题的所有答案后,例如 unrecognized selector sent to instanceUnrecognized selector sent to instance? 不满足我的情况,所以这里是我的完整场景:

描述:

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

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

最初当应用程序第一次启动时,UISwitch 将被ON 并且他的状态将保存在具有固定 ID 的 CoreData 中1 因为我不想添加很多对象我只需要保留一个对象并且当用户更改 UISwitch 的值时应用程序应该使用新状态更新对象我也尝试这个解决方案 How to update existing object in Core Data?我遇到了一个错误

完整代码:

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

对于应用程序经历的异常(exception)情况:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject setSwitchState:]: unrecognised selector sent to instance 0x7fb6f3411d20'

对于我的 xcdatamodel:

xcdatamodel

最佳答案

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

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

关于ios - [NSManagedObject setSwitchState :]: unrecognized selector sent in Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32070244/

相关文章:

ios - 在 'CamScanner' 应用程序中进行图像裁剪控制的最佳方法是什么?

ios - Storyboard标签 <animations/> 是什么意思?

swift - 核心数据获取请求不起作用

objective-c - 滚动时 UITableView subview 出现在错误的部分?

ios - TableView 在删除时不选择新行

objective-c - iOS 从服务器获取数据

ios - 如何替换当前的 CALayer

ios - 向方法添加@objc 标志时出现 Xcode 编译器错误

swift - UserDefaults 还是核心数据?

core-data - 如何处理 UIManagedDocument?