Swift 如何将空字符串转换为空字符串
我不熟悉Swift 如何将空字符串转换为空字符串,swift,null,swift2,nsnull,Swift,Null,Swift2,Nsnull,我不熟悉Swift。我尝试使用这个快捷链接NSDictionary,但没有成功 数据: "end_time" = "<null>" 每一次它都是去else语句。可能是我需要将字符串转换为null或其他解决方案。你能帮我吗 谢谢。以下是在swift中检查空值的方法: let time = endTime["end_time"] if time != "<null>" { print("time is not <null>") } else {
Swift
。我尝试使用这个快捷链接NSDictionary
,但没有成功
数据:
"end_time" = "<null>"
每一次它都是去else语句。可能是我需要将字符串转换为null或其他解决方案。你能帮我吗
谢谢。以下是在
swift
中检查空值的方法:
let time = endTime["end_time"]
if time != "<null>" {
print("time is not <null>")
}
else
{
print("time is <null>")
}
let time=endTime[“end_time”]
如果有时间!="" {
打印(“时间不是”)
}
其他的
{
打印(“时间是”)
}
您可以创建一个NilCheck控制器来检查各种数据类型的nil或null。例如,我创建了一个函数来从字典中删除null[if any],并将字典数组存储在Userdefaults中。请随时提问:)
!=nil
它会导致相同的结果,endTime[“end\u time”]等于“it而不是nil”。该值也可以是一个文本字符串”
您可以与字符串“
进行比较,如下所示:如果endTime[“end\u time”]==”
当end\u time不为“NSNull”(当它有值时)的格式是什么有一个运算符可以完成您的高级函数所做的str??“
,num??0
。
let time = endTime["end_time"]
if time != "<null>" {
print("time is not <null>")
}
else
{
print("time is <null>")
}
func removeNilAndSaveToLocalStore(array : [[String:Any]]) {
var arrayToSave = [[String:Any]]()
for place in array {
var dict = [String:Any]()
dict["AreaId"] = NilCheck.sharedInstance.checkIntForNil(nbr: place["AreaId"]! as? Int)
dict["AreaNameAr"] = NilCheck.sharedInstance.checkStringForNil(str: place["AreaNameAr"]! as? String)
dict["AreaName"] = NilCheck.sharedInstance.checkStringForNil(str: place["AreaName"]! as? String)
dict["GovernorateId"] = NilCheck.sharedInstance.checkIntForNil(nbr: place["GovernorateId"]! as? Int)
arrayToSave.append(dict)
}
LocalStore.setAreaList(token: arrayToSave)
}
class NilCheck {
static let sharedInstance : NilCheck = {
let instance = NilCheck()
return instance
}()
func checkStringForNil(str : String?) -> String {
guard let str = str else {
return String() // return default string
}
return str
}
func checkIntForNil(nbr : Int?) -> Int {
guard let num = nbr else {
return 0 // return default Int
}
return num
} }