繁体   English   中英

Swift 编译器挂起! 这是一个错误吗?

[英]Swift compiler hangs! Is it a bug?

有一次,当我在处理 Swift 项目时,Xcode 卡在状态栏中的“正在编译 Swift 源代码”消息中。 不管我等了多久,编译都没有完成。 我回滚了我最近的更改,很快意识到混淆编译器的是一个非常简单的枚举结构。 下面是一个说明问题的 Playground 示例。

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

// 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) 上都试过了。

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

更新:

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

评论中提出了一种解决方法:

Swift 似乎无法解释您的字典。 为字典提供显式类型以“帮助”编译器通常是个好主意。

以下添加“解冻”编译器:

var allowedFood: [FastingType: [String]]

是的,这可以被视为编译器错误。 编译器无法确定字典中键的类型。 可以通过为字典提供显式类型或确保第一个值完全由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. 展开字典查找,因为它们总是返回可选项。

或者,您可以让getAllowedFood() return allowedFood[type] ?? [] return allowedFood[type] ?? []如果您的字典不是详尽无遗,这会更安全。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM