How to save return value from function in swift ui and use it

My app has two buttons, the first is calling a function that return a string and does some other stuff. The second button has a function that takes the return String from the function one. Because the first function does other stuff as well, how can I store the return String in a variable for button two, so I don't have to call the function? Example below, I want to avoid usingDirString (strUpper: returnDirString())

func returnDirString () -> String {

    let dir = "/some/return/string"

    return dir

}

func usingDirString (strUpper: String) {

    print(strUpper)

}

struct ContentView: View {

    var body: some View {
        Button(action: {
            returnDirString()
        }) {
            Text("First")
        }
        .padding()
        Button(action: {
            usingDirString (strUpper: returnDirString())

        }) {

            Text("second")
        }
        .padding()
    }
}

Okay typical me hours for hours no answer to the problem and when I am asking, I find it right away... My solution:

func returnDirString () -> String {

    let dir = "/some/return/string"

    return dir

}

func usingDirString (strUpper: String) {

    print(strUpper)

}

struct ContentView: View {

    @State var processString = ""

    var body: some View {
        Button(action: {
            let someString = returnDirString()
            $processString.wrappedValue = someString
        }) {
            Text("Read Apk")
        }
        .padding()
        Button(action: {
            usingDirString (strUpper: processString)
        }) {
            Text("Sign Apk")
        }
        .padding()
    }
}

But is there a better one please discuss, I am curious!

Well from a design perspective I like to use @State variables in views just for dealing with state in the view. What you're doing would seem to classify as part of the "business logic" of your app so I would offload that logic away from your view to your "model" or "view model." Look up MVVM if you don't know what these are.

Anyway if you were to take an MVVM approach your code might look like:

struct ContentView: View {

    @ObservedObject var viewModel: MyViewModel

    var body: some View {
        Button(action: {
            viewModel.readApk()
        }) {
            Text("Read Apk")
        }
        .padding()
        Button(action: {
            viewModel.signApk()
        }) {
            Text("Sign Apk")
        }
        .padding()
    }
}

With this approach processString, usingDirString, and returnDirString are all abstracted away from your view. By keeping your view code and your business logic separate it makes it easier to deal with each of them in a more isolated and clear manner.

How to save return value from function in swift ui and use it
 
 
Q