I am having a little trouble here: I have a class
class TempC {
func GetData(){
//do stuff
}
}
And in ContentView I want to call the function, but I can't manage to do it, I am getting errors...
struct ContentView: View {
var variable : TempC
variable.GetData()
var body: some View {
Text("Hello World")
}
}
Or in any other method. How can I call an external function now?
PS: The error that I get are on the line with variable.GetData()
which are:
- Consecutive declarations on a line must be separated by ";"
- Expected "("in argument list of cantons declaration
- Expected "{"in body of function declaration
- Expected 'func' keyword in instance method declaration
- Invalid redeclaration of 'variable()'
It's like it is expecting to create a new function not to get the one that is already existing.
You can go ahead and implement View as a class. If possible implement this in a fresh new SwiftUI project.
This is the Code: struct testView: View { var body: some View { VStack { Text("TextBox") Text("SecondTextBox") self. testFunction() } } func testFunction() { print("This is a Text.") } }
Depending on what your going to do in that call there are options, ex:
Option 1
struct ContentView: View {
let variable = TempC()
init() {
variable.GetData()
}
var body: some View {
Text("Hello World")
}
}
Option 2
struct ContentView: View {
let variable = TempC()
var body: some View {
Text("Hello World")
.onAppear {
self.variable.GetData()
}
}
}
Similarly you can call it in .onTapGesture or any other, pass reference to your class instance during initialising, etc.
backup
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With