Answer the question
In order to leave comments, you need to log in
Swift: how to store a reference to another object in class parameters?
I just started learning Apple Swift and can't figure out how to store a reference to another object in the class parameters?
For example:
// List - Мой кастовый класс
class RecordsList: NSObject {
var listObj: List!
init (inout list: List!)
{
self.listObj = list
}
func printData()
{
println(self.listObj.name)
}
}
var listObject = List()
listObject.name = "FirstValue"
var RL = RecordsList(&listObject)
listObject.name = "SecondValue"
RL.printData()
// Я ожидаю: "SecondValue"
// Но получаю FirstValue =(
Answer the question
In order to leave comments, you need to log in
Is this the correct listing? Did you somehow famously create RL, neither var nor let, or is it possible now?
I didn't get your version at all. That's so normal. I will assume that the problem is in inout and that the passed parameter is explicitly declared with the wrong type (optional or!). From! It is generally better to refuse types and use them less, it is rather a crutch.
class List:NSObject{
internal var name:String?
}
class RecordsList: NSObject {
var listObj: List?
init (inout list: List?)
{
self.listObj = list
}
func printData()
{
println(self.listObj?.name)
}
}
var listObject:List? = List()
listObject?.name = "FirstValue"
var RL = RecordsList(list: &listObject)
listObject?.name = "Second Value"
RL.printData()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question