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

etr*_*tri 5 enums initialization class swift

我在类初始值设定项(名为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)
Run Code Online (Sandbox Code Playgroud)

所以,如果我输入"... typeOfBuilding:BuildingType".(初始化myBuilding时)会显示"floor"和"type",而不是枚举值.

我一定是在做错事但是什么?

Ham*_*ish 1

这是一个非常奇怪的错误

\n\n

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

\n\n

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

\n\n

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

\n\n

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

\n\n
// Incomplete (foo is one argument of many)\nlet baz = Baz(foo: Foo.\n\n// Semi-Complete (before you assign the second parameter a value)\nlet baz = Baz(foo: Foo., string: <String Placeholder>)\n\n// Complete\nlet baz = Baz(foo: Foo., string: "")\n\n// Complete (note the lack of the last bracket)\nlet baz = Baz(param: 0, foo: Foo.\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
enum Foo {\n    case Bar\n}\n\nclass Baz {\n    var iReallyShouldntBeDisplayedHere = 0\n    init(foo:Foo, string:String) {}\n    init(foo: Foo) {}\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
// Enum is the only argument\n\n// CORRECT: accepting the initialiser\'s autocomplete (so it\'s \'complete\'), then typing "Foo." brings up autocomplete options for enum cases\nlet baz = Baz(foo: Foo.)\n\n// CORRECT: typing the initialiser yourself (so it\'s \'incomplete\'), then typing "Foo." in the first parameter brings up autocomplete options for enum cases\nlet baz2 = Baz(foo: Foo.\n\n\n// Enum is one argument of many\n\n// 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")\nlet baz3 = Baz(foo: Foo., string: <String Placeholder>)\n\n// CORRECT: typing the initialiser yourself (so it\'s \'incomplete\'), and typing "Foo." in the first parameter brings up enum cases\nlet baz4 = Baz(foo: Foo.\n\n\n// Single dot syntax (where enum is one argument of many)\n\n// CORRECT: typing the initialiser yourself (so it\'s \'incomplete\'), and typing "." in the first parameter brings up enum cases\nlet baz5 = Baz(foo:.\n\n// CORRECT: accepting the initialiser\'s autocomplete (so it\'s \'semi-complete\'), then typing "." in the first parameter brings up enum cases\nlet baz6 = Baz(foo:., string: <String Placeholder>)\n\n// INCORRECT: modifying the foo: argument once the initialiser is \'complete\' by typing "." in the first parameter doesn\'t generate the autocomplete list\nlet baz7 = Baz(foo:., string: "")\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

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

\n