-1

I'm trying to program a formal system that has these rules:

  • You start with the string "MI" (which is the text of the label at the beginning)

There are four rules of inference : 1) If the last character of the string is "I", you can add a "U" to the string (ex: MI --> MIU)

2) Being x a sequence of letters, Mx can be replaced with Mxx (ex: MIU --> MIUIU)

3) If the string contains "III", you can replace "III" with "U" (ex: MUIII --> MUU)

4) If the string contains "UU", you can delete the "UU" part (ex: MIIUU --> MII)

In my project, rule 1 corresponds to button1, rule 2 to button2, rule 3 to button3, rule 4 to button 4

Also, since a user may end up in a infinite loop sometimes (ex: if you get to "MIU", from then on you can only use rule 2 and get "MIUIU" and so on) I added a "clear" button. I want the clear button to just turn the Label.text into "MI" so the user can just go back to the starting point and retry

The point is, for some reason when I press the "clear" button the Label.text does not turn into "MI" as I would want it to, but it turns into "MIU" instead!

What am I missing?

PS: I know this might is probably something extremely easy that I'm stuck on, but I don't have much programming experience

This is my code: import UIKit

class ViewController: UIViewController {

@IBOutlet weak var Label: UILabel!
@IBOutlet weak var rule1Button: UIButton!
@IBOutlet weak var rule2Button: UIButton!
@IBOutlet weak var rule3Button: UIButton!
@IBOutlet weak var rule4Button: UIButton!
@IBOutlet weak var clearButton: UIButton!



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

    clear()

}

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






var string : String = "MI"
var string1 : String = ""


func rule1() {

    if string.characters.last == "I" {
    string.append("U")
    Label.text = string
    }

}


func rule2() {

    string1 = string.replacingOccurrences(of: "M", with: "")
    string = string + string1
    Label.text = string

}


func rule3() {

    string = self.string.replacingOccurrences(of: "III", with: "U")
    Label.text = string

}


func rule4() {

    string = self.string.replacingOccurrences(of: "UU", with: "")
    Label.text = string

}


func clear() {
    string = "MI"
    Label.text = string
}









@IBAction func rule1Button(_ sender: Any) {
    rule1()
}


@IBAction func rul2Button(_ sender: Any) {
    rule2()
}


@IBAction func rule3Button(_ sender: Any) {
    rule3()
}


@IBAction func rule4Button(_ sender: Any) {
    rule4()
}


@IBAction func clearButton(_ sender: Any) {
    clear()

}

}
braX
  • 11,506
  • 5
  • 20
  • 33
tommsyeah
  • 17
  • 2
  • 9
  • Label.text = "MI". A simple as that – Mannopson May 11 '17 at 22:10
  • What did I do wrong though? Anyway, if I do as you say: the Label.text turns into "MI" when I press the clear button (which is good news) but what happens is the string remains what it was before. If for example the string was "MIUIU", and the user presses the clear button (so label.text becomes "MI") and then presses the button2, Label.text turns into "MIUIUIUIU" and not into "MII" (as it should do), because the functions operate on the string, not on the label.text and even though the Label.text is "MI", the string is "MIUIU". This was the reason I wrote string = "MI" Label.text = string – tommsyeah May 11 '17 at 22:41
  • Use the print inside of your function and you can see what happens. For example: print(string). – Mannopson May 11 '17 at 22:50

1 Answers1

0

Code is just fine - you surely have wrong connection in your storyboard between @IBActions and Buttons.

Reconnect all of them and you should be fine.

If you right click on your UIButton in Storyboard, there is information to which IBAction it connects.

enter image description here

Check all buttons and fix broken ones.

Aside from that your code can be simplified a lot, and I encourage you to use didSet to keep your variable value with UILabel text.

var string : String = "" { //no need for default value here, as you are calling clear() on viewDidLoad(), better keep constants in one place
    didSet {
        Label.text = string
    }
}

func rule1() {
    if string.characters.last == "I" {
        string = string + "U"
    }
}


func rule2() {
    string = string + string.replacingOccurrences(of: "M", with: "")
}

func rule3() {
    string = self.string.replacingOccurrences(of: "III", with: "U")
}

func rule4() {
    string = self.string.replacingOccurrences(of: "UU", with: "")
}

func clear() {
    string = "MI"
}
Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71