检查字典以包含nil值和空值

时间:2018-11-29 04:20:34

标签: swift dictionary

我想检查字典中是否包含nil或空值(nil和“”),并在用户界面上显示 我的字典包含字符串和整数值

let params = [
        "token":APItoken.getToken(),
        "gender":gender_id,
        "car_number":number_car,
        "car_model":car_model,
        "year_of_birth":1998,
        "car_year":create_year,
        "seats_number":sits,
        "facilities":fac,
        "type":typeID
        ] as [String : Any]

我尝试使用此代码,但不起作用

  if params.values.filter({ $0 == nil }).isEmpty
    {
        print("full")
    }
    else
    {
        print("empty")
    }

2 个答案:

答案 0 :(得分:2)

inventory = {847502: ['APPLES 1LB', 2, 50], 847283: ['OLIVE OIL', 1, 100], 839529: ['TOMATOS 1LB', 4, 25], 483946: ['MILK 1/2G', 2, 50], 493402: ['FLOUR 5LB', 2, 50], 485034: ['BELL PEPPERS 1LB', 3, 50] } def total_and_number(dict): total = 0 for values in dict.values(): total += values[1]*values[2] return(total) total_and_number(inventory) 的类型从 [String:Any]更改为[String:Any?] ,以将值与nil 进行比较

Dictionary

答案 1 :(得分:0)

您应该将参数类型[String:Any]更改为[String:Any?] 您的nil检查代码正确无误,只需要添加空字符串条件 就像:

ProductTask::select('product_tasks.id as product_task_id', 
                    'tasks.id as tasks_id', 'tasks.task_name as label', 
                    'tasks.serial_id as serial_id', * INSERT missing query here *)
            ->JOIN('tasks', 'tasks.id', 'product_tasks.task_id') 
            ->WHERE('product_tasks.product_id', 1) 
            ->get();