I've had a bug showing up in Xcode lately that's been interrupting my flow and making it nigh impossible to get anything done. I spent pretty much all day yesterday debugging it and finally fixed it last night. Since I had so much trouble figuring out what it was, I wanted to share the solution, in case it helps anyone else.

So here's the bug:

I'm using Xcode 7.3 on a Mac running OS X 10.11.4. I'm working on a project built entirely in Swift 2.2 with two Cocoapod libraries included: RealmSwift and Reveal.

When I'm typing (I have to be actively typing, not just looking at the code) in a file of this project, multiple times per minute—and usually within a few seconds after I start typing something—I see an error message across the top of the editor that says "An internal error occurred. Source editor functionality is limited. Attempting to restore..."

When the error message pops up, all of my code's syntax highlighting disappears, and sometimes (but not every time) code completion stops working. I also don't get error messages when I make mistakes in the code I'm writing while the error is showing.

After a few seconds the error message disappears, the syntax highlighting comes back, and everything is fine.

In trying to fix this bug, I tried so many things that didn't fix it. Here are a few:

Deleting derived data via Xcode and manually via Finder.

Closing and re-opening Xcode.

Completely deleting Xcode and installing from scratch. (I couldn't find another version to download, even in the Apple Member Center that wasn't 7.3, or I would have tried a different version as well.)

Making sure I was opening the workspace created when installing Cocoapods, not the Xcode project (I was).

Reporting the bug to Apple (I haven't heard back after several days, even after adding more detail to the bug report, so I didn't have much hope of Apple helping me out).

I even went so far as to create a whole new project to try to isolate the problem. At this point I was suspicious that Realm was the culprit, but after creating a new project I couldn't prove that was the case. The new project I created used Swift 2.2, obviously the same version of Xcode, and didn't have the same bug, even with both Reveal and RealmSwift included via Cocoapods. I even tried testing Realm code: creating an instance of Realm, writing to it and deleting items from it, subclasses Realm's base Object class.

At this point, the only differences between my project and the test project were the specific lines of code in my original app, and I was starting to think I'd have to recreate the entire app in my test project line by line until I figured out what was causing the problem.

Side note: I briefly used AppCode, which didn't have this bug, but these days I really don't like using it for various reasons. I kept ending up back in Xcode, trying to work around the bug until I stumbled onto a new lead that eventually helped me find the issue and fix it.

I had noticed a couple of times earlier that Xcode had shown me a 'Segmentation fault: 11' error when trying to run my app. Running the app again made the error go away, and it came up so rarely I hadn't paid attention to when or why. But the next time I noticed it, I realised the pattern: the seg 11 error showed up when I tried to run the app while the Xcode error message was showing at the top of the editor. If I waited for the error message to go away and the code syntax highlighting to come back, I could run the app no problem. So I started googling the seg 11 error in case I could find another way into this bug.

This error came with some more useful details about where the problem was, and this Stack Overflow answer gave me some idea that it probably was related to the initial Xcode bug I was trying to figure out:

I have encountered this and certain times expressions will just cause a crash, and splitting assignments up or even using objective-c will not cause a crash.

It can error on perfectly valid lines of code. Try simplifying and breaking up into multiple parts. For instance once it would crash trying to create a URL of two components and I had to create one string and then another, and it worked.

Although this wasn't super helpful in terms of figuring out the problem, it did give me some idea that it was to do with how I'd written my code, rather than issues linking to RealmSwift or my project set-up. The Segmentation error highlighted a big chunk of code in one of my files, so that's where I went looking.

At first I just started commenting out chunks of my code, then typing in the editor to test whether the error message would pop up. Each time I got the error message, I waited for the syntax highlighting to come back, commented out another chunk, and tested it again. Eventually I figured out this one line was the culprit:

let dataArray = data["results"] as? [[String: AnyObject]]

I'm dealing with JSON from an API in this app, (which has been a huge learning curve after how easy it is to deal with JSON in Objective-C) so this line is casting part of the JSON response to a Swift Array of Dictionary objects holding String keys and AnyObject objects.

At first I tried rewriting the line in various ways. I tried to separate the declaration of dataArray and the assignment to dataArray into different lines of code. I tried casting data["results"] to a simply Array. I couldn't find any way to change this code that would stop it causing the Xcode bug.

So I dug a little deeper.

I looked into my API client class that handles getting the JSON response from the API in the first place. To put the problem line above into context, here's how I call the getFolders from my API client class:

When I looked into the API client class where getFolders lives, I didn't see anything that was obviously wrong. So I googled "Swift JSON" and read over some tutorials of very basic JSON parsing in Swift. One thing all the tutorials had in common was that they were casting the JSON response to a native Swift object when they received it from the API. In the code above, my getFolders method is returning data which is of type AnyObject. It's in the problem line of code that I cast this AnyObject object to native Swift objects.

I followed a tutorial when setting up my API client class, and so far it hasn't given me any trouble (plus I was used to casting JSON to id in Objective-C), so I had no reason to think there was anything wrong with casting the initial JSON response to AnyObject, and casting it to native Swift objects at some other point.

I'm still not sure why this is a problem, rather than just bad practice, but when I changed the code in my API client class to cast the initial response to a native Swift object and return that, instead of AnyObject, the problem line of code no longer caused the Xcode bug.

The change was deceptively small, but here's what it was (inside my API client class, where I actually get the JSON data from the API):

This is one of the most frustrating debugging experiences I've ever had, but I hope this post might help someone else who has the same issue. And if you ended up here via googling a bug you're struggling with and this didn't help you, I'm so sorry. I know exactly what you're going through. Don't give up!

P.S. I make some stuff you might like: Exist, a personal analytics app to help you understand your life, and Larder, a bookmarking app for developers.