Swift编译器挂起了!这是一个错误吗? [英] Swift compiler hangs! Is it a bug?

查看:83
本文介绍了Swift编译器挂起了!这是一个错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一次,当我在开发一个Swift项目时,Xcode在状态栏中遇到了Compiling Swift source消息。无论我等多久,汇编都没有完成。我回滚了我最近的更改,很快意识到编译器混淆的是一个非常简单的枚举构造。下面是一个说明问题的Playground示例。

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.

创建一个新的Playground并粘贴此代码。你看到任何输出吗?

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")

在我的机器上忙碌指标永远在旋转,没有消息。我在Xcode 6.1(6A1052c)和Xcode 6.2-beta(6C86e)上尝试了这个。

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).

这看起来像是Swift编译器中的错误吗?或者我的代码有问题吗?

Does this look like a bug in Swift compiler? Or there is some problem in my code?

更新:

几个人注意到我在 getAllowedFood 函数中忘记了返回类型。但是,仅此修复并未解决问题。编译器仍然挂起。

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似乎无法解释你的字典。为字典提供一个帮助编译器的显式类型通常是一个好主意。

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.

以下添加un-freezes编译器:

The following addition "un-freezes" the compiler:

var allowedFood: [FastingType: [String]]


推荐答案

是的,这可以被视为编译器错误。编译器在查找字典中键的类型时遇到问题。通过为字典提供显式类型或确保使用 FastingType.NoFast 完全指定第一个值,可以消除无限循环行为。

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.

试试这个:

enum FastingType: Int {
    case NoFast=0, Vegetarian, FishAllowed, FastFree, Cheesefare
}

class Fasting
{
    var allowedFood:[FastingType: [String]] = [
        .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]!
    }
}

更改:


  1. 给予 allowedFood 类型 [FastingType:[String]] 因此它可以解释你的枚举值。

  2. 给予 getAllowedFood()返回类型。

  3. 打开字典查找,因为它们总是返回选项。

  1. Gave allowedFood the type [FastingType: [String]] so that it could interpret your enum values.
  2. Gave getAllowedFood() a return type.
  3. Unwrapped the dictionary lookup because they always return optionals.

或者,你可以有 getAllowedFood() return allowedFood [type] ?? [] 如果您的字典不详尽,这将更安全。

Alternatively, you could have getAllowedFood() to return allowedFood[type] ?? [] which would be safer if your dictionary is not exhaustive.

这篇关于Swift编译器挂起了!这是一个错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆