枚举在自定义初始值设置中不起作用 [英] Enum not working in custom initializer

查看:126
本文介绍了枚举在自定义初始值设置中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类初始化器中使用一个名为BuildingType的枚举变量(名为Building类)。



此枚举定义在类外,因为我也想在其他地方使用它。



自动完成



示例代码:

 

code> enum BuildingType {
case Flat,House,Villa
}

类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,而不是枚举值。



我必须在这里做错事,

解决方案

这是一个很奇怪的错误



枚举成一个初始化器的参数,自动完成将失败,而不是在键入 Enum。后建议枚举情况,它会列出你调用的类的实例成员初始化。如果你尝试并使用单点语法( .Case ),自动完成也会失败,但不显示实例成员的列表,它不会显示任何东西。



我最初认为它可能与您的枚举和类的命名有关( BuildingType & Building ),但是不是这样。



这个错误只出现在初始化(其中一个是枚举)。我无法使用单个参数初始化程序重现此问题。



重现性似乎取决于初始化程序是否完整。如果它有所有参数名称和值(枚举除外)定义,我正在考虑一个初始化是完整。例如:

  //不完整(foo是众多参数之一)
let baz = Baz(foo:Foo 。

//半完成(在为第二个参数赋值之前)
let baz = Baz(foo:Foo。,string:< String Placeholder>)

//完成
let baz = Baz(foo:Foo。,string:)

//完成(注意缺少最后一个括号)
let baz = Baz(param:0,foo:Foo。

这里是我的测试设置,Swift 2.2):

 枚举Foo {
case Bar
}
$ b b class Baz {
var iReallyShouldntBeDisplayedHere = 0
init(foo:Foo,string:String){}
init(foo:Foo){}
}

这里是一个列表,我发现错误是&

  //枚举是唯一的参数

// CORRECT:接受初始化器的自动完成,然后键入Foo。为枚举情况带来自动完成选项
let baz = Baz(foo:Foo。)

// CORRECT:自己键入初始化器),然后在第一个参数中键入Foo。为枚举情况打开自动完成选项
let baz2 = Baz(foo:Foo。


//枚举是许多的一个参数

// INCORRECT:接受初始化器的自动完成(所以它是半完成),然后键入Foo 。在第一个参数中引发Baz的实例成员(iReallyShouldntBeDisplayedHere)
let baz3 = Baz(foo:Foo。,string:< String Placeholder>)

// CORRECT:初始化者自己(所以它'不完整'),并键入Foo。在第一个参数中显示枚举的情况
let baz4 = Baz(foo:Foo。


//单点语法(其中枚举是许多的一个参数)

// CORRECT:自己键入初始化器(因此它是'不完整'),在第一个参数中键入。打开枚举例
let baz5 = Baz(foo :.

// CORRECT:接受初始化器的自动完成(因此是半完成),然后在第一个参数中输入。打开枚举例
let baz6 = Baz(foo:。,string: < String Placeholder>)

// INCORRECT:在初始化器完成后通过在第一个参数中键入。修改foo:参数不会生成自动完成列表
let baz7 = Baz(foo:。,string:)

c $ c> foo:是最后一个参数,但是在这种情况下,初始化器总是必须是完整的,所以它总是失败我尝试使用初始化器取3个参数,但它似乎有与具有2个参数的初始化器相同的行为。



如果任何人知道有更多的情况下可以重现这个错误,我很想知道!

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

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

The autocomplete for this enum is not working properly when initializing the variable typeOfBuilding.

Example code:

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." (when initializing myBuilding) 'floors' and 'type' are shown, and not the enum values.

I must be doing something wrong here but what?

解决方案

This is a pretty weird bug

It occurs when you attempt to pass an enum into an argument of an initialiser, autocomplete will fail and instead of suggesting the enum cases after typing Enum., it will list the instance members of the class you’re calling the initialiser on. If you try and use the single dot syntax (.Case), autocomplete will also fail, but instead of displaying the list of instance members, it simply won't display anything.

I initially thought it may have had something to do with the naming of your enum and class (BuildingType & Building), but this is not the case.

This bug only appears to be present in initialisers with multiple arguments (one of which being an enum). I couldn’t reproduce this issue with single argument initialisers.

The reproducibility appears to depend on whether the initialiser is 'complete'. I'm considering an initialiser to be 'complete' if it has all argument names and values (except the enum) defined. For example:

// 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 where I've found the bug does & doesn't occur:

// 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: "")

I also tried this where foo: is the last argument, but the initialiser always has to be complete in that case, so it always fails. I did try with initialisers that take 3 arguments, but it appears to have the same behaviour as an initialiser with 2 arguments.

If anyone knows of any more cases where this bug can be reproduced, I'd love to know!

这篇关于枚举在自定义初始值设置中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆