gpt4 book ai didi

ios - 在两个类之间使用 NSDictionary 值

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

我正在尝试获取两个类之间的 NSDictionary 中的值..

我在Database.m (NSObject类)

中使用此方法
+(void)fetchAllUserWithReference:(NSDictionary *)dictionary {

FIRDatabaseReference *userReference = [[[FIRDatabase database] reference] child:RTDUSER];
[userReference observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
__block NSDictionary *dict = dictionary;
dict = snapshot.value;

} withCancelBlock:nil];
}

我想在 Dashboard.m (UIView Controller) 类中获取 NSDictionary 数据,如下所示..

-(void)retrieveUsers {
NSDictionary *dict = [[NSDictionary alloc] init];
[Database fetchAllUserWithReference:dict];
NSLog(@"DICT %@",dict);
}

为什么我的Dashboard.m类的NSDictionary总是NULL?我哪里错了?

最佳答案

您可以使用完成处理程序在执行 block 后取回值。

-(void)fetchAllUserWithReference:(NSDictionary *)param
withCompletionHandler:(void (^)(NSDictionary *response))block{

FIRDatabaseReference *userReference = [[[FIRDatabase database] reference] child:RTDUSER];
[userReference observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
NSDictionary *dict = snapshot.value;
block(dict);
} withCancelBlock:
block(nil);];
}

-(void)retrieveUsers {
NSDictionary *dict = [NSDictionary init];
[self fetchAllUserWithReference:dict withCompletionHandler:^(NSDictionary *response) {
NSLog(@"%@",response);
}];
}

关于ios - 在两个类之间使用 NSDictionary 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49172295/

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