gpt4 book ai didi

ios - 制作完成 block 的问题 [iOS]

转载 作者:行者123 更新时间:2023-11-29 02:28:25 25 4
gpt4 key购买 nike

我正在尝试编写一个带有返回收集数据的完成 block 的方法。我不确定是我没有做对还是其他问题。

我的方法:

-(void)getAllUserDataWithUsername:(NSString *)username completion:(void (^)(NSDictionary     *))data {

我希望能够将 NSDictionary 设置为接收到的数据,并能够在我在某处调用此方法时获取该数据。

谢谢!

最佳答案

为了使您的声明更加清晰,需要进行一些细微的更改。 data 应该是 NSDictionary 参数的名称,而不是完成 block 名称。

声明、实现和调用带有完成 block 的方法的分步指南如下:

在实现该方法的类 header 中,您可以声明该方法:

- (void)getAllUserDataWithUsername:(NSString *)username 
completion:(void (^)(NSDictionary* data))completion;

请注意,data 是 block 中传递的参数,而 completion 是 block 的名称。

在你的类的实现中你可以这样做:

- (void)getAllUserDataWithUsername:(NSString *)username 
completion:(void (^)(NSDictionary* data))completion {
// your code to retrieve the information you need
NSDictionary *dict = //the data you retrieved
// call the completion block and pass the data
completion(dict); // this will be passed back with the block to the caller
}

现在,无论您在何处调用此方法,都可以执行以下操作:

[myClass getAllUserDataWithUsername:@"username" completion:^(NSDictionary *data) {
// data will be `dict` from above implementation
NSLog(@"data = %@", data);
}];

希望对您有所帮助。

关于ios - 制作完成 block 的问题 [iOS],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27206589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com