如何从 Objective-C 调用 Swift 初始值设定项(具有参数)? [英] How to call Swift initializer (that has a parameter) from Objective-C?

查看:54
本文介绍了如何从 Objective-C 调用 Swift 初始值设定项(具有参数)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Swift 课程:

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;
    }
}

我添加了一个属性(未显示)以便我可以设置 myField,并且一切正常.

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

希望使用 init 函数,如果语法不是完全神秘的话.有什么想法吗?

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

推荐答案

像调用 Obj-C 初始化程序一样调用它,因此对于您的示例,您可以调用 [[MyTextField alloc] initWithX:theTextField];.

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

这篇关于如何从 Objective-C 调用 Swift 初始值设定项(具有参数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆