1
votes

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?

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;
}

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.

enter image description here

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)!'

1
post your swift code not image.. - Bilal
How does that make a difference? - Astha Gupta
Got the point. Thanks :) Let me modify it. - Astha Gupta

1 Answers

0
votes

Obviously, Float is inferred from the handleAnsewer() function of self. You should define proper type in your completionBlock manually:

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

And remember, you can't automatically typecast in Swift, you should call explicit initializers, such as NSNumber(value: myFloat) or Int(percent).

P.S. I bet you come from python :)