如何让 Xcode 将自定义文件扩展名识别为 Objective-C 以进行语法突出显示?
我想让 Xcode 4 将自定义文件扩展名(例如 *.lx)识别为 Objective-C,以实现语法突出显示和缩进目的。如何让工具自动执行此操作?
I'd like to get Xcode 4 to recognize a custom file extension (e.g. *.lx) as Objective-C for syntax highlighting and indentation purposes. How do I get the tool to automatically do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Xcode 根据文件的统一类型标识符。据我所知,不可能向现有 UTI 添加额外的文件扩展名标记,但您可以声明一个符合您要映射到的类型的新 UTI。然后,系统会将指定的文件扩展名与您的新 UTI 相关联,并通过一致性 Xcode 和每个其他 UTI 感知应用程序将这些文件识别为映射类型的源代码。
您可能需要考虑一下在哪里向新的 UTI 进行申报。例如,如果这种类型的文件是由工具创建的,则该工具的捆绑包将是最合适的位置。如果没有更好的替代方案,您可以创建一个存根应用程序包并在那里声明新的 UTI:
com.yourdomain.objective-c-source
。public.objective-c-source
。您可以通过浏览系统声明的 UTI< /a> 或在 Xcode 的 Info.plist 中导出的内容。Xcode 现在应该对具有指定扩展名的文件使用适当的语法突出显示。
如果这不起作用,请检查构建的应用程序的 Info.plist,以确保所有预期信息都在那里,没有任何尾随空格。您还可以使用
lsregister
检查 UTI 是否已注册:在输出中搜索 UTI 标识符并验证它是否存在且处于活动状态。
Xcode determines how to represent a file in its user interface based on the file's Uniform Type Identifier. As far as I know it's not possible to add additional file extension tags to an existing UTI, but you can declare a new UTI that conforms to the type you want to map to. The system will then associate the specified file extension(s) with your new UTI and through conformance Xcode and every other UTI-aware application will recognize the files as source code of the mapped type.
You may want to give some thought to where to declare to the new UTI. For example, if files of this type are being created by a tool the bundle for that tool would be the most appropriate location. In the absence of a better alternative you can create a stub application bundle and declare the new UTI there:
com.yourdomain.objective-c-source
.public.objective-c-source
. You can find this by browsing the list of system-declared UTIs or those exported in Xcode's Info.plist.Xcode should now use appropriate syntax highlighting for files with the specified extension(s).
If this doesn't work, check the Info.plist of the built application to ensure that all of the expected information is there without any trailing whitespace. You can also check that the UTI has been registered using
lsregister
:Search the output for your UTI's identifier and verify that it is present and active.
Xcode 在“属性检查器”(“实用程序”右侧边栏中最左侧的选项卡)中为项目中的文件提供了一个“类型”字段:
< img src="https://i.sstatic.net/93PpP.png" alt="身份和类型设置">
正如您在上面看到的,我将项目的
Fastfile
设置为是被识别为 Ruby 脚本——确实如此,尽管缺少预期的.rb
扩展名。产生预期的语法突出显示(使用我的自定义配色方案):注意:仅当文件添加到组内的项目(而不是文件夹内)时,此选项才有效参考):
这可能并不理想,但 Xcode 无法保留项目文件中未明确跟踪的文件的属性。根据设计,文件夹在 Xcode 项目中没有其内容条目,因为它是动态的。
Xcode has a "Type" field in the "Attributes Inspector" (leftmost tab in "Utilities" right sidebar) for files in your project:
As you can see above, I set our project's
Fastfile
to be recognized as a Ruby Script—which it is, despite lacking the expected.rb
extension. Yielding the expected syntax highlighting (with my custom color scheme):Note: this only works if the file is added to your project within a group (not inside a folder reference):
This might not be ideal, but Xcode can't persist attributes of files which aren't explicitly tracked in the project file. Folders, by design, don't have entries for their contents in the Xcode project, since it's meant to be dynamic.
在 Xcode 中,当您的文件在编辑器中处于活动状态时,您可以转到编辑器菜单 ->语法着色 -> Objective-C
这会将 Objective-C 语法着色应用于文件。为我使用纯文本文件,无论其扩展名如何。
这似乎也适用于自动缩进
In Xcode when your file is active in the editor, you go to the Editor Menu -> Syntax Coloring -> Objective-C
This will apply objective-c syntax coloring to a file. Worked for me with a plain text file regardless of its extension.
This seemed to work for automatic indentation also