Advertisements
I have a problem while coding in content view. I don’t understand how can I use struct’s func in content view. I would like to get helps from you guys. I get an error like this "Result of call to ‘changeText(text:)’ is unused".
Here’s simple example of what I’m trying to achieve,
import Foundation
public struct Brain {
public func changeText(text: String) -> String{
var text = text
text = "Changed text!"
return text
}
}
import SwiftUI
struct ContentView: View {
@State var tmp: String = "hello"
var tmpTwo: Brain = Brain()
var body: some View {
VStack {
Button(action: {
tmpTwo.changeText(text: tmp )
}, label: {
Text(tmp)
})
}
.padding()
}
}
>Solution :
You need to use the return value of the function like so:
import SwiftUI
struct ContentView: View {
@State var tmp: String = "hello"
var tmpTwo: Brain = Brain()
var body: some View {
VStack {
Button(action: {
tmp = tmpTwo.changeText(text: tmp )
}, label: {
Text(tmp)
})
}
.padding()
}
}