Xcode 7.3 Syntax Highlighting and Code Completion issues with Swift
Asked Answered
H

5

37

I am having an extremely frustrating issue with XCode 7.3 (however, this issue has persisted since I installed XCode 7.2) and Swift code, and I am hoping others have had this issue and know how to resolve it. Syntax highlighting and code completion work perfectly fine in Objective-C files, and also works fine when calling other Swift objects within Swift code. However, any Objective-C objects or methods mentioned in Swift code get no syntax highlighting, and XCode will not complete ANY Objective-C declared methods or properties. Everything compiles and runs just fine.

I should also add that I have also tried doing a completely clean install of XCode. I deleted all my derived data, deleted all XCode caches, and deleted my XCode preferences files (in addition to obviously deleting the XCode.app archive before re-installing).

It is making it extremely difficult to develop in Swift. I don't want to do this, but if I can't find a way to resolve this I'll be forced to go back to using Objective-C.

Halide answered 22/3, 2016 at 21:46 Comment(5)
Make a new user and see if the problem occurs there.Unnamed
What do you mean by a new user? You mean a new account on my computer?Halide
Yes. I'm wondering whether using Xcode with a whole new clean user will solve it. If it does, it will prove that the old user is hosed in some way.Unnamed
I have several co-workers on this project and they all experience the same issue. We are hypothesizing that it is due to the size of our project (it is quite large, over 400 files, with a dozen third party libraries), and the fact that we include many files and libraries in our bridging header. This may just be an issue with XCode being overloaded by the number of symbols, and something that we have to deal with until they make improvements. We will likely go back to using Objective-C until Apple can resolve these issues.Halide
Thanks, that's interesting info.Unnamed
H
8

So it seems the issue was with CocoaPods. I was using Cocoapods as a static library instead of as frameworks. Switching to frameworks (using use_frameworks! in my Podfile) and importing the libraries into Swift has resolved all my issues. I'm guessing all those third party library headers were just too much for XCode to process. Either way, the issue is now resolved. I hope this helps someone in the future.

Halide answered 25/3, 2016 at 16:35 Comment(5)
Sorry to disappoint. But this is not a cocoapods issue. I can say this with authority because I have the same issue in a pure Objective-C project and I do not use cocoa pods in any of my projects and never will.Peonage
@Peonage there are several reasons this error can occur, my answer is just the solution that solved my specific problem. I suggest you read the other answers to see if any of them can help you. Also this question is regarding Swift. If you do not use Swift, the anwers may not apply to you. Best of luck.Halide
This is the only thing that has actually worked for me as a consistent solution. I now have code completion without the continual need to clean the project.Macerate
Upgrading the Podfile and using frameworks worked for me as well. Great answer!Laverne
Ye, solved it for me, just added use_frameworks! under platform :iOS and all is good!Favour
E
22

I have the same problem. But finally solved it. I make two change, not sure which is the key point but you can try them all.

  1. delete the module cache

Within the same folder as your project's Derived Data is a Module Cache. When Code Completion stopped working, deleting this fixed it.

Close Xcode and delete the ~/Library/Developer/Xcode/DerivedData/ModuleCache directory.

  1. change the Enable Modules value

Go to the Build Settings of your target, then search Enable Modules

If it's Yes, change it to No, and you may get some build error, just change it back to Yes.

After two steps above you should Clean(Shift+Command+K) your project.

For now you may fixed the problem.

Eerie answered 23/3, 2016 at 4:13 Comment(9)
Unfortunately neither of these tricks seems to have worked for me. Thank you for the effort though, and I'm sure this may help someone else.Halide
Works for me, but I have to clean after each build to restore highlighting and auto-complete functionalitiesDeflation
Deleting the module cache and doing a clean and clean build folder did it for me, thanks!Harbert
Lol and it's broken again. Xcode being Xcode, some things never change...Harbert
@Eerie I'm on 8.1... Unfortunately it seems like the Xcode team is incapable of getting code introspection and autocomplete features to work reliably. I swear I've had these issues since maybe Xcode 4 or 5. Never gets better.Harbert
@BenBaron maybe try switching to frameworks(using use_frameworks! if you using CocoaPods). But seems like the Xcode can't handle too complex project.Eerie
@Eerie I use Carthage rather than CocoaPods and so I'm already using all of my subprojects as frameworks. Unfortunately, I think you're right, anything more than pretty much a Calculator app and Xcode just chokes. The excuse used to be that it's Objective-C and they can only make the IDE so good, but I'm using almost exclusively Swift and it's just as bad as always so I guess it's just that Xcode sucks.Harbert
@BenBaron I think, The only way to solve it is use the AppCode. A bit slow and not all features supported. But better refactor and coding functions.Eerie
@Gavin I finally figured it out! I had a second target created for beta builds. Once I removed that target, all of my issues seem to have disappeared. So it looks like Xcode just can't handle multiple build targets of the same app right now.Harbert
H
8

So it seems the issue was with CocoaPods. I was using Cocoapods as a static library instead of as frameworks. Switching to frameworks (using use_frameworks! in my Podfile) and importing the libraries into Swift has resolved all my issues. I'm guessing all those third party library headers were just too much for XCode to process. Either way, the issue is now resolved. I hope this helps someone in the future.

Halide answered 25/3, 2016 at 16:35 Comment(5)
Sorry to disappoint. But this is not a cocoapods issue. I can say this with authority because I have the same issue in a pure Objective-C project and I do not use cocoa pods in any of my projects and never will.Peonage
@Peonage there are several reasons this error can occur, my answer is just the solution that solved my specific problem. I suggest you read the other answers to see if any of them can help you. Also this question is regarding Swift. If you do not use Swift, the anwers may not apply to you. Best of luck.Halide
This is the only thing that has actually worked for me as a consistent solution. I now have code completion without the continual need to clean the project.Macerate
Upgrading the Podfile and using frameworks worked for me as well. Great answer!Laverne
Ye, solved it for me, just added use_frameworks! under platform :iOS and all is good!Favour
C
6

This might not be necessary anymore but i still want to post this:

At the time of this post, the most recent version of cocoapods (1.0.0.beta.8) requires you to define pods for each Xcode target.

In my case I had a class compile for the project target and for a testing target. I added a pod only to the main target, because of laziness.

Now working in the code of class A I added the pod framework using import NAME and tried to use the classes of the framework. Xcode wouldn't highlight the particular code where I use the new classes, but compiling and running works fine. In the completion dialog the type of the variable was <<error type>>

The way to resolve this issue: in the Podfile add the newly added pod to all targets, the class A is member of.

Now Xcode finds the necessary frameworks for all targets and code highlighting works again.

EDIT 1:

A possible solution is defining a list of shared pods, like in my example:

platform :ios, '8.4'
use_frameworks!
inhibit_all_warnings!

def all_pods
    pod 'MPMessagePack'
    pod 'SwiftyDispatch'
    pod 'BFKit'
    pod 'Timepiece'
    pod 'Alamofire'
    pod 'AlamofireSwiftyJSON'
end

def testing_pods
    pod 'Quick'
    pod 'Nimble'
end

target 'App' do
    all_pods
end

target 'AppLogicTests' do
    all_pods
    testing_pods
end

target 'AppUITests' do
    pod 'RxTest'
    all_pods
    testing_pods
end

post_install do |installer|
    installer.pods_project.targets.each do |target|
        puts target.name
    end
end

This will add all pods to all targets and adding all testing pods to the targets. Next to these I added 'RxTest' to the AppUITests.

(Chosen pods are examples of my projects, no advertising intended :-) )

Concerned answered 27/4, 2016 at 14:31 Comment(0)
U
3

We had the same issue in a mixed ObjC/Swift project. Tried all the suggestions about deleting derived data etc, to no avail. Sometimes it helped, but not in a reproducible way and after some time it stopped working. The post of Galvin in this post put me on the track of the Module related build settings. However it was another setting that solved the code completion/coloring in a reproducible way: setting DEFINES_MODULE (under Packaging) from YES to NO for our main project was the solution.

Notes:

Uptotheminute answered 25/3, 2016 at 12:6 Comment(1)
Sadly, this setting was already set to "No" on my project. Glad you were able to resolve it though. I will poke around the settings and see if I can find anything.Halide
B
0

If none of the above worked for you and you're using Cocoapods, you can try switching to Carthage.

I tried every suggestion I could find on Google to no avail. But consistently Cocoapods seemed to be coming up as a reason with many hacks in attempt to fix it. I have been reading up on Carthage, and how it doesn't modify your project, force you to use a workspace, or potentially fill your build folder with header files (which confuses Xcode and can cause the syntax highlighting and autocomplete to break).

After making the switch I haven't run into any issues yet, and to be honest I prefer the teensy bit of overhead to have a clean solution. This post really drove it home for me.

Bengurion answered 27/11, 2016 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.