如何在 Xcode 中创建自定义的“文本宏”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5013332/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:14:42  来源:igfitidea点击:

How to create custom "Text Macros" in Xcode?

xcodemacros

提问by EmptyStack

How to create custom code completion macros in Xcode. I googled and find some methods, but I don't understand them completely. So can anyone tell me how to do it?

如何在 Xcode 中创建自定义代码完成宏。我用谷歌搜索并找到了一些方法,但我并不完全理解它们。所以有人能告诉我怎么做吗?

Thanks everyone..

谢谢大家..

采纳答案by EmptyStack

This post from ablepearhas a tutorial to add custom Text Macros. The following are the necessary steps, the tutorial specifies, to create a custom text macro for Objective - C.

这篇来自ablepear 的帖子有一个添加自定义文本宏的教程。以下是教程指定的必要步骤,用于为 Objective - C 创建自定义文本宏。

  1. Go to your Xcode application file (root/Developer/Applications/). Right-click(control-click) and Show Package Contents. Navigate to (Contents/PlugIns/TextMacros.xctxtmacro/Contents/Resources/). Select the Objective-C.xctxtmarcofile and Copy it (command-c). Open a new Finder window and select your Home folder. Navigate to (Library/Application Support/Developer/Shared/Xcode/). In the Xcode folder Paste (command-v) the Objective-C.xctxtmacro file.

  2. Open Objective-C.cxtxtmacro. It contains an Array with around 26 items, each of which is a Dictionary. Click on the "+"symbol/tab to the right of the selected cell. This will add a new Item to the plist root Array which will be our new entry (text macro definition).

  3. Select the new Item and change the Type from String to Dictionary. Now tap on the disclosure triangle (the left side of the selected cell), this will rotate the triangle from pointing right (collapsed) to pointing down (expanded). You may also notice that the "+" symbol to the right changes into a set of lines when the Item is expanded. This let's us add "children" name/value pairsto our new Item.

  4. There are a few "children" name/value pairs we need to add to our new Item to make it function and they are as follows:

    • Identifier- This describes the macro's language (parent).identifier.
    • BasedOn- This is the (parent) language (objc).
    • IsMenuItem- Boolean value. This creates a menu item in Edit menu.
    • Name- The name listen in the (above) menu item.
    • TextString- The actual string that will be inserted via the text macro.
    • CompletionPrefix- This what you type in as the key for the text macro.
  5. Add the values to the keys. A typical macro looks like this (ex:NSLog text macro).

    • Identifier - objc.flog
    • BasedOn - objc
    • IsMenuItem -YES
    • Name - Function (NSLog)
    • TextString - NSLog(@"FUNCTION: %s", _FUNCTION_);
    • CompletionPrefix - flog

    You can name your Identifier and CompletionPrefix whatever you'd like, as long as it does not conflict with any existing completion identifier. Here. flog, is used for Function Log.

  1. 转到您的 Xcode 应用程序文件(root/Developer/Applications/)右键单击按住Control 单击)并显示包内容。导航到(Contents/PlugIns/TextMacros.xctxtmacro/Contents/Resources/)。选择Objective-C.xctxtmarco文件并复制它(command-c)。打开一个新的 Finder 窗口并选择您的主文件夹。导航到(Library/Application Support/Developer/Shared/Xcode/)。在 Xcode 文件夹中粘贴(command-v)Objective-C.xctxtmacro 文件。

  2. 打开 Objective-C.cxtxtmacro。它包含一个包含大约 26 个项目的 Array ,每个项目都是一个Dictionary。单击所选单元格右侧的“+”符号/选项卡。这将向 plist 根数组添加一个新项目,这将是我们的新条目(文本宏定义)。

  3. 选择新 Item 并将 Type 从String更改为 Dictionary。现在点击显示三角形(所选单元格的左侧),这会将三角形从向右(折叠)旋转到向下(展开)。您可能还会注意到,当展开 Item 时,右侧的“+”符号会变成一组行。这让我们将“子项”名称/值对添加到我们的新项目中。

  4. 我们需要将一些“子级”名称/值对添加到我们的新 Item 中以使其起作用,它们如下所示:

    • 标识符- 这描述了宏的语言(父)。标识符。
    • 基于- 这是(父)语言(objc)。
    • IsMenuItem- 布尔值。这将在“编辑”菜单中创建一个菜单项。
    • 名称- 在(上方)菜单项中收听的名称。
    • TextString- 将通过文本宏插入的实际字符串。
    • CompletionPrefix- 这是您作为文本宏键键入的内容。
  5. 将值添加到键。一个典型的宏看起来像这样(例如:NSLog 文本宏)。

    • 标识符 - 对象。鞭打
    • 基于 - 对象
    • IsMenuItem -YES
    • 名称 -函数 (NSLog)
    • TextString - NSLog(@"FUNCTION: %s", _ FUNCTION_);
    • CompletionPrefix - flog

    您可以随意命名您的 Identifier 和 CompletionPrefix,只要它不与任何现有的完成标识符冲突。这里。flog,用于函数日志。

Read the post in the above link, to understand in completely.

阅读上面链接中的帖子,以完全理解。



IMPORTANT UPDATE:It seems the above v macro doesn't work in Xcode 3.2. To make it work we have to add the key,

重要更新:上面的 v 宏似乎在Xcode 3.2 中不起作用。为了使它工作,我们必须添加密钥,

OnlyAtBOL = YES; // or NO 

to each macro definition in your xctxtmacro file. This key specifies that the macro works only at Beginning Of Line, or not at the the beginning of line ie., works only after the beginning of line. So the flogmacro will look like this.

到 xctxtmacro 文件中的每个宏定义。此键指定宏仅在行首处工作,或不在行首,即仅在行首之后工作。所以flog宏看起来像这样。

{
    Identifier = objc.flog;
    BasedOn = objc;
    OnlyAtBOL = YES;
    IsMenuItem = YES;
    Name = "Function (NSLog)";
    TextString = "NSLog(@"FUNCTION: %s", _FUNCTION_)";
    CompletionPrefix = "flog";
}


I hope this will be of some help to someone in future.

我希望这会对将来的某人有所帮助。