1

My code is randomly generating 2 separate images on the same interface controller. I need to check if the images match each other, but i'm unsure how to go about this as they are being randomly generated. I have tried writing if statements such as:

if blueColour.setBackgroundImageNamed("colour\(randomImage).jpg") == mainBackgroundColour.setBackgroundImageNamed("mainColour\(randomMainBackground).jpg") {

        println("they match")

    } else {

        println("they dont match")

    }

but it doesn't seem to work, i get an error saying "binary operator '==' cannot be applied to two Void operands"

My code is below:

@IBOutlet var blueColour: WKInterfaceButton!
@IBOutlet var pinkColour: WKInterfaceButton!
@IBOutlet var greenColour: WKInterfaceButton!
@IBOutlet var yellowColour: WKInterfaceButton!

@IBOutlet var mainBackgroundColour: WKInterfaceGroup!

@IBOutlet var scoreLabel: WKInterfaceLabel!

var randomImage = UInt32()
var randomMainBackground = UInt32()

@IBAction func onePressedTest() {


    if blueColour.setBackgroundImageNamed("colour\(randomImage).jpg") == mainBackgroundColour.setBackgroundImageNamed("mainColour\(randomMainBackground).jpg") {

        println("they match")

    } else {

        println("they dont match")

    }

    randomImage = arc4random_uniform(4)
    blueColour.setBackgroundImageNamed("colour\(randomImage).jpg")
    randomImage = arc4random_uniform(4)
    pinkColour.setBackgroundImageNamed("colour\(randomImage).jpg")
    randomImage = arc4random_uniform(4)
    greenColour.setBackgroundImageNamed("colour\(randomImage).jpg")
    randomImage = arc4random_uniform(4)
    yellowColour.setBackgroundImageNamed("colour\(randomImage).jpg")

    randomMainBackground = arc4random_uniform(4)
    mainBackgroundColour.setBackgroundImageNamed("mainColour\(randomMainBackground).jpg")

}

NEW AMENDED CODE 20.04.2015:

import WatchKit
import Foundation

protocol WKInterfaceComparableImage {

func getImage()->UIImage;
func equalsImage(comparableWKObject:WKInterfaceComparableImage)->Bool;
}

class WKInterfaceButtonComparable : WKInterfaceButton, WKInterfaceComparableImage {

private var image:UIImage?;

override func setBackgroundImage(image: UIImage?) {
    self.image = image;
    super.setBackgroundImage(image);
}

func equalsImage(comparableWKObject: WKInterfaceComparableImage)->Bool {
    return self.image === comparableWKObject.getImage();
}

func getImage() -> UIImage {
    return image!;
}

}

 class WKInterfaceGroupComparable : WKInterfaceButton, WKInterfaceComparableImage {

private var image:UIImage?;

override func setBackgroundImage(image: UIImage?) {
    self.image = image;
    super.setBackgroundImage(image);
}

func equalsImage(comparableWKObject: WKInterfaceComparableImage)->Bool {
    return self.image === comparableWKObject.getImage();
}

func getImage() -> UIImage {
    return image!;
}
}

class ImageProvide {


private let MAX_RANDOM_NUMBER:UInt32 = 4

static let shared:ImageProvide = ImageProvide();

var images:[UIImage];

private init() {

    images = [];

    for i in 1...MAX_RANDOM_NUMBER {

        //get image with the best way to you
        images.append(UIImage(named: "colour\(i).jpg")!);
    }
}

func getRandomImage()->UIImage {

    let randomImage = arc4random_uniform(MAX_RANDOM_NUMBER);

    return getImage(id: Int(randomImage));
}

func getImage(#id:Int)->UIImage {

    return images[id];
}

}


class InterfaceController: WKInterfaceController {



@IBOutlet var blueColour: WKInterfaceButtonComparable!
@IBOutlet var pinkColour: WKInterfaceButtonComparable!
@IBOutlet var greenColour: WKInterfaceButtonComparable!
@IBOutlet var yellowColour: WKInterfaceButtonComparable!

@IBOutlet var mainBackgroundColour: WKInterfaceGroupComparable!

@IBOutlet var scoreLabel: WKInterfaceLabel!


var randomImage = UInt32()
var randomMainBackground = UInt32()

var score:Int = 1


@IBAction func onePressedTest() {

    if blueColour.equalsImage(mainBackgroundColour) || pinkColour.equalsImage(mainBackgroundColour) || greenColour.equalsImage(mainBackgroundColour) || yellowColour.equalsImage(mainBackgroundColour) {

        println("they match")
        scoreLabel.setText("\(score)")
        score++

    } else {

        println("they dont match")

    }

    blueColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
    pinkColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
    greenColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
    yellowColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
    mainBackgroundColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
Nick89
  • 2,948
  • 10
  • 31
  • 50
  • Hi Andrew, just tried it and no luck. I get the same error, this time quoting '===' – Nick89 Apr 16 '15 at 23:19
  • Hi Leonardo, sorry I don't quite get what you mean? How would that look in the code please. Could you provide the example? Thanks in advance – Nick89 Apr 16 '15 at 23:31
  • you put the classes and protocols inner `InterfaceController` class, to work you need put out of `InterfaceController`, and update with new code I edited below. – ViTUu Apr 19 '15 at 22:31

1 Answers1

0

I made an example what I think can help you find the way to resolve your issues, I created class custom with comparable power to help your work. I do it very fast, and I don't have way to test for you, but you can see the concept to help you create a solution. I hope so helped you with my example.

ComparableImage is the key to compare, this protocol can do your mission.

WKInterfaceButtonComparable extends WKInterfaceButton and with this you can save the current image to compare after, I did same with WKInterfaceGroupComparable.

ImageProvide is important because this will manage your images, there you need code your logic about images and with this Singleton you can preload your images and send to your WK components. I hope it help too.

protocol WKInterfaceComparableImage {

    func getImage()->UIImage;
    func equalsImage(comparableWKObject:WKInterfaceComparableImage)->Bool;
}

class WKInterfaceButtonComparable : WKInterfaceButton, WKInterfaceComparableImage {

    private var image:UIImage?;

    override func setBackgroundImage(image: UIImage?) {
        self.image = image;
        super.setBackgroundImage(image);
    }

    func equalsImage(comparableWKObject: WKInterfaceComparableImage)->Bool {
        return self.image === comparableWKObject.getImage();
    }

    func getImage() -> UIImage {
        return image!;
    }

}

class WKInterfaceGroupComparable : WKInterfaceButton, WKInterfaceComparableImage {

    private var image:UIImage?;

    override func setBackgroundImage(image: UIImage?) {
        self.image = image;
        super.setBackgroundImage(image);
    }

    func equalsImage(comparableWKObject: WKInterfaceComparableImage)->Bool {
        return self.image === comparableWKObject.getImage();
    }

    func getImage() -> UIImage {
        return image!;
    }

}

class ImageProvide {


    private let MAX_RANDOM_NUMBER:UInt32 = 4

    static let shared:ImageProvide = ImageProvide();

    var images:[UIImage];

    private init() {

        images = [];

        for i in 1...MAX_RANDOM_NUMBER {

            //get image with the best way to you
            images.append(UIImage(named: "colour\(i).jpg")!);
        }
    }

    func getRandomImage()->UIImage {

        let randomImage = arc4random_uniform(MAX_RANDOM_NUMBER);

        return getImage(id: Int(randomImage));
    }

    func getImage(#id:Int)->UIImage {

        return images[id];
    }

}


class InterfaceController: WKInterfaceController {


    @IBOutlet var blueColour: WKInterfaceButtonComparable!
    @IBOutlet var pinkColour: WKInterfaceButtonComparable!
    @IBOutlet var greenColour: WKInterfaceButtonComparable!
    @IBOutlet var yellowColour: WKInterfaceButtonComparable!

    @IBOutlet var mainBackgroundColour: WKInterfaceGroupComparable!

    @IBOutlet var scoreLabel: WKInterfaceLabel!


    var randomImage = UInt32()
    var randomMainBackground = UInt32()

    var score:Int = 1


    @IBAction func onePressedTest() {

        if blueColour.equalsImage(mainBackgroundColour) {

            println("they match")

        } else {

            println("they dont match")

        }

        blueColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
        pinkColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
        greenColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
        yellowColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
        mainBackgroundColour.setBackgroundImage(ImageProvide.shared.getRandomImage())
    }
}
ViTUu
  • 1,204
  • 11
  • 21
  • Thanks for your help. I have tried your code but get a couple of errors. On the line that reads: protocol ComparableImage { - i get the error: declaration is only valid at file scope. On the line that reads, class WKInterfaceButtonComparable : WKInterfaceButton, ComparableImage { - i get the error: Class 'InterfaceController.WKInterfaceButtonComparable' has no initializers. On the line that reads: let randomImage = arc4random_uniform(MAX_RANDOM_NUMBER); - i get the error: Cannot invoke 'arc4random_uniform' with an argument list type '(Int)'. I have added my updated code above. Could you help? – Nick89 Apr 19 '15 at 22:13
  • Sorry, i edited the code with your notice. **WKInterfaceButtonComparable : WKInterfaceButton, ComparableImage { - i get the error: Class 'InterfaceController.WKInterfaceButtonComparable' has no initializers** fix with `private var image:UIImage?;` *let randomImage = arc4random_uniform(MAX_RANDOM_NUMBER); - i get the error: Cannot invoke 'arc4random_uniform' with an argument list type '(Int)'* fix with adding Uint32 type in constant `MAX_RANDOM_NUMER` *protocol ComparableImage { declaration is only valid at file scope.* I believe you forgot swift file with protocol ComparableImage!! – ViTUu Apr 19 '15 at 22:23
  • hi @ViTUu - i have just tried your new code and updated it above. I still get the errors. And i also am getting an error on this line: if blueColour.equalsImage(mainBackgroundColour) { - error it get is: Cannot invoke 'equalsImage' with an argument list type ('Interface.WKInterfaceGroupComparable!)' – Nick89 Apr 19 '15 at 23:00
  • @Nick89 Can you update in your Issue this code.. I need see to understand how you use, because this error is occurred when you try use method not exist in Class. Can you update? – ViTUu Apr 19 '15 at 23:04
  • thanks for all your help with this! the code above is my latest code, which had the amends you told me to apply. – Nick89 Apr 19 '15 at 23:06
  • I still have errors but different ones now. for the line: class WKInterfaceButtonComparable : WKInterfaceButton, WKInterfaceComparableImage { - i get error: Type 'WKInterfaceButtonComparable' does not conform to protocol type 'WKInterfaceComparableImage'. And for: class WKInterfaceGroupComparable : WKInterfaceButton, WKInterfaceComparableImage { i get error: Type 'WKInterfaceGroupComparable' does not conform to protocol type 'WKInterfaceComparableImage'. – Nick89 Apr 20 '15 at 07:14
  • And for both lines: return self.image === comparableWKObject.getImage(); i get error: 'WKInterfaceButtonComparable' does not have a member named 'getImage'- and: 'WKInterfaceGroupComparable' does not have a member named 'getImage'. And the line if blueColour.equalsImage(mainBackgroundColour) {, gives an error - Cannot invoke 'equalsImage' with an argument list type ('Interface.WKInterfaceGroupComparable!)' – Nick89 Apr 20 '15 at 07:16
  • Look my new code, you need update the functions of tour class, I believe you just update protocol class.. Look other class – ViTUu Apr 20 '15 at 11:18
  • Oh yes, thanks i missed a bit of your code. I have updated it now above. The code now produces no errors, but i get an error when i run the app and press the button. When I press the button, the app crashes and gives the error on the line: if blueColour.equalsImage(mainBackgroundColour) { - the error is: Thread 1: EXC_BAD_ACCESS (code=1, address=0x60000000f) – Nick89 Apr 20 '15 at 19:53
  • I think is better you check if your images is nil. Use breakpoint for it.. It smell in ImageProvide on init method on for when creat the images no found images.. Look there – ViTUu Apr 20 '15 at 20:04
  • in this part of code: for i in 1...MAX_RANDOM_NUMBER { //get image with the best way to you images.append(UIImage(named: "colour\(i).jpg")!); } - Do i need to get the Images in here? How can i get the images in? – Nick89 Apr 20 '15 at 21:25
  • and thanks for all your help with this once again. I just need to understand this last error as above. – Nick89 Apr 20 '15 at 22:57