0

I have a problem with getting strings, arrays and functions from other class in swift and I couldn't find a clear answer for that.

On example I have main storyboard where I want to get value from function which is in another file

ViewController.swift

print(Us.username())

Users.swift

import Foundation


public class Us {

func username(){
     return "Bob" // on example
}

}
1
  • Side note, but the standard practice is for function names to start with a lowercase letter. Commented Jul 26, 2019 at 23:11

2 Answers 2

2

Well, unless your method Username() is static, you should call it by first creating an instance of class and then through it:

var instanceOfUs = Us() // create an instance here
instanceOfUs.Username() // and then call your method

or

Us().Username()

This should work.

Sign up to request clarification or add additional context in comments.

Comments

0
class Us: UIViewController {

 Struct Users { 

    static func username() { }
    static var array = [Any]()
    static var string: String = ""
 } 
}



 Class ViewController: UIViewController { 

   print(Us.Users.array)
   print(Us.Users.string)
   Us.Users.username()

  }

Comments

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.