【问题标题】:Enum not working in custom initializer枚举在自定义初始化程序中不起作用
【发布时间】:2016-08-14 15:04:07
【问题描述】:

我在类初始化器(一个名为 Building 的类)中使用了一个名为 BuildingType 的枚举变量。

这个枚举是在类之外定义的,因为我也想在其他地方使用它。

初始化变量 typeOfBuilding 时,此枚举的自动完成功能无法正常工作。

示例代码:

enum BuildingType {
    case Flat, House, Villa
}

class Building {
    var type : BuildingType = BuildingType.House
    var floors : Int = 1

    init(typeOfBuilding : BuildingType, numFloors : Int) {
        self.type = typeOfBuilding
        self.floors = numFloors
    }
}

var myBuilding : Building = Building(typeOfBuilding: BuildingType.Flat , numFloors: 3)

所以如果我输入“... typeOfBuilding: BuildingType”。 (初始化 myBuilding 时)显示“楼层”和“类型”,而不是枚举值。

我一定是在这里做错了什么,但是什么?

【问题讨论】:

  • 嗯.. 对我来说看起来像一个错误。虽然如果您只使用 .Flat 的较短形式,它会自动完成。
  • 什么,如果你只输入“... typeOfBuilding: .” (也就是说,只是 dot)?
  • @CouchDeveloper 正如上面提到的 originaluser2,单点自动补全有效。这里的特点是 BuildingType. 自动完成到 Building 的实例成员,它自然不应该这样做。
  • @originaluser2:自动完成功能不适用于 .Flat。两个选项“typeOfBuilding = BuildingType.Flat”作为“typeOfBuilding = .Flat”运行。所以唯一的问题是自动完成功能不起作用......所以,它对我来说也是一个错误。
  • @originaluser2 感谢您的努力并期待可能的解决方案!

标签: swift class enums initialization


【解决方案1】:

这是一个非常奇怪的错误

当您尝试将枚举传递给初始化程序的参数时会发生这种情况,自动完成将失败,并且在键入 Enum. 后不会提示枚举案例,它会列出您正在调用初始化程序的类的实例成员在。如果您尝试使用单点语法 (.Case),自动完成功能也会失败,但不会显示实例成员列表,而是不会显示任何内容。

我最初认为这可能与您的枚举和类的命名有关(BuildingType & Building),但事实并非如此。

这个错误似乎只出现在具有多个参数(其中一个是枚举)的初始化程序中。我无法使用单参数初始化程序重现此问题。

重现性似乎取决于初始化程序是否“完整”。如果初始化器定义了所有参数名称和值(枚举除外),我正在考虑它是“完整的”。例如:

// Incomplete (foo is one argument of many)
let baz = Baz(foo: Foo.

// Semi-Complete (before you assign the second parameter a value)
let baz = Baz(foo: Foo., string: <String Placeholder>)

// Complete
let baz = Baz(foo: Foo., string: "")

// Complete (note the lack of the last bracket)
let baz = Baz(param: 0, foo: Foo.

这是我的测试设置(Xcode 7.3、Swift 2.2):

enum Foo {
    case Bar
}

class Baz {
    var iReallyShouldntBeDisplayedHere = 0
    init(foo:Foo, string:String) {}
    init(foo: Foo) {}
}

以下是我发现错误发生和未发生的情况列表:

// Enum is the only argument

// CORRECT: accepting the initialiser's autocomplete (so it's 'complete'), then typing "Foo." brings up autocomplete options for enum cases
let baz = Baz(foo: Foo.)

// CORRECT: typing the initialiser yourself (so it's 'incomplete'), then typing "Foo." in the first parameter brings up autocomplete options for enum cases
let baz2 = Baz(foo: Foo.


// Enum is one argument of many

// INCORRECT: accepting the initialiser's autocomplete (so it's 'semi-complete'), then typing "Foo." in the first parameter brings up Baz's instance members ("iReallyShouldntBeDisplayedHere")
let baz3 = Baz(foo: Foo., string: <String Placeholder>)

// CORRECT: typing the initialiser yourself (so it's 'incomplete'), and typing "Foo." in the first parameter brings up enum cases
let baz4 = Baz(foo: Foo.


// Single dot syntax (where enum is one argument of many)

// CORRECT: typing the initialiser yourself (so it's 'incomplete'), and typing "." in the first parameter brings up enum cases
let baz5 = Baz(foo:.

// CORRECT: accepting the initialiser's autocomplete (so it's 'semi-complete'), then typing "." in the first parameter brings up enum cases
let baz6 = Baz(foo:., string: <String Placeholder>)

// INCORRECT: modifying the foo: argument once the initialiser is 'complete' by typing "." in the first parameter doesn't generate the autocomplete list
let baz7 = Baz(foo:., string: "")

我也尝试过 foo: 是最后一个参数,但在这种情况下初始化程序总是必须是完整的,所以它总是失败。我确实尝试使用带有 3 个参数的初始化器,但它似乎与带有 2 个参数的初始化器具有相同的行为。

如果有人知道更多可以重现此错误的情况,我很想知道!

【讨论】:

  • 感谢您如此广泛地测试此错误;非常感激!我还没有报告这个错误,但现在会报告。再次感谢!!
  • @etri 乐于助人:)
猜你喜欢
  • 2015-02-08
  • 2017-04-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多