Xcode Beta 6.1和Xcode 6 GM因为奇怪的原因而陷入索引

Wak*_*Wak 5 xcode swift xcode6

我正在开发一个快速的应用程序,在某些时候我有一个类似于此的代码:

 import UIKit

class ViewController: UIViewController {
    private var a: UIImageView!
    private var b: UIImageView!
    private var c: UILabel!
    private var d: UILabel!
    private var e: UILabel!
    private var f: UILabel!
    private var g: UIView!
    private var h: UIView!
    private var i: UIView!
    private var j: UIView!
    private var k: UIImageView!
    private var l: UIView!
    private var m: UIView!
    private var n: UIView!
    private var o: UIView!
    private var p: UIScrollView!
    private var q: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let viewBindingsDict = ["a" : a,
            "b" : b,
            "c" : c,
            "d" : d,
            "e" : e,
            "f" : f,
            "g" : g,
            "h" : h,
            "i" : i,
            "j" : j,
            "k" : k,
            "l" : l,
            "m" : m,
            "n" : n,
            "o" : o,
            "p" : p]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
Run Code Online (Sandbox Code Playgroud)

出于某种原因,当我添加此代码时,xcode卡住了,我无法做任何其他事情.

打开Activity Monitor,它使用100%以上的CPU显示sourcekitservice和swift.

我用上面的代码创建了这个示例项目:https://dl.dropboxusercontent.com/u/1393279/aaaaaaa.zip

我已经尝试过清理派生数据,重新安装Xcode,重新启动,等待分钟等等.它只是不起作用.

Ant*_*nio 18

类似的事情发生在我身上几次,我通过将长语句分成多行来解决它.

我在游乐场测试了你的代码,我立即注意到SourceKitService进程占用了100%的CPU.

在你的代码中,我看到的最长的语句是字典初始化,因此第一种方法是使其变为可变并且每行使用少量项目进行初始化.

Swift没有+=为字典提供运算符,所以我们首先需要一个(kuos to @shucao):

func +=<K, V> (inout left: Dictionary<K, V>, right: Dictionary<K, V>) -> Dictionary<K, V> {
    for (k, v) in right {
        left.updateValue(v, forKey: k)
    }
    return left
}
Run Code Online (Sandbox Code Playgroud)

在您的工具集中,您可以按如下方式初始化字典:

var viewBindingsDict = ["a" : a, "b" : b, "c" : c, "d" : d, "e" : e]
viewBindingsDict += ["f" : f, "g" : g, "h" : h, "i" : i, "j" : j]
viewBindingsDict += ["k" : k, "l" : l, "m" : m, "n" : n, "o" : o]
viewBindingsDict += ["p" : p]
Run Code Online (Sandbox Code Playgroud)

每行最多选择5个项目.

但是在你的代码中你将字典声明为不可变 - swift没有提供任何语句来在声明后初始化一个不可变的 - 幸运的是我们可以使用一个闭包来实现:

let viewBindingsDict = { () -> [String:UIView] in
    var bindings = ["a" : self.a, "b" : self.b, "c" : self.c, "d" : self.d, "e": self.e]
    bindings += ["f": self.f, "g" : self.g, "h" : self.h, "i" : self.i, "j" : self.j]
    bindings += ["k" : self.k, "l" : self.l, "m" : self.m, "n" : self.n,  "o" : self.o]
    bindings += ["p": self.p]
    return bindings
}()
Run Code Online (Sandbox Code Playgroud)

  • 有效.谢谢.我第一次看到这样的事情.Swift显然尚未准备好投入生产. (3认同)
  • 迁移到Xcode 6.1时我有类似的事情.我有一个菜单定义为字典列表列表,如私有let menuData = [[<<我所有的定义>>]],它占用了大约35行代码.Xcode在快速进程中以100%的速度挂在"索引编译快速代码"中.通过动态添加菜单的一小部分来修复!我想我也可以使用Plist,但这不是重点. (2认同)