快速 Xcode 索引冻结或缓慢

也许这只是我经历的一个恼人的“特征”:

在从 Xcode 6.0.1升级到 Xcode 6.1之后,情况发生了变化。Xcode 6.1永远在索引项目或编译源文件。这个项目并不大。它只包含工作区中的一些 Swift 文件和 AWS SDK 2.0 Cocoapods。我不认为它应该阻止整体索引和编译顺利。我尝试了一些 aws-sdk-ios 示例,只是想看看 Xcode 6.1是如何在它们上面工作的,结果它们永远都是一样的。

到目前为止,我尝试了哪些解决方案:

  1. 在管理器中删除“派生数据”,并重新打开和工作区。(未能修复)
  2. 在. xcodeproj 文件上“ Show Package Content”,并删除. xcworkspace,如(Xcode 4-缓慢的性能)所示

不幸的是,没有一个管用。

另外,也许我应该重新创建这个项目? 我的电脑设置: MacBook Pro (Retina,13英寸,2014年中) ,内存8 GB 1600 MHz DDR3,带优胜美地。(我认为这对于运行这个小项目来说已经足够了。)

49284 次浏览

I experienced this same issue after upgrading to 6.1. Xcode would get stuck compiling or indexing without generating a specific error message.

The issue was finally resolved by breaking some of the longer expressions in the swift files down into multiple shorter expressions. Part of my program combines many different string variables to form a longer string. Attempts to combine them in a single expression and using the addition assignment operator both failed. I was able to make it work by doing something similar to the following (simplified):

var a = "Hello"
var b = " "
var c = "World"
var d = "!"
var partA = a + b
var partB = c + d
var result = partA + partB

I got this idea from receiving the following error many times in the previous Xcode version "Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions."

Hope this helps

Finally, I "solved" the issue, though it is just a workaround.

I created another project and added files one by one to it. Then I spotted a "very long" viewcontroller.swift file. Then I broke its codes into modules and made those repeatedly used codes into functions in another swift file. And also, I took the suggestion online that long expression should be broken into shorter ones. Then, the indexing works and the compiling works.

So for now, I have it "solved".

BUT, I don't think this is right. Xcode IDE should be more than capable of handling my "very long" swift file, only 1500 lines. I believe this is definitely a bug (existing for a long time), although Xcode 6.1 is already an upgrade from Xcode 6.0.1.

I've struggled with the same problem. I've tried the two solutions mentioned ( deleting derived data and deleting .xcworkspace ) with no success. I also tried slowly commenting out most of the code bit by bit and removing the files until there was almost nothing left and the indexing was still stuck.

I did find a solution that worked for me, I've opened the project with an older Xcode Version 6.1 (6A1030) which had no problem indexing then got back to the latest Xcode Version 6.1 (6A1052d) which I was using before and indexing was fixed and continued to work well.

My conclusion is that this is a bug with Xcode Version 6.1 (6A1052d) which I hope will improve with future releases.

The problem does come back once in a while, the same fix works each time. I guess another solution would be to just stick with the older Xcode Version 6.1 (6A1030) but it won't work with devices running iOS 8.1 and it won't have the latest bug fixes.

I've had this issue as well and solved it by removing/changing expressions with the "+" operator.

I changed this:

var mainArray = arrayOne + arrayTwo + arrayThree + arrayFour + arrayFive

To this:

var mainArray = arrayOne
mainArray += arrayTwo
mainArray += arrayThree
mainArray += arrayFour
mainArray += arrayFive

It solved the problem.

My machine is a maxed out MBP late 2013

For me, I deleted the Xcode app and downloaded it again and install it. That solved the issue, at least over now.

The Xcode 6.2 beta resolved the issue for me. Not lightning-fast, but at least it isn't indexing forever. The beta does not install over the top of your regular Xcode installation, so if you don't like the beta, you can just delete it.

Various Xcode downloads including the beta >

You may wish to update to Xcode 6.1.1

It has been officially released and resolved for us the indexing problem. In the update description, it says that they have applied stability fixes so it is very likely that it will behave in a more stable fashion.

If you don't mind reverting back to 6.0.1 until they figure it out, that's what worked for me. I was having the same issue with both 6.1 and 6.1.1. Now I'm good. I'll try 6.2 when it comes out.

You can find previous versions of apple software on their official dev site, here: https://developer.apple.com/downloads/index.action

If you do this, make sure to delete your current copy of Xcode first.

I tried many of the suggestions above including splitting files, installing Xcode 6.2 beta, and breaking string concatenation statements. What finally did it for me was splitting an array of dictionaries literal declaration I was using for test data into multiple .append statements.

// This causes indexing/building to hang...
var test = [ [ "a": false, "b": "c" ],
[ "a": false, "b": "c" ],
[ "a": false, "b": "c" ],
[ "a": false, "b": "c" ],
[ "a": false, "b": "c" ],
[ "a": false, "b": "c" ] ]


// This works fine.
var test = [ [ "a": false, "b": "c" ] ]
test.append([ "a": false, "b": "c" ])
test.append([ "a": false, "b": "c" ])
test.append([ "a": false, "b": "c" ])
test.append([ "a": false, "b": "c" ])
test.append([ "a": false, "b": "c" ])

Also, for what it's worth, the 6th entry in this array is what causes the issue for me; five works just fine.

Are you using CocoaPods? I ran across the same issue earlier today. (Using xCode 6.1.1)

To fix the issue, I deleted everything in ~/Library/Developer/Xcode/DerivedData, the Pods folder in my project directory, and <project>.xcworkspace.

I then opened terminal, navigated to my project directory, and ran pod install again.

I am using Xcode 6.1.1 with swift files on the same exact MacBook Pro.

As I kept adding rows into a 3D string array, Xcode all of sudden became unusable and now I can't do anything.

Will try to revert to 6.1 and hopefully the problem will go away.

On my Xcode the solution was to close all redundant windows. For some reason many open windows make Xcode very slow.

The only working solution for me is to delete all the derived data (not only for the current project, just clean up the whole folder) and then restart Xcode.

  1. Open File / Preferences in Xcode

  2. Click on Locations on the far right of the pop up window

  3. Click on the little arrow icon next to "/Users/Mac/Library/Developer/Xcode/DerivedData"....it takes you to an Xcode folder that contains a DerivedData folder (that contains all of the derived data from your previous projects.)

  4. DELETE the DerivedData folder

For me, I tried all the above with no success; but all I had to do was to delete the derived data folder, then open up another random project, wait for it to index and now my original (malfunctioning) project works!

Do the development world a favour apple and make your swift compilers open source- so we are not all thwarted by your incompetence.

Had the same issue today. Xcode 6.3.2, medium-sized Swift project. At one point it started indexing and it would never finish indexing. The code that caused this was a dictionary of type [String:[String]], so a string-key dict with string arrays as values. I had two of these with keys from A to Z and each of these 26 entries contain a string array with 5 to 10 strings.

Clearing derived data didn't help. Only commenting out those dicts made it go again.

Honestly, this is ridiculous! Apple needs to fix Xcode! It's already horribly slow when compiling Swift projects but bugs like this are a showstopper. I can't do my job properly with this!

I am seeing this in Xcode 6.3.2. I had really hoped that a year after release, they would have the compiler working, but alas.

If none of the above solutions work for, try checking your code for syntactic errors. In the process of refactoring, I extracted a closure but forgot to qualify the parameters:

    let hangsInsteadOfError = { l, r in
return l.nameFirst < r.nameFirst
|| l.nameFirst == r.nameFirst && l.nameLast < r.nameLast }


let fixingErrorAvoidsHang = { (l:User, r:User) -> Bool in
return l.nameFirst < r.nameFirst
|| l.nameFirst == r.nameFirst && l.nameLast < r.nameLast }

If I have learned anything from working in Swift, it is to work incrementally, to avoid having to backtrack too much to find the offending code.

For those still having this issue, this is a workaround I've come to enjoy which prevents you from having to enter the objects one by one:

// instead of this, which freezes indexing
let keys = [keyQ, keyW, keyE, keyR, keyT, keyY, ... keyM]


// and instead of this, which is ugly & lengthy
var keys = [KeyboardKey]()
keys.append(keyQ)
keys.append(keyW)
...
keys.append(keyM)


// use this:
var keys = [KeyboardKey]()
keys.appendContentsOf([keyQ, keyW, keyE, keyR, keyT, keyY, ... keyM])

I am using Xcode Version 7.3 (7D175)

I think I might have figured out an underlying problem. There where two instances where I got stuck in the indexing phase:

  • I created a closure that I assigned to a variable and omitted the type signature. I think xcode had issues with that type inference step. If I remember correctly one of the arguments was a CGPoint, which has an overloaded constructor. My hypothesis is that there where too many possibilities of what my closure might accept as arguments.

  • I refactored a factory method such that instead of returning instances of one type, it could return instances of many types with a common base class. It appears that wherever I used the factory method, I had to cast the resulting object to a specific type (either with as? or by assigning it to a variable that accepts a specific type) Again the type inference step seems to be broken.

It seems like the same is going on with the dictionary declarations mentioned by earlier individuals. I filed a bug report with apple.

  1. Is your indexing status an "Indicator circle" or "Progress bar"?
  2. If it is an "Indicator circle", that means it already stuck at the beginning.
  3. Open and check with your other projects, if they are all the same, that means it's a system issue.
  4. Just restart your computer and everything will be fine.

Xcode indexing usually for your code for suggestions and auto complete among other things like helping you in story boards and vice versa. But to make faster your xcode project you can turn it off/on via terminal

Turn off indexing
defaults write com.apple.dt.XCode IDEIndexDisable 1 Turn on indexing defaults write com.apple.dt.XCode IDEIndexDisable 0

But Better approach to use a speedy mac with good RAM.

I use Xcode 8.2 and also ended in this problem. It started after I defined a complex tuple variable -- an array of tuple with subarray of tuple. Things get really slow when the subarray of tuple has a property that is programmatically calculated.

As some other answers noted, the indexing takes forever, and I believe it is trying to infer the types the variable.

I solved the problem first by clearly define the variable with types included. When updating the property, I calculate it first then assign it to the tuple, instead of calculating in defining the variable.

Here is an example code.

var sectionTuples: [(section: String, rows: [(name: String, subtitle: String)])] = []
let subtitle1: String = "" // something calculated dynamically
let subtitle2: String = "" // something calculated dynamically
sectionTuples = [(
section: "Section 1", rows: [
(name: "name1", subtitle: subtitle1),
(name: "name2", subtitle: subtitle2)
])]

The bottom line is that don't let Xcode infer complex structures.

I was having the same issue. My Xcode is 8.2.1. But in my case, I wanted to create an array of dictionary with 33 key-value pairs. I was doing in the following way which was stuck in indexing:

var parameter = [String : AnyObject]()
var finalArray = [parameter]


for item in listArray
{
parameter = ["A": item.a as AnyObject, "B": item.b as AnyObject, "C": item.c as AnyObject, ... , "Z": item.z as AnyObject]


finalArray.append(parameter)
}

Following worked for me:

var parameter = [String: AnyObject]()
var finalArray = [parameter]


for item in listArray
{
parameter["A"] = listArray.a as AnyObject
parameter["B"] = listArray.b as AnyObject
parameter["C"] = listArray.c as AnyObject
parameter["D"] = listArray.d as AnyObject
.
.
.
parameter["Z"] = listArray.z as AnyObject
finalArray.append(parameter)
}

I have tried this with Xcode 8.3.3. Here are my results:

You can write perfectly fine Swift code that will cause indexing to hang.

Once indexing hangs, it hangs. Changing the Swift code back to something that wouldn't cause indexing to hang doesn't help, it still hangs.

Closing the project and reopening doesn't help in that situation.

Quitting Xcode and restarting it helps. Indexing will not hang anymore (that is if you changed the code back to something that doesn't make it hang).

Restarting your Mac helps as well, although it isn't needed.

The hanging is caused by perfectly fine Swift code. An example that I had looked like

if let date = function1()
?? function2()
?? function3()
?? function4()
?? function5()
?? function6()
?? function7()
?? function8()
?? function9()
?? function10() {
return date
}

Indexing would hang. I commented out most of the "??" lines and it was fine (after quitting and relaunching Xcode). Uncommented one line after the other. With some number of lines it was fine, then uncommenting the next line would make it hang.

The only thing that helps apparently is changing your code.

I had expressions like:

let x = (value as? Int) ?? someDefault

also

guard let x = (dateFormatter().string(from: Date()) + msg + "\n").addingPercentEncoding(...) else { ... }

So the point is to rewrite your file to contain only kindergarten level expressions and the indexing problem will go away.

Xcode 11.5 (11E608c) and still the same issues, 6 years after the original question. I wish i could "mark" apple in this question so they can see this mess. This is a large project( >1000 files) and i was under the clock so when i notice the freezing index i was with more than 100 files changed and can't go back.

I've tried everything:

  1. clear Derived data and build
  2. Restart xcode , restart mac
  3. remove and add source
  4. Searched for dictionaries literal's etc etc

The problem was an array creation:

private var overlayColors: [UIColor] = [UIColor(hex: "#b71c1c"), UIColor(hex: "#4a148c"),
UIColor(hex: "#880e4f"), UIColor(hex: "#1de9b6"),
UIColor(hex: "#f50057"), UIColor(hex: "#311b92"),
UIColor(hex: "#f44336"), UIColor(hex: "#651fff"),
UIColor(hex: "#d500f9"), UIColor(hex: "#3d5afe"),
UIColor(hex: "#bf360c"), UIColor(hex: "#0d47a1"),
UIColor(hex: "#006064"), UIColor(hex: "#2979ff"),
UIColor(hex: "#ff6f00"), UIColor(hex: "#1a237e"),
UIColor(hex: "#795548"), UIColor(hex: "#004d40"),
UIColor(hex: "#00e676"), UIColor(hex: "#01579b"),
UIColor(hex: "#33691e"), UIColor(hex: "#827717"),
UIColor(hex: "#76ff03"), UIColor(hex: "#ffc400"),
UIColor(hex: "#e65100"), UIColor(hex: "#00b0ff"),
UIColor(hex: "#ff3d00"), UIColor(hex: "#616161"),
UIColor(hex: "#263238"), UIColor(hex: "#ff1744")]

What helped me to discover the bad swift file was when xcode freezed indexing i did the following steps

  1. open activity monitor -> "swift" process ->show process info -> open Files and Ports. This will give you a list of what files this process is running drilling down your list of possible bad files
  2. Other handy tool is this script SOURCEKIT_LOGGING=3 /Applications/Xcode.app/Contents/MacOS/Xcode &> ~/Documents/xcode.log this will start Xcode with level 3 verbose and start logging in the log file.
  3. Search in the log file the last entries for your swift files ex: "my_project/Source/App/"

It's not a full solution but it's helpful to drill down and know where to look.

open activity monitor -> "swift" process ->show process info -> open Files and Ports.