Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static imports in swift

Tags:

static

swift

In java you can do import static MyClass and you'll be able to access the static methods of MyClass without having to prepend them with the class name:

myMethod() instead of MyClass.myMethod()

Is there a way to do this in swift?

like image 480
shoe Avatar asked Sep 06 '25 01:09

shoe


1 Answers

I don´t think you can import a static class like Java, it´s a traditional static in Swift where you call it by class name + variable/function.

class  MyClass {
    static let baseURL = "someURl"

    static func myMethod() {

    }
}

MyClass.baseURL or MyClass. myMethod.

What you can do is to add a typealias to make an alias for your Static class.

private typealias M = MyClass

And then use the following: M.baseURL or M.myMethod.

like image 95
Rashwan L Avatar answered Sep 09 '25 04:09

Rashwan L