我尝试使用此方法返回变量fifeScore
,但我一直收到错误:
不兼容的块指针类型发送&#NSString *(^)(FIRDataSnapshot * __ strong)'参数类型' 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
语句时,错误消失了。这是我第一次尝试编写一个返回字符串的方法。问题是什么,如何解决错误?
答案 0 :(得分:1)
在这种情况下你应该使用阻止。
+(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);
}];