I am calling a method in objective c from swift class. My objective c method is expecting number in the format of NSNumber. I did not define the number type in swift but still I get the below message. Is there any way to resolve this?
- (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;
}
I also forced completion block typecasting
as! completionBlock(Bool, NSNumber)
but this doesnt work.
I am new to swift and any tutorials/pointers will be appreciated :)
EDIT 1:
When I modify completion block
let completionBlock = { (isSuccess, percent) in
print("-------", isSuccess, percent)
}
MTRestkitManager.instance().getPercentageMatch(_:text, listOfAnswers:validAnswers, completionBlock:completionBlock)
I don't get any error. However, if I add one slight modification again I get an error.
Why is the behavior changing?
EDIT 2: Following kelin's solution, made the following changes
@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)
}
On the last line I am receiving an error : Cannot convert value of type '(Bool, NSNumber) -> (Void)' to expected argument type '((Bool, NSNumber?) -> (Void)!'

