I am building a large class in Swift, which I want to spread over multiple files, since it is getting too big to effectively work in. It cannot be subclassed, since it is just a class with many functions. (I might be able to optimise it later on and remove double code, but I still want to achieve this.)
To make it easier to work in I want to spread the class into multiple Swift-files, using Extensions. I already achieved it partly; extending the base-class by creating the extensions in the same file. This works without any problems.
Lets say the base-file is BaseClass.Swift (and the class BaseClass) and I want to extend it to BaseClassExtension.swift, I put the following in BaseClassExtension.swift:
extension BaseClass {
func doStuff() -> String {
return myVariable
}
}
In BaseClass.swift I have private var myVariable = "I am a string"
, but in BaseClassExtension.swift I get the following error: Use of unresolved identifier 'myVariable'
I have tried the following things:
- Make the Extension public
- Make the BaseClass public
- Make both public
- (And by default they were both default (internal I guess?))
When I remove private
from myVariable
, the error is solved, but I really want it to be private, since it may never be accessed from outside the class directly.