ios Xcode 8.0 Swift 3.0 缓慢的索引和构建
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39547197/
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
Xcode 8.0 Swift 3.0 slow indexing and building
提问by Danny
I've installed Xcode 8.0 and converted Swift 2.2 to 3.0 (that process also took a lot of time, I just left my Mac running all night). I have not a big project (about 20 files). I am also using Pods
. Indexing of previous Xcode version (< 8.0) worked fast but now, after upgrade, the progress bar is stuck on one position (I am already waiting for an hour).
我已经安装了 Xcode 8.0 并将 Swift 2.2 转换为 3.0(这个过程也花了很多时间,我只是让我的 Mac 运行了一整夜)。我没有一个大项目(大约 20 个文件)。我也在使用Pods
. 以前 Xcode 版本 (< 8.0) 的索引工作很快,但现在升级后,进度条卡在一个位置(我已经等了一个小时)。
Things I've tried that didn't help me:
我尝试过但对我没有帮助的事情:
- Cleaned the
DerivedData
folder and restarted Xcode - Cleaned the project and restarted Xcode
- Deleted
Pods
directory with<project>.xcworkspace
and then installed again - Restarted Mac
- Tried build project without Pods
- Reinstalled Xcode
- Tried on another Mac with cloned project
- 清理
DerivedData
文件夹并重新启动Xcode - 清理项目并重新启动Xcode
- 删除
Pods
目录<project>.xcworkspace
然后重新安装 - 重新启动的 Mac
- 尝试在没有 Pod 的情况下构建项目
- 重新安装 Xcode
- 在另一台带有克隆项目的 Mac 上尝试
It is really not cool to make such releases of software when developers have to spend hours on solving such ridiculous problems. It is very disappointing. Any ideas how to fix this?
当开发人员不得不花费数小时来解决这些荒谬的问题时,发布这样的软件版本真的很不酷。这是非常令人失望的。任何想法如何解决这一问题?
采纳答案by Danny
I solved the problem by commenting all files and then removing comments one by one. I found that the problem is still in the array declaration as described here.
我通过评论所有文件然后一一删除评论来解决问题。我发现问题仍然存在于此处描述的数组声明中。
I had code like this and project was not indexing:
我有这样的代码,项目没有索引:
class {
var first: String!
var second: String!
var third: String!
var fourth: String!
var fifth: String!
func abc() -> [String] {
var array = [first, second, third, fourth, fifth]
}
}
I've changed it to this and indexing started working:
我已将其更改为此,索引开始工作:
class {
var first: String!
var second: String!
var third: String!
var fourth: String!
var fifth: String!
func abc() -> [String] {
var array = [first]
array.append(second)
array.append(third)
array.append(fourth)
array.append(fifth)
}
}
回答by Chris
Go to project settings, then Editor > Add Build Setting > Add User-Defined Setting, and add the following:
转到项目设置,然后编辑器 > 添加构建设置 > 添加用户定义设置,然后添加以下内容:
SWIFT_WHOLE_MODULE_OPTIMIZATION = YES
Adding this flag dropped our clean-build compile times from 7 mins to 65s for a 40KLOC swift project, miraculously. Also can confirm 2 friends have seen similar improvements on enterprise projects.
添加此标志后,40KLOC swift 项目的干净构建编译时间奇迹般地从 7 分钟缩短到 65 秒。也可以确认 2 位朋友在企业项目上看到了类似的改进。
I can only assume this is some kind of bug in Xcode 8.0
我只能假设这是 Xcode 8.0 中的某种错误
回答by Ben Simon
I've had the same issue only since upgrading to Swift 3/XCode 8 and it seems to be caused by large array literals.
自从升级到 Swift 3/XCode 8 后,我才遇到同样的问题,这似乎是由大数组文字引起的。
I was able to fix the issue by adding type annotations to the variables being assigned to the array literal, e.g.
我能够通过向分配给数组文字的变量添加类型注释来解决这个问题,例如
let array: Array<String> = ["1", "2", "3", "4", "5", "6", "7", "8"]
instead of
代替
let array = ["1", "2", "3", "4", "5", "6", "7", "8"]
回答by Stefan Nestorov
I had similar problem and followed this guide to debug : http://irace.me/swift-profilingMy problem was i had nil coalescing operator in some strings for example:
我有类似的问题,并按照本指南进行调试:http: //irace.me/swift-profiling我的问题是我在某些字符串中有 nil 合并运算符,例如:
let name = "\(someString ?? "")"
and four methods with this were causing 2 min additional building time.
四种方法会导致 2 分钟的额外构建时间。
回答by silly_cone
I had the same problem and solved it by painstakingly going through my code line by line, it turns out Swift 3 prefers string interpolation rather than using the + symbol, i.e.
我遇到了同样的问题,并通过逐行煞费苦心地检查我的代码来解决它,结果 Swift 3 更喜欢字符串插值而不是使用 + 符号,即
let url = "http://yahoo.com" + "someWebPage" + "whereItsInteresting"
If you have been using the above style of code replace it for;
如果您一直在使用上述风格的代码,请将其替换为;
let url = "http://yahoo.com\(someWebPage)\(whereItsInteresting)"
And your build time will immediately come back to normal.
您的构建时间将立即恢复正常。
回答by Constantin Saulenco
回答by Tai Le Anh
回答by Tailz
I encountered the same indexing issue, but it occurred only when I was running/debugging on a device and then switched to another device on the top-left toolbar (Target > iPhone).
我遇到了同样的索引问题,但只有在我在设备上运行/调试,然后在左上角工具栏(目标 > iPhone)上切换到另一个设备时才会发生。
None of the solutions above worked for me.
上述解决方案都不适合我。
My solution:I removed my local git working copy and cloned a new one from my 'origin'.
我的解决方案:我删除了本地 git 工作副本并从我的“原点”克隆了一个新副本。
(There are some 'magic' files within the xcuserdata/shared/session etc. folders that may have caused this issue?)
(在 xcuserdata/shared/session 等文件夹中有一些“魔法”文件可能导致了这个问题?)
回答by steve
Not that I think this is related to OP's issue, but XCode 8 for me recently slowed to a halt. I eventually found it was my mistake (and I remember inadvertently doing it) - I added XCode.app as a Framework reference. This essentially made XCode search and index the entire XCode.app folder. Once I saw the mistake and remove the Framework it came good again :)
并不是说我认为这与 OP 的问题有关,但是 XCode 8 对我来说最近放缓了脚步。我最终发现这是我的错误(我记得无意中这样做了) - 我添加了 XCode.app 作为框架参考。这基本上使 XCode 搜索并索引整个 XCode.app 文件夹。一旦我看到错误并删除了框架,它又好了:)
回答by Jojodmo
I had a function that took over a minute to compile, and after some investigation, I found that the culprit was checking if enough time had elapsed from a stored date:
我有一个函数需要一分钟来编译,经过一些调查,我发现罪魁祸首是检查从存储的日期开始是否已经过去了足够的时间:
let myStoredDate: Double = // Double representing a time in the past
// if at least one week (60 * 60 * 24 * 7 seconds) has passed since myStoredDate
if Date().timeIntervalSince1970 - myStoredDate > (60 * 60 * 24 * 7){
// do stuff
}
This code would take over 10 seconds to compile — coupled with this code being repeated with different numbers multiple times, it was causing compilation to take way too long. I was able to fix this by pre-computing the interval
这段代码的编译时间超过 10 秒——再加上这段代码用不同的数字重复多次,导致编译时间太长。我能够通过预先计算间隔来解决这个问题
let myStoredDate = // Double representing a time in the past
//it is important to explicitly specify that the variable is a Double
let interval: Double = 60 * 60 * 24 * 7
if Date().timeIntervalSince1970 - myStoredDate > interval{
// do stuff
}
After doing this with the ~10 times I was checking, the compilation time was cut from over a minute to just a few milliseconds.
在我检查了大约 10 次之后,编译时间从一分钟多到几毫秒。
It's extremely likely that this problem also occurs with the combination of type-inferance and math elsewhere, so ensure that nothing like this happens anywhere else in your code.
此问题极有可能在其他地方的类型推断和数学组合中也发生,因此请确保在您的代码中的其他任何地方都不会发生类似的情况。