问题描述
我正在使用Facebook SDK中的一个块。它返回一个字典。我希望该字典作为方法的返回值。我试图把我的头围绕整个块概念,但需要向正确的方向微调。块:
(块的参数是一个字符串userFBid)
- (NSDictionary *)getMutualFBFriendsWithFBid:(NSString *)fbID {
[ FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@/%@ / mutualfriends /%@,[[PFUser currentUser] objectForKey:kFbID],userFBid]
参数:nil
HTTPMethod:@GET
completionHandler:^(
FBRequestConnection * connection,
id result,
NSError * error
){
result =(NSDictionary *)result;
//返回结果;
}];
}
返回值?
我已经尝试google了,但我不能让我的手。
我会感激任何指向正确的方向。
编辑:
主要问题如下:我需要完成处理程序来调用另一个类中的方法...如何做到这一点?
由于方法 startWithGraphPath
是异步的,您无法编码好像它是同步的:它意味着没有返回值,因为一旦调用此方法,您的应用程序将继续执行到下一行,并且不会等待返回的值。
所以,为了保持这个异步,我假设你想在你自己的函数中使用这个结果,所以在你的 completionHandler
块中调用它:
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@/%@ / mutualfriends /%@,[[PFUser currentUser] objectForKey :kFbID],userFBid]
参数:nil
HTTPMethod:@GET
completionHandler:^(FBRequestConnection * connection,id result,NSError * error){
[self myRockinFunction :结果];
}];
//考虑这个函数
- (void)myRockinFunction:(NSDictionary *)fb_result {
//用fb_result
进行填充
编辑
好的,我明白了修改你的方法接受回调作为一个参数:
- (NSDictionary *)getMutualFBFriendsWithFBid:(NSString *)fbID andCallback :( void(^)(NSDictionary *))callback {
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@/%@ / mutualfriends /%@,[[PFUser currentUser] objectForKey:kFbID],userFBid ]
参数:nil
HTTPMethod:@GET
completionHandler:^(FBRequestConnection * connection,id result,NSError * error){
//你应该先处理错误
//然后将结果转换为NSDictionary
回调((NSDictionary *)结果); //并触发回调结果
}];
}
然后,在其他类中,使用另一个块来对待您的结果: / p>
[YourHelperClass getMutualFBFriendsWithFBid:fbID andCallback:^(NSDictionary * result){
//使用结果如何希望
//当心,这也是异步的。
}];
注意:您应该在触发回调之前对待错误。
编辑2(来自其他用户的帮助赞赏)
更好的是,您可能会尝试传递所有参数的回调(未测试,而不是确定语法,如果有人可以纠正我,我会感激):
- (NSDictionary *)getMutualFBFriendsWithFBid:(NSString *)fbID andCallback:(void(^)(FBRequestConnection *,NSDictionary *,NSError *))callback {
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@/%@ / mutualfriends /%@ ,[[PFUser currentUser] objectForKey:kFbID],userFBid]
参数:nil
HTTPMethod:@GET
completionHandler:callback()]; //不确定在这里
}
[YourHelperClass getMutualFBFriendsWithFBid:fbID andCallback:^(FBRequestConnection * connection,NSDictionary * result,NSError * error){
//你现在可以处理这里的错误
}];
的参考,以便更深入地了解。
I am using a block from Facebook SDK. It returns a dictionary. I want that dictionary as a return value of a method. I am trying to wrap my head around the whole block concept but need a nudge in the right direction.
The block:(the argument for the block is a string userFBid)
-(NSDictionary*) getMutualFBFriendsWithFBid:(NSString*)fbID {
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"/%@/mutualfriends/%@", [[PFUser currentUser] objectForKey:kFbID],userFBid]
parameters:nil
HTTPMethod:@"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
result = (NSDictionary*)result;
//return result;
}];
}
How do i get the return value?
I have tried to google it, but i cant get my hand around it.
I would appreciate any pointer in the right direction.
EDIT:The main question is the following: I need to the completion handler to call a method in another class... how to do that?
As the method startWithGraphPath
is asynchronous, you can't code as if it was synchronous : it means no return value, because as soon as this method is called, your app will continue to execute to the next line, and won't wait for a returned value.
So, to keep this async, I assume you want to use the result of this in your own function, so call it in your completionHandler
block:
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"/%@/mutualfriends/%@", [[PFUser currentUser] objectForKey:kFbID],userFBid]
parameters:nil
HTTPMethod:@"GET"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
[self myRockinFunction:result];
}];
//Considering this function
-(void)myRockinFunction:(NSDictionary*) fb_result{
//Do stuff with fb_result
}
Edit
OK, I get it. Modify your method to accept a callback as a parameter :
-(NSDictionary*) getMutualFBFriendsWithFBid:(NSString*)fbID andCallback:(void (^)(NSDictionary *))callback {
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"/%@/mutualfriends/%@", [[PFUser currentUser] objectForKey:kFbID],userFBid]
parameters:nil
HTTPMethod:@"GET"
completionHandler:^(FBRequestConnection *connection,id result,NSError *error) {
//You should treat errors first
//Then cast the result to an NSDictionary
callback((NSDictionary*) result); //And trigger the callback with the result
}];
}
Then, in your other class, use another block to treat your result :
[YourHelperClass getMutualFBFriendsWithFBid:fbID andCallback:^(NSDictionary* result){
//Use result how you wish
//Beware, this is async too.
}];
Note : you should treat the error before triggering your callback.
Edit 2 (Help from other users appreciated)
Even better, you might try to pass a callback taking all the parameters (not tested, and not sure of the syntax. If someone can correct me, I'd appreciate):
-(NSDictionary*) getMutualFBFriendsWithFBid:(NSString*)fbID andCallback:(void (^)(FBRequestConnection *,NSDictionary *,NSError *))callback {
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"/%@/mutualfriends/%@", [[PFUser currentUser] objectForKey:kFbID],userFBid]
parameters:nil
HTTPMethod:@"GET"
completionHandler:callback()]; //Not sure here!
}
[YourHelperClass getMutualFBFriendsWithFBid:fbID andCallback:^(FBRequestConnection *connection,NSDictionary * result,NSError *error){
//You could deal with errors here now
}];
Here's a reference on Apple's docs for deeper understanding.
这篇关于具有返回值的iOS块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!