Swift 枚举在自定义初始值设定项中不工作

Swift 枚举在自定义初始值设定项中不工作,swift,class,enums,initialization,Swift,Class,Enums,Initialization,我在类初始值设定项中使用了一个名为BuildingType的枚举变量(属于名为Building的类) 这个枚举是在类之外定义的,因为我想在其他地方也使用它 初始化变量typeOfBuilding时,此枚举的自动完成无法正常工作 示例代码: enum BuildingType { case Flat, House, Villa } class Building { var type : BuildingType = BuildingType.House var floor

我在类初始值设定项中使用了一个名为BuildingType的枚举变量(属于名为Building的类)

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

初始化变量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时),“楼层”和“类型”将显示,而不是枚举值

我一定是做错了什么?这是一个非常奇怪的错误 当您试图将枚举传递给初始化器的参数时,自动完成将失败,并且在键入
enum.
后,它将列出调用初始化器的类的实例成员,而不是建议枚举实例。如果尝试使用单点语法(
.Case
),autocomplete也会失败,但它不会显示实例成员列表,而不会显示任何内容

我最初认为它可能与枚举和类的命名有关(
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.
下面列出了我发现该错误发生和未发生的情况:

// 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”。将为枚举案例显示自动完成选项
设baz=baz(foo:foo.)
//更正:您自己键入初始化器(因此它是“不完整的”),然后键入“Foo”。在第一个参数中,会显示枚举案例的自动完成选项
让baz2=Baz(foo:foo)。
//Enum是许多参数中的一个
//不正确:接受初始化器的自动完成(因此它是“半完成”),然后键入“Foo”。在第一个参数中会显示Baz的实例成员(“iReallyShouldntBeDisplayedHere”)
设baz3=Baz(foo:foo.,字符串:)
//更正:您自己键入初始化器(因此它是“不完整的”),然后在第一个参数中键入“Foo”。将显示枚举情况
设baz4=Baz(foo:foo。
//单点语法(其中enum是多个参数中的一个)
//更正:您自己键入初始值设定项(因此它是“不完整的”),然后在第一个参数中键入“.”将显示枚举情况
设baz5=Baz(foo:)。
//正确:接受初始化器的自动完成(因此它是“半完成”),然后在第一个参数中键入“.”将显示枚举案例
设baz6=Baz(foo:,字符串:)
//不正确:在初始化器“完成”后,通过在第一个参数中键入“.”修改foo:参数不会生成自动完成列表
设baz7=Baz(foo:,字符串:“”)
我也尝试过这样做,其中
foo:
是最后一个参数,但在这种情况下初始化器必须是完整的,因此它总是失败。我尝试过使用3个参数的初始化器,但它似乎与使用2个参数的初始化器具有相同的行为


如果有人知道这个bug还会被复制,我很想知道!

Hmm..在我看来像个bug。虽然如果你只使用
.Flat
的较短形式,它会自动完成。如果你只键入“…typeOfBuilding:”(也就是说,就是点)怎么办?@CouchDeveloper As originaluser2如上所述,单点自动完成是有效的。这里的特点是
BuildingType.
自动完成到
Building
的实例成员,这自然是不应该的。@originaluser2:autocomplete不适用于.Flat。两个选项“typeOfBuilding=BuildingType.Flat”如下所示:“建筑类型=.Flat“尽管运行。所以唯一的问题是自动完成不起作用……所以,它对我来说也是一个bug。@originaluser2感谢您的努力,并期待可能的解决方案!感谢您如此广泛地测试此bug;非常感谢!我还没有报告此bug,但现在将报告。再次感谢!!@etri很乐意帮助:)
// 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: "")