问题
I'm running Xcode 7.3.1. When building my Swift based project, it hangs on "Compiling Swift source files". I've tried various combination of deleting DerivedData
, clean, run, restarting Xcode, restarting OS X, none seem to work. Any ideas?
回答1:
I made a class extend itself. That also causes Swift compiler to get stuck in a loop without error:
class X: X
回答2:
Thanks for all commentors' suggestions. I narrowed it down to a map
's closure referencing a property that I had removed. Example:
var people: [Person] = ...
let foo = people.map { "\($0.name), \($0.age)" }
where Person
looks something like:
protocol Person {
var name: String { get }
var age: Int { get }
}
This all works fine. Then I removed age
while keeping the closure unchanged. This caused Xcode to become hopelessly confused. Probably related to the Swift's type inference.
回答3:
Try clean your Project Build Folder
- Hold down option key and got to Product -> Clean Build Folder ( where Clean used to be in the menu)
- If you are using CocoaPods delete your Workspace file and run
Pod Install
orPod Update
I think 2 is probably the cause.
回答4:
Change "Swift Compiler Optimization Level" in Build Settings from "Whole module optimization" to "Single file optimization". It may not be your problem, but it solved mine I was stuck with for half a day. It may just be a temporary bug in the recent Xcode version (8.2.1 was the one I have been using at the time I wrote this).
回答5:
I had the same problem. In my case it seems to be a result of applying too many nil coalescing actions. I was building a json item:
json = [ "item1": value1 ?? "",
"item2": value2 ?? "",
"item3": value3 ?? "",
...
"item14": value14 ?? "" ]
This wouldn't compile. When I removed all the nil coalescing so that it looked like the following, it compiled fine.
json = [ "item1": value 1,
"item2": value 2,
"item3": value 3,
...
"item14": value 14 ]
I did not try to figure out the cutoff point for the number of items before it got stuck.
回答6:
There seems to be various possible causes of extremely long compilation time. Corner or edge cases are everywhere. So the best way is to observe and investigate your own case.
Although being mentioned by others in comments, but steps below still worths more attention:
- Run project
- Switch to Report Navigator (command + 9), and select current running
Build
task. See which source file is taking up a lot of compiling time. - Check recent commit history of that source file. Investigate the possible cause.
回答7:
In my case the problem was during JSON parsing. I was sending an optional value in a dictionary parameter during JSON parsing.
回答8:
Watching the Report Navigator helped me find the problem. In my case, the issue was that I tried to add auto layout constraints to a programmatically added subview of a UITableView
in a UITableViewController
.
回答9:
xcode seems to have a problem concatenating more than 5 strings. See this: Xcode freezes when trying to execute this in a Swift playground? The given workaround solved my problem
回答10:
In my case XCode stucks on big dictionary literal:
requestParameters = [
"asset" : "...",
"user" : "...",
// about 15 additional keys
]
The problem was fixed after replacing this part by:
var requestParameters = [String : Any]()
requestParameters["asset"] = "..."
requestParameters["user"] = "..."
// about 15 additional keys
回答11:
So I believe that in most cases it's well known dictionary literal type interference problem.
Code like this:
let params = [
"title": title, "desc": desc, "velikost": velikost,
"cena": cena, "vykon": vykon, "telefon": telefon,
"rokVyroby": rokVyroby, "stkDo": stkDo,
"zemePuvodu": zemePuvodu, "najetoKilometru": najetoKilometru,
"stav": stav, "ZnackaId": znackaId,
"VyrobceId": vyrobceId,
"category": categoryId, "subCategory": subCategoryId,
"modely[]": modelId, "prodejNakup": prodejNakup,
"mena": mena, "isNovy": isNovy, "serviska": serviska,
"abs": abs, "technicak": technicak,
]
Have to be written better always like this:
let params: [String: String] = [
"title": title, "desc": desc, "velikost": velikost,
"cena": cena, "vykon": vykon, "telefon": telefon,
"rokVyroby": rokVyroby, "stkDo": stkDo,
"zemePuvodu": zemePuvodu, "najetoKilometru": najetoKilometru,
"stav": stav, "ZnackaId": znackaId,
"VyrobceId": vyrobceId,
"category": categoryId, "subCategory": subCategoryId,
"modely[]": modelId, "prodejNakup": prodejNakup,
"mena": mena, "isNovy": isNovy, "serviska": serviska,
"abs": abs, "technicak": technicak,
]
But I believe that even shorter literals are issue and to save time by waiting for build it's better to always define type for dictionary literals so compiler don't have to find out by himself, as he obviously struggle. PS: I think apple engineers have some serious personal problems there, they have to hire some people from Jatbrains or maybe even me ;) to focus on important things and not waste time on discussions on how Swift have to be different from others...
来源:https://stackoverflow.com/questions/38174514/xcode-hangs-on-compiling-swift-source-files