24

The code above has correct syntax highlighting in Xcode 7. It is mix of Obj-C + Swift:

enter image description here

I've updated project to support Xcode 8 and only few things was changed:

  • In Build Settings Swift 2.3 support enter image description here

  • And have fixed few errors related to implicitly unwrapped properties in UIKit enter image description here

After all the project is compiled fine for Xcode 8.

But Obj-C code integrated in Swift doesn't have any syntax highlighting and vice versa:

enter image description here

And there is << error type >> problem with autocomplete: enter image description here

Derived data deleting doesn't help, Xcode restart either :) CocoaPods version 0.38.2, iOS 7

Andrey Banshchikov
  • 1,558
  • 2
  • 15
  • 27

9 Answers9

20

This answer helped me https://forums.developer.apple.com/thread/46223 with one of my projects:

I got help from an apple engineer at WWDC on this issue and resolved it. The problem stemmed from cocoapods... Apparently cocoapods was copying .h files into the build directory and SourceKit was getting confused. I'm not entirely sure why the .h files were being copied - they aren't needed there. So the fix was to add a post-build script in your build phases section that removes the headers after a build.
It would look something like this:

function removeHeaders() {  
    find $BUILD_ROOT/Debug-iphonesimulator/  -name '*.h' -exec rm -f {} \;  
}  
removeHeaders  
Andrey Banshchikov
  • 1,558
  • 2
  • 15
  • 27
  • 1
    Thanks for the post helped me alot. – ymutlu Dec 27 '16 at 14:27
  • 1
    [There is now a Cocoapods issue on Github](https://github.com/CocoaPods/CocoaPods/issues/6424) – Endama Mar 01 '17 at 20:39
  • I encountered this issue when converting a Swift 2.3 syntax project to Swift 3.0. Adding that script resolved the issue for me. My project did not have any CocoaPods in it. – Adrian Mar 06 '17 at 19:08
  • 1
    How to add `post-build script`? Is that `target -> Build Phases tab -> Plus button click -> New Run Script Phase` option click ? – Shamsiddin Saidov Mar 07 '17 at 07:01
  • 1
    @Shamsiddin Yes, that's the spot you'll want to put the script. Cut and paste it in there, then press shift+option+command+K to clean caches, then click ctrl+B t rebuild and it should work properly for you after that. If that doesn't do it, try restarting Xcode and re-open your project. – Adrian Mar 07 '17 at 20:34
  • AKA, another reason not to use Cocopods – TheCodingArt May 09 '17 at 13:29
14

Fixed. Problem was related to the presence of target in project which is not compiled. So if you have targets e.g. A, B, C and C is not compiled this cause problems with syntax highlighting.

Andrey Banshchikov
  • 1,558
  • 2
  • 15
  • 27
  • Thanks for the update, I'm happy that you've solved. It continue to happen to me sometimes but it's not related to compiled targets anyway, but I solve every time restarting Xcode. – Massimo Polimeni Sep 23 '16 at 17:14
  • Wow I think this finally fixed it for me! I've been going crazy with Xcode features begin constantly broken ever since I created a second build target recently, but I hadn't put 2 and 2 together. THANK YOU!! – Ben Baron Nov 07 '16 at 19:45
  • This really was my problem. Trying to build all the other targets fingerpointed to broken one and fixing it brought intellisense back. Thanks! – Viktor Kucera Dec 23 '16 at 07:36
8

I have found that when syntax highlighting falls over, switching tabs in Xcode fixes the issue ¯_(ツ)_/¯ sometimes I find I need to switch to at least 4 different tabs before it comes on again.

Otherwise a quit and reopen

Daniel Galasko
  • 23,617
  • 8
  • 77
  • 97
4

It happened also to me with Xcode 8 GM but also lots of time when I used Xcode 7: sometimes the auto-complete feature and the syntax highlighting die without notice.

The only solution that it works for me is restart Xcode but a few times I needed to complete reboot my Mac; I still don't know exactly what causes this annoying problem.

Massimo Polimeni
  • 4,826
  • 4
  • 27
  • 54
3

Setting Use Legacy Swift Language Version to YES in the Build Settings helped me to solve similar issue

Andrey Volobuev
  • 862
  • 8
  • 11
  • 1
    Flipping that to `YES` generated a bunch of compiler errors, but flipping it back to `NO`, cleaning caches, and rebuilding fixed the autocompletion issue for me. – Adrian Mar 06 '17 at 18:46
3

Commenting and then uncommenting the affected lines worked for me.

abc123
  • 8,043
  • 7
  • 49
  • 80
1

In my case:

In Finder I navigated to user->Library->Developer->Xcode->DerivedData. Close your Xcode Project then Clear DerivedData. It's work for me.

Happy Coding...:)

Renish Dadhaniya
  • 10,642
  • 2
  • 31
  • 56
0

For me, code completion/highlighting not working due to the missing file in the Compile Sources under Build Phases. I added that file and it works.

Hope this helps anyone.

iGW
  • 633
  • 5
  • 14
-1

As others have stated above - if I create a new tab ..or 4 then close the tab that usually does it. Sometimes commenting out a line then immediately uncommenting does the trick as well. I got tired of using several keys to accompolish this and made a simple AppleScript to do it for me.

Create a Automator 'service' and add this script to it. In System Preferences under Keyboard -> Shortcuts -> Services find your new service and assign it to an empty function key (I used F1) and now you have a 1 key fix!

on run {input, parameters}

tell application "Xcode"
    activate
    tell application "System Events" to keystroke "t" using command down
    delay (0.5)
    tell application "System Events" to keystroke "w" using command down
    delay (0.5)
    tell application "System Events" to keystroke "/" using command down
    delay (0.5)
    tell application "System Events" to keystroke "/" using command down
    delay (0.5)
end tell


return input
end run
Desh__
  • 899
  • 1
  • 10
  • 16
  • This is viable option that certainly works. I've yet to find a solution that solves this problem 'correctly' so as a temporary fix, whats the problem? 2 birds with 1 stone. – Desh__ Jun 07 '17 at 00:48