Xcode Beta 6.1 and Xcode 6 GM stuck indexing for weird reason

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
}()

Leave a Comment