简体   繁体   English

枚举案例“国家”不是“RegisterViewController.options”类型的成员? 为什么会这样?

[英]Enum case 'country' is not a member of type 'RegisterViewController.options?' why is it so?

I want to change datasource array of tableview according to the button clicked.我想根据单击的按钮更改 tableview 的数据源数组。 I use enumeration method to identify or mark the button I click.我使用枚举方法来识别或标记我点击的按钮。

But in the code it shows an error like:但是在代码中它显示了如下错误:

Enum case 'country' is not a member of type 'RegisterViewController.options?'枚举案例“国家”不是“RegisterViewController.options”类型的成员?

I can't figure it out.我想不通。

options is the enum containing 3 cases. options是包含 3 个案例的枚举。

//enum declaration

enum options {
    case dawat
    case country
    case nationality
}

var lastSelection:options?

//code
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! PIckerTableViewCell

    if isSearching == true {
        signUser.tableData = signUser.filteredArray
        cell.lblTitle.text = signUser.tableData[indexPath.row]
    } else {
        switch lastSelection { 
        case options.country:
            signUser.unfilteredArray = signUser.countries
        case options.dawat:
            signUser.unfilteredArray = signUser.dawatTitles
        case options.nationality:
            signUser.unfilteredArray = signUser.nationalities
            signUser.tableData = signUser.unfilteredArray
            cell.lblTitle.text = signUser.tableData[indexPath.row]
        }
        return cell
    }
}

Either unwrap your optional property before using it在使用它之前打开您的可选属性

if let last = lastSelection {
    switch last { 
        case options.country:
            signUser.unfilteredArray = signUser.countries
        //and so on...                
    }
} else {
    //?
}

or add a default case to your enum或在您的枚举中添加默认大小写

enum options {
    case dawat
    case country
    case nationality
    case notSet
}

var lastSelection = options.notSet

and then use it in the switch然后在开关中使用

switch lastSelection { 
    //other case...
    case options.notSet:
        //?
    }
}

hey check this answer:嘿检查这个答案:

enum  options  {
    case dawat
    case country
    case nationality
    case none

}

var lastSelection = options.none

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM