swift – Enum不在自定义初始化程序中工作
我在类初始值设定项(名为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”和“type”,而不是枚举值. 我一定是在做错事但是什么? 解决方法
这是一个非常奇怪的错误
当您尝试将枚举传递给初始化器的参数时,它会发生,自动完成将失败,而不是在键入枚举后建议枚举案例.它将列出您正在调用初始化器的类的实例成员.如果您尝试使用单点语法(.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个参数的初始化程序具有相同的行为. 如果有人知道可以再现这个bug的更多案例,我很想知道! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |