At one point, while I was working on a Swift project, the Xcode got stuck with "Compiling Swift source" message in the status bar. The compilation did not finish no matter how long I waited. I rolled back my recent changes, and soon realized that what confuses the compiler is a very simple enum construct. Below is a Playground example that illustrates the problem.
Create a new Playground and paste this code. Do you see any output?
// Playground - noun: a place where people can play
import UIKit
enum FastingType: Int {
case NoFast=0, Vegetarian, FishAllowed, FastFree, Cheesefare
}
class Fasting
{
var allowedFood = [
.NoFast: ["meat", "fish", "milk", "egg", "cupcake"],
.Vegetarian: ["vegetables", "bread", "nuts"],
.FishAllowed: ["fish", "vegetables", "bread", "nuts"],
.FastFree: ["cupcake", "meat", "fish", "cheese"],
.Cheesefare: ["cheese", "cupcake", "milk", "egg"]
]
func getAllowedFood(type: FastingType) -> [String] {
return allowedFood[type]
}
}
var fasting = Fasting()
println(fasting.getAllowedFood(.Vegetarian))
println("Hello world")
On my machine the busy indicator keeps spinning forever, and there are no messages. I tried this on both Xcode 6.1 (6A1052c) and Xcode 6.2-beta (6C86e).
Does this look like a bug in Swift compiler? Or there is some problem in my code?
UPDATE:
Several people noticed that I forgot return type in getAllowedFood
function. This fix alone, however, does not solve the problem. The compiler still hangs.
A workaround was suggested in comments:
Swift seems to have trouble interpreting your dictionary. It's usually a good idea to give dictionaries an explicit type to "help out" the compiler.
The following addition "un-freezes" the compiler:
var allowedFood: [FastingType: [String]]
Yes, this can be considered a compiler bug. The compiler is having trouble figuring out the type of the key in your dictionary. The infinite looping behavior can be eliminated by giving the dictionary an explicit type or making sure the first value is fully specified with
FastingType.NoFast
.Try this:
Changes:
allowedFood
the type[FastingType: [String]]
so that it could interpret your enum values.getAllowedFood()
a return type.Alternatively, you could have
getAllowedFood()
toreturn allowedFood[type] ?? []
which would be safer if your dictionary is not exhaustive.