Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 7.3 Syntax Highlighting and Code Completion issues with Swift

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.

like image 614
aeskreis Avatar asked Mar 22 '16 21:03

aeskreis


4 Answers

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.

like image 93
Galvin Avatar answered Nov 18 '22 17:11

Galvin


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.

like image 8
aeskreis Avatar answered Nov 18 '22 16:11

aeskreis


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

like image 6
Phil Niedertscheider Avatar answered Nov 18 '22 15:11

Phil Niedertscheider


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:

  • I expected this to break the ObjC/Swift interoperability in our project, but that still works. It seems that setting is only to be used for framework targets. (https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html)
  • This project setting has not been changed for months, but the code completion issues came up only recently, both for my colleague and me.
like image 3
Marius Avatar answered Nov 18 '22 16:11

Marius