Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI - How can I call a Function in my View?

Tags:

ios

swift

swiftui

I am using SwiftUI/Swift for a week now, and I love it. Now I have a problem. I want to call a Function from my View, but I get this Error

Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols

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.")
    }
    
}

I don't get it. In other languages its much simpler and could work that way. Can anybody help me please? Swift is pretty new to me :D

like image 664
yama_HD Avatar asked Aug 03 '20 15:08

yama_HD


People also ask

How do you pass a function as a parameter in SwiftUI?

To pass a function as a parameter into another function in Swift, the accepted parameter type needs to describe a function. Now you can call this function by passing it an argument that is any function that takes two doubles and returns a double.

How do I make a call with SwiftUI?

Making a phone call from a SwiftUI app is easy and only takes a few lines of code to implement. All we need to do is setup a button that will open the phone app and make a phone call with a phone number we pass in.

Why does SwiftUI use some view?

First, using some View is important for performance: SwiftUI needs to be able to look at the views we are showing and understand how they change, so it can correctly update the user interface.

How do you call a function in Swift?

You give a function a name that identifies what it does, and this name is used to “call” the function to perform its task when needed. Swift’s unified function syntax is flexible enough to express anything from a simple C-style function with no parameter names to a complex Objective-C-style method with names and argument labels for each parameter.

What can SwiftUI do for You?

It has a way to declare all principles we learn in this declarative world. We can declare data as a source of truth or dependency, and view as a function of those data. SwiftUI will do take care of the rest for us.

What is the use of view controller in SwiftUI?

It makes the concept of view controller not relevant anymore in SwiftUI. The purpose of the view controller is to manage views and their interaction. Most of the codes in view controller are for responding to user interaction from its views. Setting up target-action to a button, listen for a delegate.

What is the difference between UIView and SwiftUI?

View in UIKIt usually a result of sequences of events, whether target-action, delegate, or notification. View in SwiftUI, on the other hand, designed to depend solely on data.


2 Answers

Meanwhile here are the places (not all) where/how you can call a function

    init() {
        self.testFunction()     // non always good, but can
    }

    var body: some View {
        self.testFunction()            // 1)
        return VStack {
            Text("TextBox")
               .onTapGesture {
                  self.testFunction()    // 2)
               }
            Text("SecondTextBox")
        }
        .onAppear {
            self.testFunction()     // 3)
        }
        .onDisappear {
            self.testFunction()     // 4)
        }
    }

... and so on

like image 187
Asperi Avatar answered Nov 11 '22 20:11

Asperi


Using Swift 5.3 and Xcode 12.4

I use a little extension to debug inside the Views (VStack in the example), e.g. to inspect a geometryReader.size. It could be used to call any function in the View as follows:

NOTE: For debugging purposes only. I don't recommend including code like this in any production code

VStack {
    Text.console("Hello, World")
    Text("TEXT"
}

extension Text {
    static func console<T>(_ value: T) -> EmptyView {
        print("\(value)")
        return EmptyView()
    }
}

This will print "Hello, World" to the console.....

like image 29
eharo2 Avatar answered Nov 11 '22 20:11

eharo2