Xcode Beta 6.1 和 Xcode 6 GM 由于奇怪的原因卡住索引 [英] Xcode Beta 6.1 and Xcode 6 GM stuck indexing for weird reason
问题描述
我正在开发一个 swift 应用程序,在某些时候我有一个类似于这样的代码:
I'm developing a swift application that at some point I have a code similar to this:
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.
}
}
由于某种原因,当我添加这段代码时,xcode 卡住了,我无法做任何其他事情.
For some reason, when I add this code, xcode gets stuck and I can't do anything else.
打开活动监视器,它会显示使用超过 100% CPU 的 sourcekitservice 和 swift.
Opening the Activity Monitor, it displays sourcekitservice and swift using more than 100% CPU.
我用上面的代码创建了这个示例项目:https://dl.dropboxusercontent.com/u/1393279/aaaaaaa.zip
I've created this sample project with the code above : https://dl.dropboxusercontent.com/u/1393279/aaaaaaa.zip
我已经尝试清理派生数据、重新安装 Xcode、重新启动、等待几分钟等.但它不起作用.
I've already tried cleaning derived data, reinstalling Xcode, rebooting, waiting minutes, etc. It just doesn't work.
推荐答案
类似的事情发生在我身上几次,我通过将长语句分成多行解决了这个问题.
Something similar happened to me a few times, and I solved it by splitting long statements into multiple lines.
我在操场上测试了您的代码,我立即注意到 SourceKitService 进程占用了我 100% 的 CPU.
I tested your code in a playground, and I immediately noticed the SourceKitService process eating 100% of my CPU.
在您的代码中,我看到最长的语句是字典初始化,因此第一种方法是使其可变并使用每行少量项目进行初始化.
In your code the longest statement I see is the dictionary initialization, so a first approach would be to make it mutable and initialize with a short number of items per line.
Swift 没有为字典提供 +=
运算符,所以我们首先需要一个(感谢 @shucao):
Swift doesn't provide a +=
operator for dictionaries, so we first need one (kudos 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
}
在您的工具集中使用它,您可以按如下方式初始化字典:
With that in your toolset, you can initialize the dictionary as follows:
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]
每行最多选择 5 个项目.
choosing a max of 5 items per line.
但是在您的代码中,您将字典声明为不可变的 - swift 没有提供任何语句来在声明之后初始化不可变的 - 幸运的是我们可以使用闭包来实现:
But in your code you declared the dictionary as immutable - swift doesn't provide any statement to initialize an immutable after its declaration - fortunately we can use a closure to achieve that:
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
}()
这篇关于Xcode Beta 6.1 和 Xcode 6 GM 由于奇怪的原因卡住索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!