-1

property block getting nil I have objective-c viewcontroller class which has a property block in // UAEPassWebViewController.h

@property (nonatomic, copy) void (^onUAEPassSuccessBlock)(NSString *response);
@property (nonatomic, copy) void (^onUAEPassFailureBlock)(NSString *response);

I had made the bridging-header.h class and trying to call code in swift class like below

webVC.onUAEPassSuccessBlock = { (code: String) in
            print(code)
            if (code != "") {
                self.showHud()
                self.getUaePassTokenForCode(code)
            }
            } as? (String?) -> Void

but onUAEPassSuccessBlock property getting nil when executing code in // UAEPassWebViewController.m

if(_onUAEPassSuccessBlock && code) {
            _onUAEPassSuccessBlock(code);
            [self.navigationController popViewControllerAnimated:true];
        }

this is only happens when I am calling from swift class. if I call the same property from objective-c class the below is the code it worked fine. // ViewController.m

 webVC.onUAEPassSuccessBlock = ^(NSString *code) {

            if(code) {

                [self showLoadingIndicator];
                [self getUaePassTokenForCode:code];
            }
        };

anyone can help me please

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

0

The value is nil because that's what you requested when you added as? (String?) -> Void. This says "if it's not possible to convert this to the given type, then return nil." It's not possible to convert this function to the correct type, so it's nil.

The function you've written is (String) -> Void. You cannot implicitly convert that to (String?) -> Void. What would the first function do if it received nil?

Rewrite your ObjC to not allow nil in this position using _Nonnull:

@property (nonatomic, copy) void (^onUAEPassSuccessBlock)(NSString * _Nonnull response);

Or rewrite the function to accept String?:

webVC.onUAEPassSuccessBlock = { (code: String?) in
...
Rob Napier
  • 286,113
  • 34
  • 456
  • 610