我使用了这段代码
if let breedVaccRecorddStr = self.petInfoDict.objectForKey("breedvaccinationrecord") {
}else{
ALToastView.toastInView(UIApplication.sharedApplication().keyWindow, withText: "No Data Found")
}
这是我的回复
{
breed = dabour;
breedvaccinationrecord = "";
"city_registration" = 123146515;
"date_of_birth" = "12-10-2014";
"emergency_contacts" = 1245749876;
gender = male;
"owner_name" = Environmental;
"pt_id" = 4150;
"pt_images" = "";
"pt_name" = demo123;
ptinfo = "http://www.petcsi.com/wp-content/uploads/2015/12/jacky42.jpg";
"prop_name" = "Bella Vida Estates";
"qr_tag" = 5215653454;
species = test;
"vaccination_records" = test123;
vaccinationrecord = "";
"vet_info" = "";
},
常量不能为零,所以它执行if block。但我期待输出“没有找到数据”。他们可能是那个密钥中的图像网址如何检查这个?
答案 0 :(得分:1)
你的问题不是很清楚,但我猜你的问题是breedvaccinationrecord
包含一个空字符串,而不是nil。 if let
构造本身只测试nil,这就是为什么它不适合你。但是,您也可以使用where
子句:
let value = self.petInfoDict.objectForKey("breedvaccinationrecord")
if let breedVaccRecorddStr = value where value != "" {
// do your stuff
} else {
ALToastView.toastInView(UIApplication.sharedApplication().keyWindow, withText: "No Data Found")
}
如果else
为零或空字符串,则会执行breedvaccinationrecord
。
答案 1 :(得分:0)
你应该编写另一个if if语句来表示你期望的nil值。如:
if let imageString = self.petInfoDict.objectForKey("ptInfo") as String {
//Do something with the string
}
else {
}