问题
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.
}
}
For some reason, when I add this code, xcode gets stuck and I can't do anything else.
Opening the Activity Monitor, it displays sourcekitservice and swift using more than 100% CPU.
I've created this sample project with the code above : https://dl.dropboxusercontent.com/u/1393279/aaaaaaa.zip
I've already tried cleaning derived data, reinstalling Xcode, rebooting, waiting minutes, etc. It just doesn't work.
回答1:
Something similar happened to me a few times, and I solved it by splitting long statements into multiple lines.
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 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]
choosing a max of 5 items per line.
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
}()
回答2:
I had the same problem. Deleting precompiled headers and and derived data seemed to fix it. I'm not sure if that will fix it permanently, but it is working for now at least.
来源:https://stackoverflow.com/questions/25810625/xcode-beta-6-1-and-xcode-6-gm-stuck-indexing-for-weird-reason