I am trying to have a button that checks if it has an icon if so do X other wise do Y
if button.currentImage!.isEqual(UIImage(named: "check")) {
print("X")
} else {
print("Y")
}
Since currentImage could be nil then you need to safely use ? instead of !
if button.currentImage?.isEqual(UIImage(named: "check")) ?? false {
print("X")
} else {
print("Y")
}
if it's only check if image exists then no compare
if button.currentImage != nil {
print("X")
} else {
print("Y")
}
Related
I want to check if myButton has a named image.
I try this but it doesn't work
if (myButton.currentImage?.isEqual(UIImage(named: "ButtonAppuyer.png")) != nil){
print("YES")
} else {
print("NO")
}
and this too doesn't work
if myButton.currentImage?.isEqual(UIImage(named: "ButtonAppuyer.png")){
print("YES")
} else {
print("NO")
}
Here is what I came up with in Swift 3.0.
if let myButtonImage = myButton.image(for: .normal),
let buttonAppuyerImage = UIImage(named: "ButtonAppuyer.png"),
UIImagePNGRepresentation(myButtonImage) == UIImagePNGRepresentation(buttonAppuyerImage)
{
print("YES")
} else {
print("NO")
}
This could be cleaned up a lot.
extension UIButton {
func hasImage(named imageName: String, for state: UIControlState) -> Bool {
guard let buttonImage = image(for: state), let namedImage = UIImage(named: imageName) else {
return false
}
return UIImagePNGRepresentation(buttonImage) == UIImagePNGRepresentation(namedImage)
}
}
Then use it
if myButton.hasImage(named: "ButtonAppuyer.png", for: .normal) {
print("YES")
} else {
print("NO")
}
in swift 4.2 and Xcode 10.1
buttonOne.image(for: .normal)!.pngData() == UIImage(named: "selected")!.pngData()
You can convert the button's image and the named image to NSDatas and compare the two data objects:
let imgData1 = UIImagePNGRepresentation(buttonImage)
let imgData2 = UIImagePNGRepresentation(namedImage)
let equal = imgData1 == imgData2
Note that this will only work if the two images are completely identical (e.g. come from the same source file); if one is a scaled down version of the other, it won't work.
Also, it should be mentioned that this can be a pretty expensive operation and should not be run frequently.
There is no way to retrieve the image name from a UI element. You will need to store the value you want to retrieve in another location.
For instance, you could create a custom subclass of UIButton that contains an additional property to store the image name or other identifying data.
if let ButtonImage = myButton.image(for: .normal),
let Image = UIImage(named: "ButtonAppuyer.png"),
UIImagePNGRepresentation(ButtonImage) == UIImagePNGRepresentation(Image)
{
print("YES")
} else {
print("NO")
}
if myCell.followButton.currentImage.isEqual(UIImage(named: "yourImageName")) {
//do something here
}
I'm trying to check the content of an array of labels but it not works.
To explain the program: I have 4 arrays of 4 UIlabels each one and every label contains a number in String. This function returns true if one label contains the number 16. I tried to use the function "contains" but it doesn't work because "16" it's a string and not a label.
Thanks
Example of declaration of array of labels:
Fila1 = [UILabel]()
Function Win:
func win() -> Bool {
for i in 0..<Fila1.count {
if(Fila1[i].text == "16") {
return true
} else if(Fila2[i].text == "16") {
return true
} else if(Fila3[i].text == "16") {
return true
} else if(Fila4[i].text == "16") {
return true
} else {
return false
}
}
return false
}
The issue is that you shouldn't have that else statement return false.
It should be:
if(Fila1[i].text == "16") {
return true
} else if(Fila2[i].text == "16") {
return true
} else if(Fila3[i].text == "16") {
return true
} else if(Fila4[i].text == "16") {
return true
} // end if here
You're exiting out of the loop too early. If the first element isn't what you are looking for you aren't checking the rest.
Your code is fine, you just need to remove the else condition that contains return false and the code should work as expected.
This is my code but it seems the conditional "if" check for UIImage being empty is not executing. Is it the wrong way of checking if UIImage is empty? I have also tried
if allStyleArrays[i][ii][iii] as UIImage? == nil {
...
}
And not working, please if none of those methods checks, what is the correct way?
Thanks.
Code:
for var i = 0; i <= allStyleArrays.count; i++ {
if i == allStyleArrays.count {
self.performSegueWithIdentifier("toStylistReviewAndConfirm", sender: self)
} else {
for var ii = 0; ii < allStyleArrays[i].count; ii++ {
for var iii = 0; iii < allStyleArrays[i][ii].count; iii++ {
if allStyleArrays[i][ii][iii] as UIImage? == UIImage(named: "") {
} else {
switch i {
case 0:
styleN[0].append(allStyleArrays[i][ii][iii])
//print("style 1 \(style1.count)")
break
case 1:
styleN[1].append(allStyleArrays[i][ii][iii])
//print("style 2 \(style2.count)")
break
default:
styleN[2].append(allStyleArrays[i][ii][iii])
//print("style 3 \(style3.count)")
break
}
}
}
}
}
}
try
if allStyleArrays[i][ii][iii].imageAsset == nil
I've tried implementing the dot or "point" in calculator I'm programming the windows 7 calculator as a reference to my calcu.
I've tried this in my code:
#IBAction func btnDot(sender: AnyObject) {
if(lblResult.text!.characters.count == 0) {
lblResult.text = "0."
}
else {
if(Float(lblResult.text!.lowercaseString.characters.contains(".")) == -1) {
lblResult.text = lblResult.text! + "."
}
}
lblResult.text = lblResult.text
}
but the function btnDot doesn't seem to work. What seems to be the problem here?
Note: The lblResult.text is the Id of my UILabel for the display of results
Try this code
if (lblResult.text?.characters.count == 0){
lblResult.text = "0."
}
else{
if lblResult.text!.rangeOfString(".") == nil{
lblResult.text = lblResult.text! + "."
}
}
If you want help you with your code you'll need to provide more info about the problem, now I just could give you a working example.
I am writing a puzzle game for an IOS. In my code I need to fill an array with some random (and non-random numbers) that will represent the main data structure.
func Random(r : Range<Int>) -> Int {
return Int(arc4random_uniform(UInt32(r.endIndex - r.startIndex))) + r.startIndex
} // function to generate random number
var arrWithColors = [Int]() // array that will hold the numbers
//function to that check if an array contains a number in a range already
func checkForNumberInArrayWithRange(r: Range <Int>, n: Int ) -> Bool {
for i in r.startIndex..<r.endIndex {
if arrWithColors[i] == n { return true }
}
return false
}
// here is the main function where i keep getting stuck. Out of let's say five times it would only work 3 or even less.
func randHexWithTag() {
var rNumber : Int = Random(0...5)
for i in 0...5 {
while (true) {
rNumber = Random(0...5)
if !checkForNumberInArrayWithRange(0...5, n: rNumber) {
break
}
}
arrWithColors[i] = rNumber
}
arrWithColors[10] = arrWithColors[1]
for i in 6...11 {
while(true) {
rNumber = Random(0...5)
if ((rNumber == arrWithColors[0] && i == 9) || (rNumber == arrWithColors[2] && i == 11)) {
continue
}
if !checkForNumberInArrayWithRange(6...11, n: rNumber) {
break
}
}
if (i != 10) {
arrWithColors[i] = rNumber
}
}
arrWithColors[13] = arrWithColors[4]
for i in 12...17 {
while(true) {
rNumber = Random(0...5)
if (rNumber == arrWithColors[3] && i == 12) || (rNumber == arrWithColors[5] && i == 14) {
continue
}
if !checkForNumberInArrayWithRange(12...17, n: rNumber) {
break
}
}
if (i != 13) {
arrWithColors[i] = rNumber
}
}
}
The above code will ALWAYS fail during the first call to checkForNumberInArrayWithRange on this line:
if arrWithColors[i] == n { return true }
That is because arrWithColors is empty, and index i is out of range