【发布时间】:2015-11-11 06:17:32
【问题描述】:
在检查了诸如unrecognized selector sent to instance 和Unrecognized selector sent to instance? 之类的“Unrecognized selector sent”问题的所有答案后,我的情况并不满足,因此这是我的完整方案:
说明:
在我的应用程序中存在一个设置视图,其中包含一个 UISwitch 将他的所有预订添加到电话日历或不添加。
所以我需要将用户的选择保存在 CoreData 中以将预订添加到日历或不添加,即 UISwitch 的状态。
当应用程序第一次启动时,UISwitch 将 ON 并且他的状态将保存在 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