【发布时间】:2012-12-25 17:41:51
【问题描述】:
我有一个在回调中异步返回对象(例如 UserProfile)的方法。
基于此 UserProfile 对象,一些代码计算 UITableViewCell 是否可编辑:
我创建了以下代码,但很遗憾它不起作用。
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
Entry *entry = [[self.feed entries] objectAtIndex:[indexPath row]];
typedef BOOL (^BOOLBlock)(id);
BOOLBlock bar = ^BOOL (id p) {
if (p) {
UserProfile *user = (UserProfile *) p;
NSEnumerator *e = [[entry authors] objectEnumerator];
id object;
while (object = [e nextObject]) {
if ([[object name] isEqualToString:[[[user authors] objectAtIndex:0] name]])
return TRUE;
}
return FALSE;
}
else {
return FALSE;
}
};
[[APPContentManager classInstance] userProfile:bar];
}
在最后一行,它说不兼容的块指针类型发送
'__strong BOOLBlock' (aka 'BOOL (^__strong)(__strong id)') to parameter of type 'void (^)(UserProfile *__strong)'
APPContentManager.h
-(void)userProfile:(void (^)(UserProfile *user))callback;
【问题讨论】:
-
您的 AppContentManager userProfile 方法似乎不期望具有布尔返回值的块。
-
此外,您应该知道委托方法 canEditRowAtIndexPath 是立即返回布尔值而不是异步的事实。您应该返回数据建模板,看看是否可以在需要时立即将所需信息保留在手边(例如,通过使用该值预填充 userProfile 模型)。
-
您也没有在该方法中返回任何值。
-
直到,感谢您的快速回复。 1.)我添加了相关方法的签名。你是对的,在它当前的实现中,它不期望一个布尔返回值。有没有办法更改 canEditRowAtIndexPath 中的代码而不是 userProfile 中的代码? 2.)感谢您的提示。实际上,UserProfile 缓存在 APPContentManager 中,所以会立即返回。
-
请参阅@NoahWitherspoon 解决方案 - 尽管我个人会朝不同的方向前进,只需向模型类 (userProfile) 添加一个方法,让我知道是否可以编辑该配置文件。这样你就不需要使用信号量了——信号量是一种相当重的武器......
标签: iphone objective-c ios callback objective-c-blocks