无法检查我的变量是否为 nil SWIFT
Cannot check if my variable is nil or not SWIFT
我有一个很奇怪的问题。
我需要检查我的二进制数据(在我的核心数据库中)是否为零。
在控制台中,如果我直接打印变量,它是零。
但是如果我使用if/else条件来检查它,我的变量不是零。
println(self.selectedPost!.attachment?.image) // ->print nil
if let mybinary = self.selectedPost!.attachment?.image {
println("mybinary is not nil") // -> print "mybinary is not nil"
} else {
println("mybinary is nil")
}
编辑
同样的结果使用:
if self.selectedPost!.attachment?.image != nil {...
println("mybinary is not nil") // -> print "mybinary is not nil"
} else {
println("mybinary is nil")
}
我不明白为什么。
此问题仅在发布模式下出现,在调试模式下不会出现。
您需要与 !=nil
确认
就目前而言,myBinary
的值是可选的。您需要检查其内容。
您也可以尝试让我们as?
在您的 if 语句中转换数据。
println(self.selectedPost!.attachment?.image) // ->print nil
if let mybinary = self.selectedPost!.attachment?.image as? NSData {
println("mybinary is not nil") // -> print "mybinary is not nil"
} else {
println("mybinary is nil")
}
如果可能,as? NSData
将施放mybinary
。如果它是 nil 这将 return nil 而不是可选类型。
我有一个很奇怪的问题。 我需要检查我的二进制数据(在我的核心数据库中)是否为零。
在控制台中,如果我直接打印变量,它是零。 但是如果我使用if/else条件来检查它,我的变量不是零。
println(self.selectedPost!.attachment?.image) // ->print nil
if let mybinary = self.selectedPost!.attachment?.image {
println("mybinary is not nil") // -> print "mybinary is not nil"
} else {
println("mybinary is nil")
}
编辑
同样的结果使用:
if self.selectedPost!.attachment?.image != nil {...
println("mybinary is not nil") // -> print "mybinary is not nil"
} else {
println("mybinary is nil")
}
我不明白为什么。
此问题仅在发布模式下出现,在调试模式下不会出现。
您需要与 !=nil
就目前而言,myBinary
的值是可选的。您需要检查其内容。
您也可以尝试让我们as?
在您的 if 语句中转换数据。
println(self.selectedPost!.attachment?.image) // ->print nil
if let mybinary = self.selectedPost!.attachment?.image as? NSData {
println("mybinary is not nil") // -> print "mybinary is not nil"
} else {
println("mybinary is nil")
}
如果可能,as? NSData
将施放mybinary
。如果它是 nil 这将 return nil 而不是可选类型。