2017-08-11 50 views
-1
中創建枚舉

我想在我的結構中創建枚舉。我想:Swift在struct

​​

然後我做了:手機 - - 未解決的標識符的使用

let tesEnum = ProfileTextfieldItem(txtfPlaceholder: "o.ivanova", bottomTxt: "Логин Skype", enumType: phone) 

但是編譯器並沒有讓我跑,它說。

+0

您初始化不匹配反正聲明。代碼不能編譯。而'enum'類型應該以大寫字母('ModelType')開頭。這避免了'modelType'類型與變量名稱'modelType'的混淆。 – vadian

+0

@vadian謝謝你,但如果可能的話,我希望看到你對該任務的表示。 –

+0

我的表示等於paper1111's。 – vadian

回答

2

取而代之的是,這裏是你應該做的:

struct ProfileTextfieldItem { 

    var txtfPlaceholder: String 
    var bottomTxt: String 
    var modelType: ModelType 

    enum ModelType { 
     case phone 
     case skype 
    } 
} 

在斯威夫特,enum s爲類型,它們可用於就像class ES和struct秒。本作的一個實例,這樣做:

let tesEnum = ProfileTextfieldItem(txtfPlaceholder: "o.ivanova", bottomTxt: "Логин Skype", modelType: .phone) 

注意第三個參數在前面有一個.,被稱爲modelType,不enumType

+0

很好的答案!謝謝。 –

1

應定義結構是這樣的:

struct ProfileTextfieldItem { 

    var txtfPlaceholder: String 
    var bottomTxt: String 
    var modelType : ModelType 

    enum ModelType { 
     case phone 
     case skype 
    } 
} 

和初始的VAR tesEnum這樣的:

let tesEnum = ProfileTextfieldItem(txtfPlaceholder: "o.ivanova", bottomTxt: "Логин Skype", modelType: .phone) 
+0

看起來你的答案類似於@ paper1111的回答。 –

+0

@AnhPham它看起來像它複製 –