M
M
MaxR2015-08-11 19:28:56
iOS
MaxR, 2015-08-11 19:28:56

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 =(

Please help me get "SecondValue")

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Elchev, 2015-08-12
@MaxR

Is this the correct listing? Did you somehow famously create RL, neither var nor let, or is it possible now?

A
An, 2015-08-12
@Flanker_4

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()

That's the norm

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question