Int 类型的变量不能分配给定义为 Int 的枚举
A variable of type Int cannot be assigned to an enum which is defined as Int
Enum 在 class 的开头声明。然后我尝试将 TAG 值分配给枚举;但是编译器给出错误提示:'MenuItems is not convertible to Int'
您认为这里的问题是什么?
enum MenuItems : Int {
case menuItemEmail = 0
case menuItemName
case menuItemPassword
case menuItemAddPet
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: SettingsViewTableCell = tableView.dequeueReusableCellWithIdentifier("cell") as SettingsViewTableCell
cell.cellDescriptionText.tag = MenuItems.menuItemName
}
最后一行是编译时的错误。
具有原始值的枚举具有 rawValue
属性:
cell.cellDescriptionText.tag = MenuItems.menuItemName.rawValue
Enum 在 class 的开头声明。然后我尝试将 TAG 值分配给枚举;但是编译器给出错误提示:'MenuItems is not convertible to Int'
您认为这里的问题是什么?
enum MenuItems : Int {
case menuItemEmail = 0
case menuItemName
case menuItemPassword
case menuItemAddPet
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: SettingsViewTableCell = tableView.dequeueReusableCellWithIdentifier("cell") as SettingsViewTableCell
cell.cellDescriptionText.tag = MenuItems.menuItemName
}
最后一行是编译时的错误。
具有原始值的枚举具有 rawValue
属性:
cell.cellDescriptionText.tag = MenuItems.menuItemName.rawValue