Search code examples
objective-cswiftxcodeobjective-c-blocks

Objective C block syntax - Xcode autocomplete is not working


Some OBJECTIVE-C blocksyntax help, please.

This is my call site (AppDelegate) - autocompleted by XCode

Objective-C

  [SwiftClass passValue: response completion:^(NSDictionary<NSString *,NSString *> * _Nullable, ErrorResponse * _Nullable) { }];

And my method looks like this (Swift)

  @objc static func passValue(code: ValidatedResponse, completion: @escaping ([String : String]?, ErrorResponse?) -> Void)

I am getting an Error saying : Parameter name omitted

So, I gave names to the parameters like this:

[SwiftClass passValue: response completion:^(NSDictionary<NSString *,NSString *> *responseDictionary _Nullable, ErrorResponse *errorResponse _Nullable) {}];

Now I get this Error:

Incompatible block pointer types sending 'void (^)(NSDictionary<NSString *,NSString *> *__strong)' to parameter of type 'void (^ _Nonnull)(NSDictionary<NSString *,NSString *> * _Nullable __strong, ErrorResponse * _Nullable __strong)'

Do you guys know what could be wrong with the way I am calling the method in ObjectiveC ?

by the way, yes, I am aware of the docs and of http://fuckingblocksyntax.com/, I have already looked through there and cannot figure out the problem.


Solution

  • I found it! the parameter names have to go after Nullable, like so:

    [SwiftClass passValue: response completion:^(NSDictionary<NSString *,NSString *> * _Nullable responseDictionary, ErrorResponse * _Nullable errorResponse) {}];