网页
综合
更新时间 2026-11-14 01:37:05
Generate code | AppCode Documentation
自动蜘蛛
www.jetbrains.com
正文约 4529 字
AppCode provides multiple ways to generate common code constructs and recurring elements, which helps you increase productivity. These can be either file templates used when creating a new file, custom or predefined live templates that are applied differently based on the context, various wrappers, or automatic pairing of characters.
Additionally, AppCode provides code completion and Emmet support.
From the main menu, select Code | Generate Alt+Insert to open the popup menu with available constructs that you can generate.
AppCode allows you to generate initializers for your classes or actors and select initialization parameters.
On the Code menu, click Generate Alt+Insert.
In the Generate popup, click Initializer for Swift or initWith for Objective-C for Kotlin.
If the class contains fields, select the fields to be initialized and click OK.
The following code fragment shows the result of generating an initializer for a class:
From the Generate menu, you can add code for the description and debugDescription properties. With the caret placed inside the class you want to add these properties to, press Alt+Insert and select description or debugDescription. In the dialog that opens, select properties that should be added to the description. In the Template field, select how the property implementation should look like:
AppCode can generate overrides for standard equals and hash methods.
Swift: equals and hash
Objective-C: equals and hash
From the Code menu, click Generate Alt+Insert.
In the Generate popup, click equals and hashValue (for Swift) or -isEqual: and -hash (for Objective-C) .
Select a velocity template from the Template list.
Select the fields that should be used to determine equality, and click Next.
Select the fields to use for calculating the hash code value. You can choose only from fields that were selected on the previous step (for determining equality). Click Next.
Here is an example of the generated code:
You can override any method of a parent class by generating the corresponding method stub in a child class.
Perform the following steps to do this:
Place a caret inside a child class and do one of the following:
On the Code menu, click Override methods (Ctrl+O).
On the Code menu, click Generate (Alt+Insert) and select Override methods.
Choose the desired method in the opened dialog.
Implement the created method.
Use the button in the left gutter to open the overridden method declaration.
If a class is declared as implementing an interface or an abstract class, it has to implement the methods of the parent class or the base interface. AppCode creates stubs for implemented methods.
Select Generate from the context menu of a class Alt+Insert and select Implement Methods from the popup or just press Ctrl+I.
In the dialog that opens, select the methods to implement and click OK.
You can generate an empty stub for a property that you want to use but have not yet defined.
To do this, perform the following steps:
Type a name that references a non-existent property. AppCode highlights the reference.
Press Alt+Enter, choose the Create property '<name>' from the suggestion list, and press Enter.
AppCode provides a number of predefined live templates for many common code constructs. You can also define custom templates to cover use cases specific to your workflow.
You can create code constructs by inserting and expanding live templates as well as wrap fragments of code using surround templates.
Place the caret at the place where you want the template to expand.
Type the template abbreviation and press the invocation key (Tab by default).
Alternatively, to use code completion, press Ctrl+J or select Insert Live Template from the context menu and select the required template from the suggestion list. To view Quick Documentation for the selected suggestion, press Ctrl+Q.
If the selected template requires user input, the first input field is marked with a red frame. Type your value in this frame and press Enter or Tab to complete input and pass to the next input field. After completing the last input field, the caret moves to the end of the construct, and the editor returns to the regular mode of operation.
You can surround your code just with those live templates where the $SELECTION$ variable is used. This variable indicates the code that should be wrapped. To add the variable to a template, go to Preferences | Editor | Live Template, select your language and a template, and place the variable at the desired position in the Template text field.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。