我有一个对象selectedActivity定义如下:
@property (strong, nonatomic) Activities *selectedActivity;
在我需要使用它时,请注销:
{
actname = "Running";
aid = 23;
}
Activities对象是NSManaged对象,其.h文件如下所示。
@interface Activities : NSManagedObject
@property (nonatomic, retain) NSString * actname;
@property (nonatomic, retain) NSNumber * aid;
@end
我试图将一个对象属性放在一个变量中,使用以下代码来处理它:
NSNumber *aid = _selectedActivity.aid;
但是,上面的行给出了以下错误消息:
[NSKnownKeysDictionary1 aid]: unrecognized selector sent to instance 0x147842fb0
任何人都可以解释这个错误以及如何修复它吗?
感谢您的任何建议。
更新: 选定活动的设置
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
_selectedActivity =([_activities objectAtIndex:row]);
NSLog(@"selectedActivity in pickerview%@",_selectedActivity);
}
//activities in .h file
@property (strong, nonatomic) NSArray *activities;
//setting of activities
self.activities= [self getActivities];
//method that gets activities
- (id) getActivities{
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Activities"];
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"created" ascending:NO];
[fetchRequest setSortDescriptors:@[sort]];
NSError *error = nil;
self.managedObjectContext = [Model sharedInstance].managedObjectContext;
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest
error:&error];
NSMutableArray *mutableresults = [results mutableCopy];
[mutableresults removeObjectIdenticalTo:[NSNull null]];
return mutableresults;
}
答案 0 :(得分:0)
结果将作为字典出现。
代替通常的NSManagedObject
- 样式值检索:
目标C
NSString *actname = _selectedActivity.actname;
NSNumber *aid = _selectedActivity.aid;
夫特
let actname:String = _selectedActivity.actname
let aid:Int = _selectedActivity.aid
使用 NSDictionary
- 样式值检索,如下所示:
目标C :
NSString *actname = [_selectedActivity valueForKey:@"actname"];
NSNumber *aid = [[_selectedActivity valueForKey:@"aid"] intValue];
Swift(从3.0.1开始测试)
_selectedActivity(forKey: "actname") as! String
_selectedActivity.value(forKey: "aid") as! Int