Swift: struct with mutation and update the enum values through a button click

Viewed 282

I need to add a proper text to the end of the data after the change button is clicked.

I have a struct that defines the data format:

struct CaptureData {
    var mode: Mode = .one
        mutating func nextCase(){
        mode = mode.next()
        }
    var method : String {
        return "<~method:\(mode.next())>"
    }
}

Let's say at some point I want to fill an array with one value, and update the mode from one to .two, right after, add another value to your array and update the mode from .two to .three.


Blow are the cases that I want to change:

enum Mode: String, CaseIterable {
    case one, two, three
}

extension CaseIterable where Self: Equatable {
    var allCases: AllCases { Self.allCases }
    var nextCase: Self {
        let index = allCases.index(after: allCases.firstIndex(of: self)!)
        guard index != allCases.endIndex
        else { return allCases.first! }
        return allCases[index]
    }
    @discardableResult
    func next() -> Self {
        return self.nextCase
    }
}

The change button to change text and update it:

var x = 0
var instance = CaptureData()

@IBAction func ChangingTapped(_ btn: UIButton) {
    if x == 0 {
        Textfield.text = "Method 1"
        print(instance.mode) //successfully to print case 1
    } else if x == 1 {
        Textfield.text = "Method 2"
        instance.next()
        print(instance.mode) //successfully to print case 2
    } else {
        Textfield.text = "Method 3"
        instance.next()
        print(instance.mode) //successfully to print case 3
    }
  x += 1
}

The stop button to stop capturing:

var captureData: [CaptureData] = []

@IBAction func stopbuttonTapped(_ btn: UIButton) {
    guard let data = getFrameData() else {return}
    var capdata = captureData.map {$0.method}.joined()
    let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let url = dir.appendingPathComponent("captured.txt")
    try? capdata.write(to: url, atomically: false, encoding: .utf8)

func getFrameData() -> CaptureData? {
    let data = CaptureData()
       return data
   }

My problem is that the getFrameData will return a CaptureData with only the default mode set on, and adds it to the capture data array. The place I call the nextCase() method is the ChangeTapped func and which do not touch the capture data array. I am wondering how one can fix this so that I can print the mode from one to .two, right after, add another value to the array and update the mode from .two to .three.

Currently, when I click the change button I can successfully print the cases from one to two, two to three:

enter image description here

The saved result after clicking the stop button only showing method two:

enter image description here

Expected output: <~method:one>(this is the default case)<~method:two>(when the change button is pressed)<~method:three>(this is the change button is pressed again)

Anyone would have suggestions on this? Any suggestion is appreciated! Thanks in advance!

0 Answers
Related