简体   繁体   中英

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.

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.

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.

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

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM