在我的 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]);
}
}