65

I'm currently developing an iOS app using swift and Xcode 6 (Beta 3).

Everything went fine so far but now as my project grows, Xcode suddenly began indexing and it does that again and again, making Xcode nearly unusable.

I have searched the web for similar problems and tried the solutions but none of them did help. Even disabling the indexing process (defaults write com.apple.dt.Xcode IDEIndexDisable 1) does not stop Xcode to do that.

While indexing, my CPU usage goes up to 300%+, causing the fans to run at highest speed.

In Activity Monitor there are several tasks named "swift" taking up about 1GB memory each.

pkamb
  • 33,281
  • 23
  • 160
  • 191
zisoft
  • 22,770
  • 10
  • 62
  • 73

29 Answers29

26

Killing the processes named 'swift' and then checking the error in xcode will give you the part of the code giving you trouble. There are some bugs in swift that needs to be circumvented.

To kill the process: Applications > Utilities > Activity Monitor. Then find the "swift" process, double click and choose Quit or Force Quit.

hannesr
  • 449
  • 5
  • 12
  • 1
    Yes, I tracked down and it was really a problem within the swift code. Thanks for the answer! – zisoft Jul 16 '14 at 17:06
  • Killing the `swift` process stops the indexing. But it still freezes when I compile. I had to delete some of my Swift code and it works! Seems like the `as` part in `case serverBtn as SKSpriteNode:` in my switch case causes the problem. And I'm in Xcode 6 beta 7. – Hlung Sep 07 '14 at 15:16
  • This led me in the right direction. In my instance, the subprocess of Xcode was called something like SourceKit (should have made a note of it, whoops!). I killed it, it restarted and the progress bar went further then hung again. Killed it again, it restarted and the progress bar finished up. Thanks! – Kevin Vaughan Feb 18 '15 at 18:59
  • 22
    Please elaborate on how exactly to "kill processes named swift and check the error in Xcode". – Travis M. Mar 25 '15 at 21:04
  • 2
    To kill the process: Applications > Utilities > Activity Monitor ... then find the "swift" process and terminate it ... this worked for me ... though I also don't understand "check the error in XCode" – Chris Allinson Mar 22 '17 at 22:19
22

Happened to me with Xcode 7.3

Had to clean everything Xcode had cached to solve it.

Solution:

rm -frd ~/Library/Developer/Xcode/DerivedData/*

rm -frd ~/Library/Caches/com.apple.dt.Xcode/*
Fawkes
  • 3,831
  • 3
  • 22
  • 37
  • 1
    I did this and restarted my Mac and it was solved (using Xcode 8) – Sam Sep 30 '16 at 20:38
  • I did and did not work. There was a file which was not supposed to be in my project because part of a package I was editing. I had to remove the file (just remove the reference) and it worked. Sometimes are small things. Xcode is a bit messy when copying files always check if they are in the correct folder (in finder) – multitudes Jun 15 '21 at 13:24
17

I had this same issue and it took me FOREVER to solve it. I'm pretty sure I've seen every question on the internet about this issue and I tried all of the solutions. Turns out all I had to do was....

Restart my computer

Alex
  • 309
  • 2
  • 7
  • This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/11520331) – E-Riddie Mar 05 '16 at 23:51
  • 2
    @EridB How does this not attempt to provide an answer? It might be completely wrong, [but that doesn't mean we should abuse the review queues to delete it.](http://meta.stackoverflow.com/q/287563/1849664) – Undo Mar 07 '16 at 00:39
  • @Undo I wasn't aware of that discussion, thanks for pointing it out. As for `the answer`, we can leave it there, but still doesn't describe anything related to what could have caused this problem. Imagine if there were answers like this all around. :) – E-Riddie Mar 07 '16 at 00:58
  • 4
    @EridB Idk what caused the problem. All i know is that i had the exact same problem and it would've saved me tons of time if someone would've told me that simply restarting my computer could have solved it lol. I believe all possible solutions are important to every issue. I would not have posted it otherwise – Alex Mar 07 '16 at 07:42
  • Ha..told you this was a perfectly acceptable solution, @EridB and Undo.. turns out you guys don't know it all – Alex Jan 10 '17 at 23:26
  • 2
    I'm very glad this answer was here, it solved my problem, and if someone had deleted it for being a "non-answer", I'd probably still be banging my head against the wall. – biomiker Jul 11 '19 at 22:28
  • This was the perfect solution for me too. Yes, all of the other answers are valuable (and often required), but sometimes, this issue is just caused by bugs in macOS, for which a restart's the only answer! For me, it seemed to be related to a 10.15 bug related to syncing iOS causing macOS to lock up, but there can be many causes. So glad this answer wasn't deleted. – TheNeil Dec 27 '19 at 19:13
  • It's likely this answer doesn't have more upvotes because not everyone remembers to come back here after restarting his computer and verifying it actually solves the problem. – Ulysses Alves Nov 29 '20 at 19:43
  • Rather than scolding , this turned out to be the solution that worked without explanation other than *sometimes xcode gets screwed up*. – truedat101 Jan 13 '21 at 11:15
13

Solved it: I deleted the most recently added files from the project and the problem disappeared. Then I started to add back the files, one by one until the problem reappeared. So I found the file causing the problem. Then I deleted the most recently added code from that file and again, the problem disappeared.

That way, I found a piece of code which was responsible for that behavior.

zisoft
  • 22,770
  • 10
  • 62
  • 73
  • 4
    For my part, the definition of a literal dictionary containing all device models caused an endless indexing ... After removing this statement, XCode accepts to work again. This is very strange! – Chrstpsln Dec 11 '14 at 13:14
  • 3
    I just had the same issue with a literal dictionary declaration in Xcode 6.3 release version – Dorian Roy Apr 13 '15 at 13:58
  • Me too! Same problem here – ppaulojr Jun 02 '16 at 16:31
  • After seeing this post and the replies, I went back and looked at my dictionaryLiteral as I was having this issue for a couple of days, turned out I had some unused elements in my dictionary which I had deleted in the constraints part and had set some auto layout in storyboard for them ; when I removed those in the dictionary everything starting to feel as it was before and swift doesn't take long time to index or run the project! Cheers – TheBen Nov 24 '16 at 02:24
10

I had the same issue in my code. The solution for me was delete all spaces in the array in my code.

Ex.

  struct Objects {

  let objectA = ["text1", 
                 "text2", 
                 "text3", 
                 "text4"] }

// Noise, CPU 100% and Index forever. The solution is...

struct Objects {
    let objectA = ["text1","text2","text3","text4"]}

// Solved making the Array or String with no space.

Richard Groves
  • 3,596
  • 3
  • 21
  • 21
Maximo Lucosi
  • 378
  • 4
  • 9
  • 1
    Interestingly, for me removing the crlf for better readability in a dictionary solved the issue as well. Indexing was stuck on an endless loop and compiling of the struct took literally forever – Ron Aug 31 '15 at 17:49
  • This solved my issue also! It was a dictionary but same difference, basically just had to keep it all on the same line! – Gregg Sep 27 '16 at 03:45
4

I had that problem when I was at the swift crunch in krakow a couple weeks ago. We had the code on github, experienced that indexing problem on a macbook, we tried pulling the repo on 2 other macbooks, same result.

It's clearly a bug, I don't know what is causing it, we tried whatever we could think of (clean, clean build folder, manually removing files not in the repo, rebooting, killing processes, etc.), and after a couple hours the only thing left to do was creating a new xcode project from scratch and manually importing the files from the other project.

Never happened again since then, neither on that nor on other projects.

Antonio
  • 71,651
  • 11
  • 148
  • 165
  • Down-vote without an explanation kind of makes you sorry you responded? Down-voter, tell us why? I up-voted to correct the un-explained down-vote. – zaph Jul 16 '14 at 14:45
  • 1
    Thanks @Zaph - I agree, a downvote should be followed by an explanation. Mine is not a solution to the problem... because we couldn't find any, so I have just shared my experience. If anybody has a solution, then he's very welcome – Antonio Jul 16 '14 at 14:49
  • 2
    Actually there is a solution in the answer: "creating a new Xcode project". – zaph Jul 16 '14 at 15:33
  • 2
    :) that's a drastic and brutal way of fixing a problem, to be used as last resort. But it definitely solved the problem. – Antonio Jul 16 '14 at 15:43
  • ?? was giving me this weird indexing bug – mateusmaso Jul 03 '15 at 03:07
4

For me, I made a stupid mistake. I write a Class like this:

class A: A {
.......
}

A class inherit itself that causes the freezing. There is no message hint from Xcode. You can just take this as possible reason ~ .

Victor Choy
  • 4,006
  • 28
  • 35
4

It's Xcode bug. Problem caused with concatenation in one line:

var value = "some text" // it can be String or Array
value = value + value1 + value2 + value3 + value4 + value5 + value6 // etc

This correction fixes this bug:

var value = "some text"
value += value1
value += value2
value += value3
value += value4
value += value5
value += value6
pkamb
  • 33,281
  • 23
  • 160
  • 191
Igor
  • 12,165
  • 4
  • 57
  • 73
3

I was creating a dictionary like this

var dic1 = [
    "isDestination" : self.isDestination ?? false,
    "price" : self.price ?? ""
]

and self.price is of type Int and I was giving its fallback value as an empty string which screwed up the Xcode compilation.

pkamb
  • 33,281
  • 23
  • 160
  • 191
Rajat Talwar
  • 11,922
  • 1
  • 18
  • 12
2

I got this issue and 6 hours later (after trying everithing and build new project again step by step copying resources) I FOUND MY PROBLEM:

class A : A {
...
}

By the fact of copy/paste I had a class that extends itself, and this makes indexing crazy.

SendelDL
  • 39
  • 1
2

It's definitely a Xcode bug and I reported that to Apple. When you have a large dictionary literal or a nested dictionary literal. You have to break your dictionary to smaller parts and add them with append method until they fix the bug. Xcode 8.2.1 (8C1002)

Saeed Ir
  • 1,974
  • 2
  • 20
  • 22
  • 1
    dic[key] = value to add the entries to your dictionary and like you said, the problem's fixed. – alasker Jun 03 '18 at 13:00
1

I had the same problem with one call adding 11 NSLayoutConstraint objects to an array.

The solution was to divide the code into several calls, each adding only 3 objects to the array. Weird.

That was in Xcode 6.4

Oliver Eichhorn
  • 307
  • 2
  • 6
0

Too many string concatenations in one line cause troubles. Helped me too. Originally was pointed by Zhenshan Yu there: Xcode 6 Beta not compiling

Community
  • 1
  • 1
Mikhail Larionov
  • 676
  • 1
  • 7
  • 11
0

I had this issue with XCode 6.3 when creating a C++ project. Before switching over to developing in SubLime, my last ditch effort was to delete the XCode app and reinstall. It was a long process, but my version of XCode is now updated to 7.3 and everything is working as it should.

So if nothing else seems to be working, you could try deleting XCode from your applications folder and then reinstalling. Just be sure you aren't deleting any project files you want to keep.

TMC
  • 13
  • 1
  • 4
0

My particular problem was a fairly long literal dictionary containing much data.

My solution was to understand that Xcode indexing wasn't "stuck", but just taking a long time.

So I only had to wait more time than I expected.

pkamb
  • 33,281
  • 23
  • 160
  • 191
Jacobo Koenig
  • 11,728
  • 9
  • 40
  • 75
  • Instead of a dictionary literal add the objects to the dictionary like dic[key] = value and you'll be good to go :) – alasker Jun 03 '18 at 13:00
0

Mine was about dragging a new file with String extension to the project and not adding it to all required targets. Hope that helps someone.

nikans
  • 2,468
  • 1
  • 28
  • 35
0

Backup your project delete the lastone and restart the xcode simple :-)

Shakeel Ahmed
  • 5,361
  • 1
  • 43
  • 34
0

I too faced the same issue for Xcode 9.1. So i looked into Activity Monitor. There was swift process which was above 100%. Double Clicked it and Quit. Done. Now its working fine.

0

I went to tools->task and contexts->clear contexts and that seemed to give the computer rest finally!

0

enter image description here

I got this issue when my Xcode was 9.2 . First I deleted xcworkspace file ,cleaned and built according to the others' answer.But it did not work. Then I updated Xcode to 9.3 It also did not work. I checked my code and found that the recently written code made Xcode Indexing forever:

TimeInterval(3600*24*(-randomDay))

Then I amended it:

TimeInterval(-3600*24*randomDay)

It worked. I find that many situations can cause Xcode to work abnormally.So I think the correct solution is that think about what you've done for your project recently

MartinWo
  • 11
  • 2
0

In my case the issue was caused by some aritmetic sums. I was creating a collectionView with all the different frames programmatically doing it like this:

cell.textView.frame = CGRectMake(8 + 10 + 12, 0, 150 + 6 + 6 + 4, 50)

I just changed it to:

cell.textView.frame = CGRectMake(30, 0, 166, 50)

It helps me figure out the margins and paddings more easily, but just puting the result of the sum changed the build speed from 5 - 7 minutes to 20 seconds or so.

Yluna
  • 103
  • 2
  • 15
0

Yet one possible thing, that may cause such behavior: For debugging purpose I changed system time, set up in one week ago - and I've got infinite indexing. As soon, as I set time back - indexing has stopped.

user3567929
  • 97
  • 1
  • 4
0

in my case i had the emulator open with an app builded with previous files. Just close de emulator

David Rearte
  • 764
  • 8
  • 19
0

I've tried all the things listed, indexing is keep freezing. This helped me: If your indexing is freeze, and you have one or more swift process eating 99% of your cpu - just kill this swift task(s), wait a bit, and progress should move. It can repeats, until it reaches finish, in my case I killed the process 7 times, but at the end, indexing was completed!

David
  • 857
  • 1
  • 11
  • 25
0

For me it was circular inheritance causing the issue:

class CustomButton: CustomButton {
    ...
}

And various other recent find/replace errors in the code. Xcode wasn't highlighting them as errors and just kept indexing.

Charlie S
  • 4,366
  • 6
  • 59
  • 97
0

I recognized today, that running Apples Playgrounds application alongside Xcode I have that very same symptoms. and maybe that was the root cause in earlier situations. So in my case, closing Playgrounds did the trick.

edsa-steffen
  • 179
  • 1
  • 9
0

Non of the answers helped in my case. I tried multiple things like:

Device and Simulators - Wifi connected device

Disconnect all devices which can be connected with wirelessly.

Removing Xcode's cache, derived data:

rm -frd ~/Library/Developer/Xcode/DerivedData/*
rm -frd ~/Library/Caches/com.apple.dt.Xcode/*

Deleting defaults for Xcode

defaults delete com.apple.dt.Xcode

Solution

What helped in my case was to rename project folder. Once I did that everything started working again.

Marek Staňa
  • 522
  • 7
  • 10
0

In my case, I tried all the suggestions I found on the internet but nothing worked.

The solution that worked for me was to run another project on xcode that could be indexed, and once indexing was done I closed xcode and opened it on the first project that had indexing issues, and it works.

No idea why, but it works :)

EL OUFIR Hatim
  • 267
  • 2
  • 16
-1

This is a workaround I posted on another stackoverflow thread related to Xcode indexing problem. This question looks to be more swift related but my workaround can probably be useful here too. So here it is. My project is very big (merging objective c, c++, swift, and java files with j2obj) and none of the answers here solved the indexing problem. The idea is to limit the CPU usage of the Xcode indexing process with an external tool like cputhrottle.

So first you need to install cputhrottle in terminal

brew install cputhrottle

Then limit the Xcode indexing process like this (20 = 20%)

sudo cputhrottle $(pgrep -f com.apple.dt.SKAgent) 20

I've exposed my "solution" here with more details : How to prevent Xcode using 100% of CPU when indexing big projects

jptsetung
  • 9,064
  • 3
  • 43
  • 55