I have a View Controller and two classes. Both ClassOne and ClassTwo have a reference to the view controller like so:
var viewController: ViewController?
Both the view controller and ClassTwo have a ClassOne
variable that I've assigned like so, in order to retrieve other variables and call functions.
let class_one = ClassOne()
In ClassOne
, I am trying to call a function in ClassTwo that uses class_one
to call functions in ClassOne. For example, in ClassTwo:
func changeString() {
self.class_one.string = "yada yada"
}
Here are different ways I have tried to call changeString()
in ClassOne
:
// Method one:
ClassTwo().changeString()
// Method two:
let class_two = ClassTwo()
self.class_two.changeString()
// Method three:
var class_two: ClassTwo?
self.class_two!.changeString()
When I run the app using method one, the app crashes and says the viewController and classOne references in ClassTwo
are nil.
When I run the app using method two, it crashes and Xcode gives me the following memory warning, citing back and forth errors between the changeString()
function call in ClassOne
and my self.class_one.string
variable call in ClassTwo
.
Xcode memory warning - could not load any Objective-C class information
This isn't my actual code, but this is the problem I am facing. I'm a beginner with Swift, and I don't really understand what the difference is in all three of those ClassTwo
function calls from ClassOne
.
Can someone please explain what I'm doing wrong?