我上课
Collections *tempLocalCollection = [[Collections alloc] init];
问题在于,当我尝试以后续方式提升属性时
tempLocalCollection.id = [f numberFromString:[NSString stringWithFormat:@"%@", @"0"]];
tempLocalCollection.action = @"client_insert";
我应该是错误的
- [Collections setId:]:无法识别的选择器发送到实例0xff8afb0 2015-09-16 10:25:14.235 App [1042:128375] *** 由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:' - [Collections setId:]: 无法识别的选择器发送到实例0xff8afb0'
我哪里错了?
Collections.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Items;
@interface Collections : NSManagedObject
@property (nonatomic, retain) NSNumber * datetime_creation;
@property (nonatomic, retain) NSNumber * datetime_last_update_client;
@property (nonatomic, retain) NSNumber * datetime_last_update_server;
@property (nonatomic, retain) NSString * local_delete;
@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSString * action;
@property (nonatomic, retain) NSString * label;
@property (nonatomic, retain) NSString * labelServer;
@property (nonatomic, retain) NSNumber * ref_user;
@property (nonatomic, retain) NSNumber * sorting;
@property (nonatomic, retain) NSNumber * system;
@property (nonatomic, retain) NSSet *collection_item;
@end
@interface Collections (CoreDataGeneratedAccessors)
@end
Collections.m
#import "Collections.h"
#import "Items.h"
@implementation Collections
@dynamic datetime_creation;
@dynamic datetime_last_update_client;
@dynamic datetime_last_update_server;
@dynamic local_delete;
@dynamic action;
@dynamic id;
@dynamic label;
@dynamic labelServer;
@dynamic ref_user;
@dynamic sorting;
@dynamic system;
@dynamic collection_item;
@end
这是使用类及其出错的方法。 我认为上下文是一样的吗?
-(Collections *) upgrateListCollection:(NSDictionary *) coll{
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber *id = [f numberFromString:[NSString stringWithFormat:@"%@", coll[@"id"]]];
Collections *tempLocalCollection = [[Collections alloc] init];
NSArray *tmp = [Collections MR_findByAttribute:@"id" withValue:id];
if(tmp.count != 0){
tempLocalCollection = tmp[0];
}
if(!tempLocalCollection.isAccessibilityElement){
tempLocalCollection.id = [f numberFromString:[NSString stringWithFormat:@"%@", @"0"]];
tempLocalCollection.action = @"client_insert";
}else{
if([tempLocalCollection.local_delete isEqual: @"1"]){
tempLocalCollection.action = @"client_delete";
}else{
if([tempLocalCollection.label isEqual:coll[@"label"]]){
if(tempLocalCollection.datetime_last_update_client < coll[@"datetime_last_update_server"]){
tempLocalCollection.action = @"client_update";
}else{
tempLocalCollection.action = @"server_update";
}
}
}
}
if(tempLocalCollection != nil){
tempLocalCollection.labelServer = coll[@"label"];
tempLocalCollection.datetime_last_update_server = coll[@"datetime_last_update_server"];
tempLocalCollection.datetime_creation = coll[@"creation_utc_server"];
}
return tempLocalCollection;
}
答案 0 :(得分:1)
NSManagedObject
子类(Collections
)属性是动态的,它们的setter和getter是generated at runtime
因此,在这种情况下使用Collections
创建alloc/init
的对象时,不会创建动态属性,因此会出现异常unrecognized selector sent to instance
创建NSManagedObject
的正确方法是
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Collections" inManagedObjectContext:myMOC];
Collections *collection = (Collections *)[[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:myMOC];
如果你想创建Collections
的临时对象,那么将nil
作为管理对象上下文传递
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Collections" inManagedObjectContext:myMOC];
Collections *tempObj = (Collections *)[[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
答案 1 :(得分:0)
这是一个托管对象,需要在其管理环境中创建。