Swift 的 Xcode 7.3 语法高亮和代码完成问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36165865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 7.3 Syntax Highlighting and Code Completion issues with Swift
提问by aeskreis
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.
我在使用 XCode 7.3(但是,自从我安装 XCode 7.2 以来这个问题一直存在)和 Swift 代码遇到了一个非常令人沮丧的问题,我希望其他人也遇到过这个问题并知道如何解决它。语法高亮和代码完成在 Objective-C 文件中工作得非常好,在 Swift 代码中调用其他 Swift 对象时也能正常工作。但是,Swift 代码中提到的任何 Objective-C 对象或方法都没有语法高亮显示,并且 XCode 不会完成任何 Objective-C 声明的方法或属性。一切都编译并运行得很好。
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).
我还应该补充一点,我也尝试过完全干净地安装 XCode。我删除了所有派生数据,删除了所有 XCode 缓存,并删除了我的 XCode 首选项文件(除了在重新安装之前明显删除了 XCode.app 存档之外)。
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.
这使得在 Swift 中开发变得极其困难。我不想这样做,但如果我找不到解决此问题的方法,我将被迫重新使用 Objective-C。
采纳答案by aeskreis
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.
所以问题似乎出在 CocoaPods 上。我使用 Cocoapods 作为静态库而不是框架。切换到框架(use_frameworks!
在我的 Podfile 中使用)并将库导入 Swift 解决了我所有的问题。我猜所有这些第三方库头文件对于 XCode 来说太多了。不管怎样,这个问题现在已经解决了。我希望这对未来的人有所帮助。
回答by Galvin
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.
我也有同样的问题。不过最后解决了。我做了两个更改,不确定哪个是关键点,但您可以全部尝试。
- 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 Xcodeand delete the ~/Library/Developer/Xcode/DerivedData/ModuleCachedirectory.
在与您的项目的派生数据相同的文件夹中是一个模块缓存。当代码完成停止工作时,删除它修复了它。
关闭 Xcode并删除 ~/Library/Developer/Xcode/DerivedData/ModuleCache目录。
- change the Enable Modules value
- 更改启用模块值
Go to the Build Settingsof 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.
转到目标的构建设置,然后搜索启用模块
如果是 Yes,请将其更改为No,您可能会遇到一些构建错误,只需将其更改回Yes 即可。
After two steps above you should Clean(Shift+Command+K)your project.
完成上述两个步骤后,您应该清理(Shift+Command+K)您的项目。
For now you may fixed the problem.
现在你可以解决这个问题。
回答by Phil Niedertscheider
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.
在撰写本文时,最新版本的 cocoapods (1.0.0.beta.8) 要求您为每个 Xcode 目标定义 pod。
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.
就我而言,我为项目目标和测试目标编译了一个类。由于懒惰,我只向主要目标添加了一个 pod。
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>>
现在在类的代码中工作,A
我使用添加了 pod 框架import NAME
并尝试使用框架的类。Xcode 不会突出显示我使用新类的特定代码,但编译和运行工作正常。在完成对话框中,变量的类型是<<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.
解决这个问题的方法:在Podfile中将新添加的pod添加到所有target中,该类A
是其成员。
Now Xcode finds the necessary frameworks for all targets and code highlighting works again.
现在 Xcode 为所有目标找到了必要的框架,代码高亮再次起作用。
EDIT 1:
编辑 1:
A possible solution is defining a list of shared pods, like in my example:
一个可能的解决方案是定义一个共享 pod 列表,就像我的例子一样:
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.
这会将所有 Pod 添加到所有目标,并将所有测试 Pod 添加到目标。在这些旁边,我将“RxTest”添加到 AppUITests。
(Chosen pods are examples of my projects, no advertising intended :-) )
(选择的豆荚是我的项目的例子,没有广告意图:-))
回答by Marius
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.
我们在一个混合的 ObjC/Swift 项目中遇到了同样的问题。尝试了有关删除派生数据等的所有建议,但无济于事。有时它有帮助,但不是以可重复的方式,一段时间后它停止工作。Galvin 在这篇文章中的帖子让我走上了与模块相关的构建设置的轨道。然而,这是另一种以可重现的方式解决代码完成/着色的设置:将 DEFINES_MODULE(在打包下)从 YES 设置为我们的主项目的 NO 是解决方案。
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.
- 我预计这会破坏我们项目中的 ObjC/Swift 互操作性,但这仍然有效。似乎设置仅用于框架目标。( https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html)
- 这个项目设置已经几个月没有改变了,但代码补全问题最近才出现,对我和我的同事都是如此。
回答by Scott Fister
If none of the above worked for youand you're using Cocoapods, you can try switching to Carthage.
如果以上方法都不适合您并且您使用的是 Cocoapods,则可以尝试切换到 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).
我尝试了所有可以在 Google 上找到的建议都无济于事。但一直以来,Cocoapods 似乎成为许多试图修复它的黑客的原因。我一直在阅读 Carthage,以及它如何不修改您的项目、强制您使用工作区或可能用头文件填充您的构建文件夹(这会混淆 Xcode 并可能导致语法突出显示和自动完成中断)。
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 postreally drove it home for me.
进行切换后,我还没有遇到任何问题,老实说,我更喜欢少量开销以获得干净的解决方案。这篇文章真的让我把它带回家了。