Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

How to make Xcode recognize a custom file extension as Objective-C for syntax highlighting?

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?

question from:https://stackoverflow.com/questions/9050035/how-to-make-xcode-recognize-a-custom-file-extension-as-objective-c-for-syntax-hi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  1. Create a new Cocoa Application project in Xcode.
  2. In project settings, select the application target, then the Info tab.
  3. Create a new Exported UTI. UTI definition example
  4. Set the Identifier field to a unique name using reverse DNS notation for a domain that you control. For example, com.yourdomain.objective-c-source.
  5. Set the Conforms To field to the UTI that you want to map to, such as public.objective-c-source. You can find this by browsing the list of system-declared UTIs or those exported in Xcode's Info.plist.
  6. Set the Extensions field to the comma-separated list of extensions that you want to associate with the new UTI.
  7. Commit the change to the last field by pressing return or moving the focus to a different field.
  8. Build and run the application to register it with Launch Services.
  9. Restart Xcode.

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:

/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -dump

Search the output for your UTI's identifier and verify that it is present and active.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...