Search code examples
swiftmu

I don't know what I'm doing wrong in this Swift project?


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()

}

}

Solution

  • 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"
    }