0

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?

1 Answer 1

3

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") 
Sign up to request clarification or add additional context in comments.

3 Comments

This works, but is there a way that I can do the same without creating an instance of the class (I've changed the function to be static)? I'm trying to follow Kilo Loco's video below youtube.com/watch?v=GMYxlkOE35k
Both are correct. If you make the method static then you could just call Greeter.greet()
Perfect, I just tried that and they both work! thank you so much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.