xcode 自动生成自定义init方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23361160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate custom init method automatically
提问by Javier Roberto
Is it possible to generate custom init method automatically from Xcode, as Android Studio does for android? I mean, if I declare some properties in .h, for example: int a; int b;
是否可以从 Xcode 自动生成自定义 init 方法,就像 Android Studio 为 android 所做的那样?我的意思是,如果我在 .h 中声明一些属性,例如: int a; 国际b;
So, I would like to create automatically a init method as:
所以,我想自动创建一个 init 方法:
- (id)initWithA:(int) aInner andB:(int) bInner
{
a = aInner;
b = bInner;
}
回答by Mark
While there's still no way to do this automatically (without installing a plugin), there's a neat trick to convert a list of property declarations to assignments using multiple cursors:
虽然仍然无法自动执行此操作(无需安装插件),但有一个巧妙的技巧可以使用多个游标将属性声明列表转换为赋值:
- Copy the list of properties and paste it into your constructor
- Use Shift+Ctrl+ click to insert multiple cursors (Shift+Ctrl+↑/↓work as well)
- Edit with multiple cursors to assign values
- 复制属性列表并将其粘贴到您的构造函数中
- 使用Shift+ Ctrl+ 单击插入多个光标(Shift+ Ctrl+ ↑/↓也可以)
- 使用多个光标进行编辑以分配值
Credits go to Paul Hudson: Xcode in 20 Seconds: Multiple cursors
感谢 Paul Hudson:20 秒内的 Xcode:多个游标
回答by Wu Yuan Chun
New Xcode (after Xcode 10) support this issue for class (not for struct).
新的 Xcode(在 Xcode 10 之后)为类(不适用于结构)支持这个问题。
Steps:
脚步:
- right click on class name
- click "refactor"
- click "Generate Memberwise Initializer"
- 右键单击类名
- 点击“重构”
- 单击“生成成员初始化程序”
As for struct. You can make it class first, and change it back to struct after you get auto-gen init.
至于结构。您可以先将其设为 class,然后在获得 auto-gen init 后将其改回 struct。
回答by Matthew Cawley
There is no native way of doing this, however you can install XCode extensions that will add support for this.
没有这样做的本地方法,但是您可以安装 XCode 扩展来添加对此的支持。
See this following extension as this will provide the feature you are after (Swift version).
请参阅以下扩展,因为这将提供您所追求的功能(Swift 版本)。
回答by Earl Grey
Yo can create a snippet. You need to play with it a bit, create nice blue placeholders where relevant but most importantly, attach a keyboard shortcut to it. For example "initx"
你可以创建一个片段。您需要稍微尝试一下,在相关的地方创建漂亮的蓝色占位符,但最重要的是,为其附加键盘快捷键。例如“initx”
Then you just start to type the shortcut in line where you want the initialiser to be and voila, you have your custom init.
然后,您只需开始在您希望初始化程序所在的行中键入快捷方式,瞧,您就有了自定义的 init。
回答by morroko
Yes you can initialise an class by custom init method or you can pass parameter when you want to initialise class with custom init method
是的,您可以通过自定义 init 方法初始化类,也可以在要使用自定义 init 方法初始化类时传递参数
look
看
Define in your class.h file
在你的 class.h 文件中定义
@interface CustomView : UIView
- (id)initWithA:(int) aInner andB:(int) bInner
In .m file implement initWithStringMethod.
在 .m 文件中实现 initWithStringMethod。
- (id)initWithA:(int) aInner andB:(int) bInner
{
if((self = [super init]))
return self;
}
initialised class from other class or viewController
从其他类或 viewController 初始化的类
CustomView *cv = [[CustomView alloc] initWithA:5 andB:10 ];