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?


Solution 1:

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 :)

Solution 2:

Still happens to me occasionally on Xcode 4.3.2,

I simply do this:

Xcode > Organiser > Projects > Select your project > Derived Data > Click the delete button

This also deletes the indexing. I then usually restart Xcode and it starts re-indexing, and I get my syntax highlighting and autocompletion back again. (I don't even always restart Xcode - but an excuse to have a break from starting at a screen for hours is always a good thing for your glazballs!)

It happens that rarely, that this is relatively painless.