1

I have this simple class:

import Foundation

class Utility {
    func somma(int1:String, int2:String) -> String {
        let totale = (Int(int1) ?? 0) + (Int(int2) ?? 0)
        let totale_Str = String(totale)
        return totale_Str
    }
}

and this simple view:

import SwiftUI

struct ContentView: View {
    @State private var int1:String = "0"
    @State private var int2:String = "0"

    @State private var somma:String = ""

    var body: some View {
        VStack {
            TextField("Intero 1", text: $int1)
            TextField("Intero 2", text: $int2)
            Text("\(somma)")
            Button(action: {
                self.somma = Utility.somma(self.int1, self.int2)
            }) {
                Text("Somma")
            }
        }
    }
}

but in the line self.somma = Utility.somma(self.int1, self.int2) I have this error Instance member 'somma' cannot be used on type 'Utility'; did you mean to use a value of this type instead? I have tried in different ways to eliminate the error without finding the solution. How can I eliminate this error?

2
  • Either make the somma function static or create an instance of Utility. This is not SwiftUI related but a generic Swift design principle as in all other OO languages I know. Did you miss this or is there something SwiftUI related I overlooked? Commented Oct 30, 2019 at 22:14
  • Can you change my example to make it work? Commented Oct 30, 2019 at 22:26

3 Answers 3

2

I found the solution. Simply change:

self.somma = Utility.somma(self.int1, self.int2)

with:

self.somma = Utility().somma(int1: self.int1, int2: self.int2)
Sign up to request clarification or add additional context in comments.

Comments

1

Class function in Swift are preceded with static keyword, so if you want to use somma as class (i.e static) function you have to declare it like this:

static func somma(int1:String, int2:String) -> String { //etc.

1 Comment

I tried to do as you suggested, but it doesn't work.
0

Class:

import Foundation

class Utility:ObservableObject {
    var totale_Str : String = ""
    func somma(int1:String, int2:String) -> String {
        let totale = (Int(int1) ?? 0) + (Int(int2) ?? 0)
        let totale_Str = String(totale)
        return totale_Str
    }
}

View:

import SwiftUI

struct ContentView: View {
    @State private var int1:String = "0"
    @State private var int2:String = "0"

    @State private var somma : String = ""
    @ObservedObject var utility = Utility()
    var body: some View {
        VStack {
            TextField("Intero 1", text: $int1)
            TextField("Intero 2", text: $int2)
            Text("\(somma)")
            Button(action: {
                self.somma = utility.somma(int1: self.int1, int2: self.int2)
            }) {
                Text("Somma")
            }
        }
    }
}

2 Comments

Could you explain your answer? e.g. why is this better (or not) than Cesare Piersigilli's own answer.
I just wanted to solve the problem, and I didn't notice that he had solved it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.