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

前端 未结 2 1314
无人共我
无人共我 2020-11-28 15:38

I\'m developing a swift application that at some point I have a code similar to this:

 import UIKit

class ViewController: UIViewController {
    private var         


        
2条回答
  •  臣服心动
    2020-11-28 16:09

    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 += (inout left: Dictionary, right: Dictionary) -> Dictionary {
        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
    }()
    

提交回复
热议问题