Nelson Matias
Nelson Matias

Reputation: 473

Call Function of custom Swift Framework

I'm trying to create a simple framework that has a function that returns "hello name" name being a passed argument. Below is the framework and code trying to call it.

Framework:

public class Greeter {
    public init () {}
    public static func greet(name: String) -> String {
        return "Hello /(name)."
    }
}

Code:

import Greeter

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let greetingString: String = Greeter.greet("Bob")
        print(greetingString)
    }
}

When I try typing out "greet("Bob")", what autocompletes is "(name: String) -> String greet(self: Greeter)". And when I manually type "greet("Bob")", I get the error: Instance member 'greet' cannot be used on type 'Greeter'; did you mean to use a value of this type instead?

Upvotes: 0

Views: 955

Answers (1)

Frankenstein
Frankenstein

Reputation: 16341

You need to create an instance of Greeter class first and then call it's method.

let greeter = Greeter()
let greetingString: String = greeter.greet(name: "Bob")
print(greetingString)

Update: You don't need : String it's redundant here. So you can modify that line to:

let greetingString = greeter.greet(name: "Bob")

Upvotes: 3

Related Questions