gpt4 book ai didi

ios - 无法检查我的变量是否为零 SWIFT

转载 作者:行者123 更新时间:2023-11-30 14:06:34 25 4
gpt4 key购买 nike

我有一个非常奇怪的问题。我需要检查我的二进制数据(在我的核心数据库中)是否为零。

在控制台中,如果我直接打印变量,它是零。但如果我使用 if/else 条件来检查它,我的变量不是 nil。

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")
}

我不明白为什么。

此问题仅在 Release模式下出现,在 Debug模式下不会出现。

最佳答案

您也可以尝试联系我们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,则返回 nil 而不是可选类型。

关于ios - 无法检查我的变量是否为零 SWIFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32317143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com