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.

4

5 回答 5

22

我也有同样的问题。但最终解决了。我做了两个更改,不确定哪个是关键点,但您可以全部尝试。

  1. 删除模块缓存

在与项目的派生数据相同的文件夹中是模块缓存。当代码完成停止工作时,删除它修复了它。

关闭 Xcode并删除 ~/Library/Developer/Xcode/DerivedData/ModuleCache目录。

  1. 更改启用模块值

转到目标的构建设置,然后搜索启用模块

如果是Yes,将其更改为No,您可能会遇到一些构建错误,只需将其更改回Yes

在上述两个步骤之后,您应该清理(Shift+Command+K)您的项目。

现在你可以解决这个问题。

于 2016-03-23T04:13:47.200 回答
8

所以看来问题出在 CocoaPods 上。我使用 Cocoapods 作为静态库而不是框架。切换到框架(use_frameworks!在我的 Podfile 中使用)并将库导入 Swift 解决了我的所有问题。我猜所有这些第三方库头对于 XCode 来说都太多了。不管怎样,现在问题已经解决了。我希望这对将来的某人有所帮助。

于 2016-03-25T16:35:29.907 回答
6

这可能不再需要了,但我仍然想发布这个:

在撰写本文时,最新版本的 cocoapods (1.0.0.beta.8) 要求您为每个 Xcode 目标定义 pod。

就我而言,我为项目目标和测试目标编译了一个类。由于懒惰,我只向主要目标添加了一个 pod。

现在在类的代码中工作,A我添加了 pod 框架,import NAME并尝试使用框架的类。Xcode 不会突出显示我使用新类的特定代码,但编译和运行工作正常。在完成对话框中,变量的类型是<<error type>>

解决这个问题的方法:在 Podfile 中将新添加的 pod 添加到所有目标中,该类A是其成员。

现在 Xcode 为所有目标找到了必要的框架,并且代码高亮再次工作。

编辑1:

一个可能的解决方案是定义一个共享 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

这会将所有 pod 添加到所有目标,并将所有测试 pod 添加到目标。在这些旁边,我将“RxTest”添加到 AppUITests。

(选择的豆荚是我的项目的例子,没有广告意图:-))

于 2016-04-27T14:31:38.633 回答
3

我们在混合的 ObjC/Swift 项目中遇到了同样的问题。尝试了有关删除派生数据等的所有建议,但无济于事。有时它有所帮助,但不是以可重现的方式,一段时间后它停止工作。Galvin 在这篇文章中的帖子让我了解了与模块相关的构建设置。然而,这是另一个以可重现的方式解决代码完成/着色的设置:将 DEFINES_MODULE(在 Packaging 下)从 YES 设置为 NO 是解决方案。

笔记:

于 2016-03-25T12:06:24.280 回答
0

如果上述方法都不适合您并且您正在使用 Cocoapods,您可以尝试切换到 Carthage。

我尝试了所有可以在 Google 上找到的建议,但均无济于事。但是 Cocoapods 似乎一直是出现的原因,许多黑客试图修复它。我一直在阅读 Carthage,以及它如何不修改您的项目、强制您使用工作区或可能用头文件填充您的构建文件夹(这会使 Xcode 混淆并可能导致语法突出显示和自动完成中断)。

进行切换后,我还没有遇到任何问题,老实说,我更喜欢少量的开销来获得一个干净的解决方案。这篇文章真的把它带回家了。

于 2016-11-27T19:34:41.877 回答