Xcode Beta 6.1 和 Xcode 6 GM 由于奇怪的原因卡住索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25810625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:32:25  来源:igfitidea点击:

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

xcodeswiftxcode6

提问by Wak

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

我正在开发一个 swift 应用程序,在某些时候我有一个类似于这样的代码:

 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.

出于某种原因,当我添加此代码时,xcode 卡住了,我无法做任何其他事情。

Opening the Activity Monitor, it displays sourcekitservice and swift using more than 100% CPU.

打开活动监视器,它会显示使用超过 100% CPU 的 sourcekitservice 和 swift。

I've created this sample project with the code above : https://dl.dropboxusercontent.com/u/1393279/aaaaaaa.zip

我用上面的代码创建了这个示例项目: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.

我已经尝试过清理派生数据、重新安装 Xcode、重新启动、等待几分钟等。它只是不起作用。

回答by Antonio

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.

我在操场上测试了您的代码,我立即注意到 SourceKitService 进程占用了我 100% 的 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):

Swift 没有+=为字典提供运算符,所以我们首先需要一个(感谢@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.

每行最多选择 5 个项目。

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:

但是在您的代码中,您将字典声明为不可变的 - swift 没有提供任何语句来在声明后初始化不可变的 - 幸运的是我们可以使用闭包来实现:

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

回答by kgreenek

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.

我有同样的问题。删除预编译的头文件和派生数据似乎可以解决这个问题。我不确定这是否会永久修复它,但至少现在它正在起作用。