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

下面是一个示例图像:


当前回答

Xcode 6.1 Beta 3:

一旦你在一个Swift文件中定义了一个非私有类型别名(例如typealias Foo = Int),在另一个文件中输入字母c, SourceKitService就会崩溃。

解决方案是要么放弃使用类型别名,等待下一个beta版本,要么将类型别名包装在类定义中:

public class DummyClass {
    public typealias Foo = String
}

像这样使用它:

var myVar:DummyClass.Foo?

其他回答

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

extension SomeType {
// new functionality to add 

}

为了解决这个问题,你的Swift代码可能会有一些奇怪的问题。例如,因为您正在复制和粘贴,所以有多个iboutlet的定义。通常这只是一个无法处理的语法错误。

我在苹果的开发者论坛上找到了一个解决方案(需要登录,所以我也会在这里解释)。

TLDR:不要在Objective-C中导入与@interface同名的@protocol到Swift桥接头。例如,Facebook SDK有一个名为“FBGraphObject”的协议和接口。

以下是论坛帖子:

Adding Facebook SDK causes SourceKitService to crash. If you want to use beta 3 and you need Facebook SDK, one work around that I found and is working for me is refactoring Facebook SDK's @protocol FBGraphObject and renaming it to something like @protocol FBGraphObjectProtocol as an @interface FBGraphObject also exists and apparently SourceKit doesn't like it. It sure is a temporary solution just to keep you functional and you'll have to use Cocoapods or otherwise build Facebook SDK yourself rather than using the framework library. For the time being, you'll need to make sure you don't have conflicting @protocol and @interface declarations and that includes 3rd party libraries you may be using. This cost me today, hope it helps! Posted by e.parto on July 10, 2014

对我来说(xcode 6.1),原因是我忘记采用我的子类协议。

例如,这是错误的:

protocol SomeProtocol { ... }
class A :NSObject, SomeProtocol {
...
}
class B : A {
...
}

这是可以的:

protocol SomeProtocol { ... }
class A : NSObject, SomeProtocol {
...
}
class B : A, SomeProtocol {
...
}

我发现,通过显式地(静态地)键入变量类型,而不是推断它们,为我解决了这个问题。