SharadCodes SharadCodes - 3 months ago 11
Swift Question

Swift: inherit from Class within the project

How to inherit from any other class within the project? I tried to use import. But as I have read, it is used to import other packages.
The sample code is below.

[MyClass.swift]
class MyClass: AnotherClass { //error: use of undeclared type 'AnotherClass'

}

[AnotherClass.swift]
class AnotherClass {
var name: String
}

Answer

In order to import a class from a different file you will need to mark your class as public in its declaration.

public class AnotherClass {
    var name: String
}