0

From Objective-C, to call a a Swift function with one argument you have to use bracket syntax and do something I've always found confusing: Add the word "With" before the argument and capitalize the argument label as in:

//swift extension

@objc public func getImageCalled(name: String?) -> UIImage? {
}

//Called from Objective-C
 ImageClass *imgObject = [[ImageClass alloc] init];
 UIImage * img = [imgObject getImageCalledWithName:myname];

How do you do this with two arguments as in:

@objc public func getImageCalled(name: String?,width:Int?) -> UIImage? {
}

[imgObject getImageCalledWithName:myname WithWidth:30]; is not working for me.

3
  • Does your Swift code compile at all? There should be an error message “Method cannot be marked @objc because the type of the parameter 2 cannot be represented in Objective-C” Commented Oct 14, 2020 at 20:47
  • Any reason why that width is optional? Commented Oct 14, 2020 at 21:21
  • The With<NameOfFirstParameter> is only a convention for the first argument, all subsequent arguments are just named the same. It originates from Objective-C where you don't specify a named argument for the first parameter due to its syntax. Commented Oct 14, 2020 at 22:01

1 Answer 1

3

You can try

[imgObject getImageCalledWithName:myname width:30];  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.