Xcode 7.3 Syntax Highlighting and Code Completion issues with Swift

旧城冷巷雨未停 提交于 2019-11-29 22:09:38

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!