2

EDIT: Ok. I just changed my code as: var randomX = Int(arc4random()%6) Wish i could think of it before posting here :|

I took accepted answer of this topic as reference: Swift convert UInt to Int


I've been trying to make a simple ios guessing app with swift. I'm generating a random number and getting another number from user and comparing both. But i'm stuck with this error: 'UInt32' is not convertible to 'MirrorDisposition' while comparing two integers (one of them converted from string to integer by toInt() method)

Below you can see my ui, my code, two stackoverflow topics i read and how i changed my code after reading those topics.

UI: (i couldn't resize the image) enter image description here

my code:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var myImageView: UIImageView!
    @IBOutlet var inputField: UITextField!
    @IBAction func clickedGuessButtonAction(sender: AnyObject) {
        println("Guess button clicked")
        var randomX = arc4random()%6
        println("randomX = \(randomX)")
        var guess = inputField.text.toInt()

        if((inputField.text) != nil){
            if(guess == randomX){
                println("correct")
                var image = UIImage(named: "images/tick.png");
                myImageView.image=image;
                self.view.addSubview(myImageView); // what is this?
                inputField.resignFirstResponder();// hides keyboard

            }
            else
            {
                println("wrong")
                var image = UIImage(named: "images/cross.png")
                myImageView.image=image;
                self.view.addSubview(myImageView);
                inputField.resignFirstResponder();//hides keyboard
            }
        }
        else{
            println("invalid input. requires integer only")
            inputField.resignFirstResponder();// hides keyboard
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

I found these topics on stackoverflow:

float is not convertible to 'MirrorDisposition' Swift What is mirrordisposition?

iOS Swift Error: 'T' is not convertible to 'MirrorDisposition'

First one especially has an extended answer finally suggesting if intValue == Int(floatValue)

Than i changed var guess = inputField.text.toInt()

to var guess = Int(inputField.text);

But this time i'm getting an error message like this: Cannot invoke 'init' with an argument of type '@lvalue String!'

This time, i searched this error message but couldn't find anything helpful. It shouldn't be this difficult to compare 2 integers. I'm definitely missing something easy. Any ideas?

Community
  • 1
  • 1
t1w
  • 1,408
  • 5
  • 25
  • 55
  • Ok. I just changed my code as: **var randomX = Int(arc4random()%6)** Wish i could think of it before posting here :| – t1w Sep 19 '14 at 12:53
  • possible duplicate of [Swift convert UInt to Int](http://stackoverflow.com/questions/24144557/swift-convert-uint-to-int) – mattt Nov 20 '14 at 06:42

2 Answers2

3

Try changing:

  var randomX = arc4random()%6

to

  var randomX = Int(arc4random()%6)
0

this should do it

let sizeX = UInt32(6)
let randomX = CGFloat(arc4random_uniform(sizeX))
Michael N
  • 436
  • 5
  • 6