gpt4 book ai didi

ios - 返回 NSString 时如何解决不兼容的 block 指针类型编译器错误?

转载 作者:行者123 更新时间:2023-11-29 00:05:38 24 4
gpt4 key购买 nike

我试图用这个方法返回一个变量 fifeScore,但我一直收到错误:

Incompatible block pointer types sending 'NSString *(^)(FIRDataSnapshot *__strong)' to parameter of type 'void (^ _Nonnull)(FIRDataSnapshot * _Nonnull __strong)'

+(NSString *)getFifeScoreForUserWithUID:(NSString *)uid {

FIRDatabaseReference *scoreRef = [[[[FIRDatabase database] reference] child:@"Scores"] child:uid];
[scoreRef observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {

NSString * fifeScore;

if(snapshot.exists){
NSString * tempScore = [snapshot.value objectForKey:@"fifeScore"];
fifeScore = [NSString stringWithFormat:@"%@",tempScore];
} else {
fifeScore = @"0";
}
return fifeScore;
}];

}

当我注释掉 return 语句时,错误消失了。这是我第一次尝试编写返回字符串的方法。这里有什么问题,我该如何解决错误?

最佳答案

在这种情况下你应该使用 block 。

+(void)getFifeScoreForUserWithUID:(NSString *)uid completionBlock:(void(^)(NSString *score))completion {

FIRDatabaseReference *scoreRef = [[[[FIRDatabase database] reference] child:@"Scores"] child:uid];
[scoreRef observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {

NSString * fifeScore;

if(snapshot.exists){
NSString * tempScore = [snapshot.value objectForKey:@"fifeScore"];
fifeScore = [NSString stringWithFormat:@"%@",tempScore];
} else {
fifeScore = @"0";
}

if (completion) {
completion(fifeScore)
}
}];
}

用法:

[YOUR_CLASS getFifeScoreForUserWithUID:YOUR_UID completionBlock:^(NSString *score) {
NSLog(@"%@", score);
}];

关于ios - 返回 NSString 时如何解决不兼容的 block 指针类型编译器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48125517/

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