Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 247
Wak Avatar asked Sep 12 '14 14:09

Wak


2 Answers

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
}()
like image 75
Antonio Avatar answered Nov 15 '22 08:11

Antonio


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.

like image 27
kgreenek Avatar answered Nov 15 '22 09:11

kgreenek