我在Xcode中遇到了一个问题,错误“源套件服务终止”正在弹出,所有语法高亮显示和代码完成都在Swift中消失了。我该如何解决这个问题?

下面是一个示例图像:


当前回答

在项目中使用Swift以外的其他名称。“Swift”是保留的。

其他回答

报告给苹果公司(#17266321):

细节:

简介: 如果我们试图将值作为数组打印一个字典,则弹出窗口会不断弹出-“SourceKit terminated.”编辑器功能暂时受限”。Xcode冻结并失去上下文识别,导致文本变黑。

复制步骤: 1. 创建一个数组为- "var iOSHistoryArray = ["iOS6","iOS7","iOS8",]"

创建一个字典as -"var MacOSYosemiteFeatures: dictionary = ["Device":iOSHistoryArray]"。 将字典打印为- "println("包含数组值的字典:%@ \n",MacOSYosemiteFeatures)"(< < -罪魁祸首——> >) 第3步导致了注释问题,这使得Xcode再次起作用。

预期结果: Xcode应该能正常工作。

实际结果: Xcode变得无功能,失去上下文识别(所有字体丢失,所有文本变成纯黑色),整个Xcode变成非活动。

版本: 版本6.0 (6A215l)

Swift试图动态地而不是静态地找出所有内容的类型,但当它处理复杂类型(如链式过滤器、映射、flatMap调用)或大型字典时,这可能需要付出巨大的努力。有时你需要把它们分开,以便编译器更容易分析。这个问题的一个表亲是“太复杂而不能及时评估”的错误。想象一下,有几个几乎太复杂而无法及时评估的项目:它们一起变成了资源占用者。

试着找到所有您创建动态字典或使用无类型调用链的地方。尝试更具体地输入,特别是在变量声明处。

一些简单的例子:

字典

Bad:

let myDict = [ /* bunch of different things */ ]

好:

let myDict: [String: AnyObject] = [ /* bunch of different things */ ] 

最好的:

let myDict: [String: SpecificType] = [ /* bunch of similar things */ ]

映射

Bad:

let filteredUserIds = users.filter({ user in user.enabled })
    .flatMap { user in user.name != nil ? return user.id : return nil }

好:

let filteredUserIds: [Int] = users.filter({ user in user.enabled })
    .flatMap { user in user.name != nil ? return user.id : return nil }

最好的:

let enabledUsers: [User] = users.filter { user in user.enabled } 
let filteredUserIds: [Int] = enabledUsers.flatMap { 
    user in user.name != nil ? return user.id : return nil 
}

好处还包括额外的检查代码,因为您总是会得到预期的类型或编译器错误。缺点是它开始看起来有点像Java。

当然,随着项目的发展,这个问题首先会积累起来。小型Swift项目总是很快。所以可能有很多工作要做。经验告诉我们哪些表达是复杂且耗时的。如果你能对这些事情进行基准测试,那就太好了。

将扩展移动到主体类之前。

extension SomeType {
// new functionality to add 

}

我相信我找到了一个更通用的解决方案。下面是我用来鼓励Xcode不要产生SourceKitService Terminated错误的步骤。


我的症状是:

When I would start up a new playground, I would receive an error about not being able to communicate with the playground (Error running playground: Failed prepare for communication with playground. See this image on twitter. When I would switch the playground from OS X to iOS, I would receive another error (unfortunately I did not write that one down). When I would start to type in an iOS based Swift project, attempting to use code completion/intellisense on any UIKit specific class, I would receive the SourceKitService Terminated issue in this thread.


调试过程:

我开始通过谷歌寻找SourceKitService,这得到了很少。 然后我开始监视控制台。应用程序,同时使用Xcode。这显示了几个错误: IDEPlaygroundDocument:运行游乐场时遇到错误 com.apple.CoreSimulator。CoreSimulatorService[3952]: The runtime for The selected device is not installed。


我做了什么来纠正这个问题。

如果您只是在Swift项目上下文中遇到问题,请先单独尝试一下。如果这不起作用,那么尝试下面的所有步骤。

打开项目并将目标的部署目标更改为<= 7.1。

更漫长和复杂的过程。 (前3步不一定有帮助,但我做到了,所以把它们记录在这里)

Completely delete all copies of Xcode on your system. Restart your computer. Reinstall Xcode6-beta only. Verify that you still have the issue in playground and/or projects. Open iOS Simulator. Hardware -> Device -> Manage Devices Remove all devices. Recreate all devices you want. I appended the iOS version to the end of the name, just because. Restart Xcode and the simulator. Verify that at least playgrounds no longer throw issues when switched from OS X to iOS. Open your project and change the target's deployment target to something <= 7.1.


分析

看来问题是Xcode6不能正确地找到并连接到模拟器。我不知道为什么会出现这种情况,但这让我可以继续使用Swift进行开发。这可能与模拟器二进制文件似乎移动的事实有关。

将部署目标更改为iOS 8.0对我来说很有效。我知道有人说将它移动到<=7.1可以工作,但这个问题似乎来自多个来源,所以这个替代方案可能可以工作。