在我的tableViewController中,我有这种代码(这里是简化的):
-(void)saveOnDatabase{
//here I set up NSManagedObjectContext and everything
[[[array objectAtIndex:1] objectForKey:@"tableLabels"] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
if (obj==solvent) {
[pepe setValue: [self numberFromText:delta :2] forKey:obj];
}else {
[pepe setValue: nil forKey:obj];
}
}];
}
它运作正常,它符合我的预期。但是为了保持代码干净,我想从我的模型执行这段代码,这是NSObject的子类,我在那里进行所有计算以及与UI无关的所有事情。问题是,当我尝试在模型类上实现它时,它会给我带来错误,例如:Incompatible block pointer types sending 'int((^))(void)' to parameter of type 'void (^)(__strong id, NSUInteger, BOOL*)'
该块包含如下方法:
-(void)saveCustomToDatabase:id withContext:(NSManagedObjectContext*)context withImpurity:(NSString*)impurity withDelta:(NSString *)delta ... // the rest
我真的不知道这里的交易是什么以及为什么它甚至没有在其他类上编译,考虑到我传递了所有必要的参数......
完整的方法就是这个:
-(void)saveCustomToDatabase:id withContext:(NSManagedObjectContext*)context withImpurity:(NSString*)impurity withDelta:(NSString *)delta withAssigment:(NSString*)assigment withEntity:(NSString*)entity withSolvent:(NSString*)solvent {
NSManagedObject *pepe = [NSEntityDescription insertNewObjectForEntityForName:entity inManagedObjectContext:context];
if (assigment.length>1) [pepe setValue:assigment forKey:@"definition"];
[pepe setValue:impurity forKey:@"impurity"];
[[[[self pickerArray] objectAtIndex:1] objectForKey:@"tableLabels"] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
if (obj==solvent) {
[pepe setValue: [self numberFromText:delta :2] forKey:obj];
}else {
[pepe setValue: nil forKey:obj];
}
}];
NSError *error;
if (![context save:&error])
{
NSLog(@"Problem saving: %@", [error localizedDescription]);
}
}