Swift nested class properties

IosSwift

Ios Problem Overview


Does swift not have nested classes??

For example, I can't seem to access the property test of the master class from the nested class.

class master{
  var test = 2;

  class nested{
     init(){
       let example = test; //this doesn't work
     }
   }
 }

Ios Solutions


Solution 1 - Ios

Swift's nested classes are not like Java's nested classes. Well, they're like one kind of Java's nested classes, but not the kind you're thinking of.

In Java, an instance of an inner class automatically has a reference to an instance of the outer class (unless the inner class is declared static). You can only create an instance of the inner class if you have an instance of the outer class. That's why in Java you say something like this.new nested().

In Swift, an instance of an inner class is independent of any instance of the outer class. It is as if all inner classes in Swift are declared using Java's static. If you want the instance of the inner class to have a reference to an instance of the outer class, you must make it explicit:

class Master {
    var test = 2;

    class Nested{
        init(master: Master) {
            let example = master.test;
        }
    }

    func f() {
        // Nested is in scope in the body of Master, so this works:
        let n = Nested(master: self)
    }
}

var m = Master()
// Outside Master, you must qualify the name of the inner class:
var n = Master.Nested(master:m)

// This doesn't work because Nested isn't an instance property or function:
var n2 = m.Nested()

// This doesn't work because Nested isn't in scope here:
var n3 = Nested(master: m)

Solution 2 - Ios

This solution is sort of similar to how I use it in C#, and I have successfully tested it in Xcode.

Here's a breakdown of the process:

  1. Your nested class needs to be made optional so you don't have to initialize it, so theres a '?' in the declaration; if you initialize both your parent class and your nested class, you end up with a 'recursion' effect and an error is generated
  2. Create a regular function that receives an argument of the same type as your main class
  3. Pass that argument to your nested class (this can go into the nested object's normal initializer). - Since objects are passed by reference by default, there's nothing special to get your nested class to link to the parent class
  4. Inside your nested class, you need a variable of the same type as your parent class

From here on out set up everything as you normally would.

In the code execution area, your nested class object also needs to be regarded as optional (hence the '?'). If you forget about it, Xcode will add it anyways.

In this example, I wanted to design a keyword "set," so when I set variables, I can type:

testClass.set.(and then a descriptive method name)

Here is the code, and its goal is to output "test" in the console, after the value is set via the nested object:

class testClass
{
    var test_string:String = ""
    var set: class_set?

    func construct_objects(argument: testClass)
    {
        self.set = class_set(argument: argument)
    }
    class class_set
    {
        var parent:testClass
        init(argument: testClass)
        {
            parent = argument
        }
        func test_string_to_argument(argument: String)
        {
            parent.test_string = argument
        }
    }
}
var oTestClass = testClass()
oTestClass.construct_objects(oTestClass)
oTestClass.set?.test_string_to_argument("test")
print(oTestClass.test_string)

Solution 3 - Ios

Nested for Swift and Java

Swift's Nested is more similar to Java's Static Nested, as a result you do not have an access to properties of outer class. To get access of outer class you can pass it as a parameter.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNJGUYView Question on Stackoverflow
Solution 1 - Iosrob mayoffView Answer on Stackoverflow
Solution 2 - IosJames HazlettView Answer on Stackoverflow
Solution 3 - IosyoAlex5View Answer on Stackoverflow