我想这样在UIButton中检查图片名称。
@IBAction func buttonTapped(_ sender: Any) {
if xcodeButton.currentImage == UIImage(named: "xcode") {
print("xcode image")
}
}
但我在 "UIButton "中遇到了一个断点 if
语句,这就是输出。
po xcodeButton.currentImage
▿ Optional<UIImage>
- some : <UIImage:0x6000011a93b0 named(main: xcode) {500, 500}>
但如果我比较一下
po xcodeButton.currentImage == UIImage(named: "xcode")
false
有谁知道为什么比较的结果是返回false吗? 或者如何比较UIButton中的图片名称?
我真的很感谢你的帮助。
你应该使用 isEqual(_:)
来自 文件 滚到 比较图像 节
let image1 = UIImage(named: "MyImage")
let image2 = UIImage(named: "MyImage")
if image1 != nil && image1!.isEqual(image2) {
// Correct. This technique compares the image data correctly.
}
if image1 == image2 {
// Incorrect! Direct object comparisons may not work.
}