如何检查字典值是否为空 [英] How to check dictionary value is empty or not

查看:67
本文介绍了如何检查字典值是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 swift 的新手,遇到的问题很少,我不明白如何检查字典值.

i am new to swift and facing little issue and i not understanding how to check dictionary value.

func doValidate(data:Dictionary<String,AnyObject>,isEmail : String) -> Bool {
    if(isEmail=="signup"){
        if( data["last_name"] == nil || data["email"] == nil || data["password"] == nil || data["first_name"] == nil){
            return false;
        }
        else{
            return true;
        }
    }
}

字典键总是不变的,每次字典键都存在但我如何检查

Dictionary key is always constant and every time dictionary key exists but how i can check

value of data["last_name"] is empty or not?

如果我使用循环,那么它可以工作,但为什么单个值不起作用?

if i used loop then its working but why single value not work?

for( myKey,myValue ) in data { 
    if(myValue as! String == ""){ // Work Perfect
        return false;
    }
}

推荐答案

如果您想检查字符串值,请检查长度.例如

Check length if you wanna check your string value. for example

func doValidate(data:Dictionary<String,AnyObject>,isEmail : String) -> Bool {
        if(isEmail=="signup"){
            if( data["last_name"]?.length == 0 || data["email"]?.length == 0 || data["password"]?.length == 0 || data["first_name"]?.length == 0){
                return false;
            }
            else{
                return true;
            }
        }
    }

这篇关于如何检查字典值是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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