For example I have this method in swift:
@objc class MyClass: NSObject
....
@objc class func viewWithIndex(index: Int, str: String) {
println(index, str)
}
then i wanna call that method in my objective-c class, and i was expecting as simple as this call [MyClass viewWithIndex:10 str:@"string"];
but it doesn't work.
How do i call it? Please help.
Note: I have already a working swift function call to objective-c [MyClass showSomething];
so that means i have successfully setup necessary settings to bridge classes. Only the function that has two of more parameters is my problem. :)
Solved:
I dont know what i happened but i just restarted my mac and removed objc
and it worked with the call [MyClass viewWithIndex:10 str:@"string"];
. I remember reading in documentation.
Migrating Your Objective-C Code to Swift
You can use Objective-C and Swift files together in a single project, no matter which language the project used originally. This makes creating mixed-language app and framework targets as straightforward as creating an app or framework target written in a single language.
Unfortunately, it's not possible to subclass a Swift class in Objective-C. Straight from the docs: You cannot subclass a Swift class in Objective-C.
This worked for me in Swift 3.0
public class func viewWithIndex(_ index: Int, str: String) {
println(index, str)
}
Adding the underscore before the first parameter in the Swift declaration allowed me to call from objective c without naming the first parameter, like this:
[MyClass viewWithIndex:10 str:@"string"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With