5

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.

Wak
  • 818
  • 2
  • 11
  • 18
  • I've had the various betas of Xcode 6 hang on indexing for 15sec or so. Occasionally I got annoyed and stopped the job and tried again to have to work right away. I haven't seen the issue in the GM seed (yet). On a side note, from the title "Xcode Beta 6.1"... huh? There is no Xcode 6.1 yet. – DBD Sep 12 '14 at 14:55
  • 2
    Yes, there is: https://developer.apple.com/xcode/downloads/ – Wak Sep 12 '14 at 15:23
  • My mistake I stand corrected. – DBD Sep 12 '14 at 15:31

2 Answers2

18

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
}()
Community
  • 1
  • 1
Antonio
  • 71,651
  • 11
  • 148
  • 165
  • 3
    It worked. Thanks. First time I see something like this. Swift is clearly not ready for production yet. – Wak Sep 21 '14 at 02:43
  • 1
    Is this fixed in XCode 6.0.1? I am still getting this problem. – zeeple Oct 01 '14 at 05:16
  • 2
    I had a similar thing when migrating to Xcode 6.1. I had a menu defined as a List of Lists of Dictionaries, like private let menuData = [[ <> ]] that took about 35 lines of codes. Xcode was hanging in "Indexing | Compiling swift code" with CPU at 100% in swift process. Fixed by adding small parts of the menu dynamically instead! I guess I could have used a Plist also, but this is not the point. – Francois Robert Nov 02 '14 at 01:40
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.

kgreenek
  • 4,986
  • 3
  • 19
  • 30