I want to do the following:
MyClass.swift:
public class MyClass {
...
}
MyClass+Extension.swift:
extension MyClass {
...
}
If I have both in the same file it works fine, if they are in separate files I get the following error:
"Use of undeclared type 'MyClass'"
I already checked the target membership of both files. They are part of the same target.
In Swift 4, an extension can reach across to a private declaration. This only applies to extensions that live in the same source file as the private declaration. In other words, an entity that is declared private is accessible from within any extensions for that type within the same source file.
In Swift, you can even extend a protocol to provide implementations of its requirements or add additional functionality that conforming types can take advantage of. For more details, see Protocol Extensions. Extensions can add new functionality to a type, but they can't override existing functionality.
We can't add the stored properties to extensions directly but we can have the computed variables . Extensions in Swift can: Add computed instance properties and computed type properties.
The following will enable you to access your additional functions in MyClass+Extension.swift from anywhere that your original MyClass is accessible, which included outside its module since it had a public
access level:
public extension MyClass {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With