Gerry
Gerry

Reputation: 1236

How to call Swift initializer (that has a parameter) from Objective-C?

I have a Swift class:

class MyTextField : NSObject, UITextFieldDelegate { 

    var myField: UITextField

    // no idea how to pass in my UITextField from Objective-C code
    init (x: UITextField) {
        myField = x;
    }
}

I have added a property (not shown) so I could set myField, and everything is working.

Would like to use the init function, if only the syntax were not a complete mystery. Any ideas?

Upvotes: 19

Views: 6589

Answers (1)

NRitH
NRitH

Reputation: 13893

Call it the same way you'd call an Obj-C initializer, so for your example, you'd call [[MyTextField alloc] initWithX:theTextField];.

Upvotes: 18

Related Questions