枚举不在自定义初始化程序中

时间:2016-04-21 12:08:10

标签: swift class enums initialization

我在类初始值设定项(名为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时)' floor'和'键入'显示,而不是枚举值。

我必须在这里做错事但是什么?

1 个答案:

答案 0 :(得分: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) {}
}

以下是我发现该错误的案例列表&amp;没有发生:

// 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个参数的初始化程序具有相同的行为。

如果有人知道可以复制此错误的更多案例,我很乐意知道!