Ios Swift:UIButton.currentImage在比较映像名称时不起作用
我试图检查UIButton中的图像名称,如下所示:Ios Swift:UIButton.currentImage在比较映像名称时不起作用,ios,swift,uibutton,swift5.2,xcode11.2.1,Ios,Swift,Uibutton,Swift5.2,Xcode11.2.1,我试图检查UIButton中的图像名称,如下所示: @IBAction func buttonTapped(_ sender: Any) { if xcodeButton.currentImage == UIImage(named: "xcode") { print("xcode image") } } 但是我在if语句中有一个断点,这是输出: po xcodeButton.currentImage ▿ Optional<UIImage> -
@IBAction func buttonTapped(_ sender: Any) {
if xcodeButton.currentImage == UIImage(named: "xcode") {
print("xcode image")
}
}
但是我在if
语句中有一个断点,这是输出:
po xcodeButton.currentImage
▿ Optional<UIImage>
- some : <UIImage:0x6000011a93b0 named(main: xcode) {500, 500}>
你们中有人知道为什么比较结果是假的吗?或者如何比较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.
}
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.
}