无法将Float类型的值转换为预期的参数类型NSNumber

问题描述:

我从swift类调用目标c中的方法。我的目标c方法是期望在NSNumber格式的数字。我没有在swift中定义数字类型,但仍然收到以下消息。有什么办法可以解决这个问题吗?无法将Float类型的值转换为预期的参数类型NSNumber

enter image description here

- (void)getPercentageMatch:(NSString *)answer listOfAnswers:(NSArray *)validAnswers completionBlock:(void(^)(BOOL isSuccess, NSNumber *percent))completionBlock { 
NSMutableDictionary *params = [NSMutableDictionary dictionary]; 
[params setValue:answer forKey:@"myanswer"]; 
[params setValue:validAnswers forKey:@"answers"]; 
NSMutableURLRequest *req = [AuthorizationHandler createAuthReq:@"POST" path:@"validateAnswer" params:params]; 

AFJSONRequestOperation *op = [[AFJSONRequestOperation alloc] initWithRequest:req]; 
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSDictionary *responseheaders = (NSDictionary *)responseObject; 
    if (completionBlock) { 
     completionBlock(true, [responseheaders valueForKey:@"percentage"]); 
    } 
} 
failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    if (completionBlock) { 
     completionBlock(false, 0); 
    } 
}]; 
[op start]; 
return; 
} 

我也被迫结束块铸字

as! completionBlock(Bool, NSNumber) 

但这并不工作。

我是新来的迅速和任何教程/指针可以理解的:)

编辑1:

当我修改完成块

let completionBlock = { (isSuccess, percent) in 
     print("-------", isSuccess, percent) 
    } 
    MTRestkitManager.instance().getPercentageMatch(_:text, listOfAnswers:validAnswers, completionBlock:completionBlock) 

我没有得到任何错误。但是,如果我再次添加一个轻微的修改,则会出现错误。

enter image description here

为什么行为变化?

编辑2: 继柯林的解决方案,进行了如下修改

@IBAction func submitButtonTapped(_ sender: Any) { 
    answerTextView.resignFirstResponder() 
    if answerTextView.textColor == UIColor.lightGray { 
     ZUtility.showError(NSLocalizedString("Please enter answer before submitting", comment: "")) 
     return; 
    } 
    if answerTextView.text.isEmpty { 
     ZUtility.showError(NSLocalizedString("Please enter answer before submitting", comment: "")) 
     setPlaceholderText() 
     return; 
    } 
    let text = answerTextView.text as String 
    let completionBlock = { (isSuccess, percent:NSNumber) -> (Void) in 
     self.handleAnswer(isSuccess: isSuccess, percent: Float(percent)) 
    } 
    MTRestkitManager.instance().getPercentageMatch(_:text, listOfAnswers:validAnswers, completionBlock:completionBlock) 
} 

在我收到一个错误的最后一行:无法将类型的价值“(BOOL,NSNumber的) - >(无效)” '?!((BOOL,NSNumber的) - >(无效)' 预期参数类型

+1

发布你的swift代码不是图像.. – Bilal

+0

这是如何有所作为? –

+0

看到这个... https://meta.stackoverflow.com/a/285557/1825618 – Bilal

显然,Float是从selfhandleAnsewer()功能推断你应该在你的手动定义completionBlock正确类型:

let completionBlock = { (isSuccess, percent: NSNumber?) in 
    self.handleAnsewer(isSuccess: isSuccess, percent: percent.floatValue ?? 0) 
} 

并记住,你不能自动在Swift中进行类型转换,你应该调用显式初始值设定项,如NSNumber(value: myFloat)Int(percent)。

P.S.我敢打赌,你来自蟒蛇:)

+0

谢谢你的回答,凯琳。我修改了我的代码并编译了它,但在运行时失败,并显示EXC_BAD_INSTRUCTION消息: MTRestkitManager.instance()。getPercentageMatch(_:text,listOfAnswers:validAnswers,completionBlock:completionBlock as!(Bool,NSNumber?) - > void ) –

+0

似乎'Float(百分比)'返回可选值。我更新了答案。 – kelin

+0

感谢您的更新。我仍然面临EDIT 2中给出的问题。任何想法为什么期望的参数类型NSNumber是可选的?我相信,在objective-c中没有这样的概念。 –