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
379 views
in Technique[技术] by (71.8m points)

ios - Issue with code autocompletion / syntax highlighting in Xcode 4.x

I am having a rather strange issue in Xcode on one of my projects.

When I start to write my code, I am used to the auto-completion suggesting numerous entries for me. For some reason, since yesterday, this has stopped working. I now get strange auto-completion entries such as "and", "the", "Andy", "MyCompanyName" etc.

This never happened before and so I am confused as to why it has occurred now, and only on one of my projects. I searched this site for quite some time for any fixes and I've attempted to clean Derived Data, quit Xcode, delete the workspace and user files from the Xcode project and restart, but after doing so, the auto-complete works for a while, then breaks again after about 30 seconds of coding.

I can sometimes see auto-complete solutions, but just plain text (no class indicators), then other times it stops working entirely.

Has anybody else experienced anything like this and could provide some solution?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I suffered for a long time with a project that kept losing auto-completion and syntax highlighting. Deleteing the Derived Data folder and restarting Xcode would fix it temporarily, and eventually that almost became muscle memory. My problem seemed to be exacerbated by using a workspace that contained multiple projects, many of which were dependant on each other. It turns out the problem was with the clang indexer. The project would compile cleanly, but looking at the output from the indexer it was littered with errors. Fixing ALL these resolved my problems.

BEFORE DOING ANYTHING: make sure ALL the targets in ALL the projects in your Workspace compile cleanly. That's without ANY warnings. This was a big failure on my part - I had old targets that just didn't compile cleanly. Some of these were unused and could be deleted, others were non-ARC that used modules in targets that had been converted to ARC (converted those targets too), and some just needed the code fixing.

Once you've resolved ALL those problems, the first thing we need to know is where the indexer is failing, by doing a complete index of your project.

  • First, Make sure Xcode is closed, then turn up the indexer warning message level by entering in the Terminal:

     defaults write com.apple.dt.Xcode IDEIndexingClangInvocationLogLevel 3
    
  • Then find the Derived Data folder for your project/workspace (probably ~/Library/Developer/Xcode/DerivedData/your project name-some gibberish), and delete it (but keep the Derived Data folder open because you'll probably have to go back and do this a few times.

  • Next, open the Console app and click Clear Display, and set the filter to "IDE" to only show the messages we're interested in.

  • Finally, open Xcode and the project/workspace that's causing your problems, and you'll see the console start to fill up with messages.

  • To look for errors, change the Console filter to "error:" - our aim is to reduce these to zero.

  • The majority of errors I found were missing header files. Xcode itself seems to be smarter than the indexer. It seems to be able to find and include header files from other projects that the indexer can't.

  • So, anywhere you see "fatal error: 'somefile.h' file not found", make sure you add the path of that file to the User Header Search Paths in the target's build settings. The best way I've found to do this is to double click on the field to get the popup, and drag the folder from the finder into it. This adds a new entry, but assuming it's in the same workspace, it adds a relative file path (this is important in case you move your workspace folder in the future). If you've got multiple targets for the same project, you can set the search path at the project rather than target level, and set the target search paths to $(inherited).

  • You might have similar problems with Libraries or Frameworks - again, update the appropriate search path.

  • Once your fixed some errors, close Xcode, clear the console, delete the project's Derived Data, open Xcode and let it index again.

  • Repeat until all your errors are gone.

Once you're in this good place, code autocomplete and syntax highlighting should work like a charm :)


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

...