Enum does not work in user initializer

I am using an enum variable called BuildingType in the initializer of a class (Building class).

This enumeration is defined outside the class, because I want to use it in other places.

Autocomplete of this enumeration does not work properly when initializing the typeOfBuilding variable.

Code example:

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) 

So, if I type "... typeOfBuilding: BuildingType." (upon initialization of myBuilding) "floors" and "type" are displayed, not enumeration values.

I have to do something wrong here, but what?

+5
source share
1 answer

This is a rather strange mistake.

This happens when you try to pass enum to the initializer argument, autocomplete will fail and instead of suggesting enum cases after entering Enum. , it will list the members of the class instance that you are calling the initializer on. If you try to use the single-point syntax ( .Case ), autocompletion will also fail, but instead of displaying a list of instance members, it just won't display anything.

Initially, I thought that this could have something to do with the name of your enum and class ( BuildingType and Building ), but that is not the case.

This error is present only in initializers with several arguments (one of which is an enumeration). I could not reproduce this problem with single argument initializers.

Reproducibility seems to depend on whether the initializer is "full". I consider the initializer to be "full" if it has all the names and values โ€‹โ€‹of the arguments (except for the enumeration). For instance:

 // 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. 

Here is my test setup (Xcode 7.3, Swift 2.2):

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

And here is a list of cases when I found an error, and does not happen:

 // Enum is the only argument // CORRECT: accepting the initialiser autocomplete (so it 'complete'), then typing "Foo." brings up autocomplete options for enum cases let baz = Baz(foo: Foo.) // CORRECT: typing the initialiser yourself (so it '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 autocomplete (so it 'semi-complete'), then typing "Foo." in the first parameter brings up Baz instance members ("iReallyShouldntBeDisplayedHere") let baz3 = Baz(foo: Foo., string: <String Placeholder>) // CORRECT: typing the initialiser yourself (so it '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 'incomplete'), and typing "." in the first parameter brings up enum cases let baz5 = Baz(foo:. // CORRECT: accepting the initialiser autocomplete (so it '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: "") 

I also tried this when foo: is the last argument, but the initializer should always be complete in this case, so it always fails. I tried with initializers that take 3 arguments, but it looks like they have the same behavior as an initializer with two arguments.

If anyone knows about any cases where this error can be reproduced, I would like to know!

+1
source

Source: https://habr.com/ru/post/1247613/


All Articles