Nested Types in Swift - What Is the Good Practice

Nested types in Swift - what is the good practice?

After much searching, all I've found is @jpsim's remark that "the idea behind the nesting rule is to avoid complex interfaces".

Therefore, apart from the compiler issues outlined in my question, which will eventually subside, there seems to not be any technical reason for this rule.

It is a good practice to use inner classes in swift for models?

This is a good practice to make your namespace organized and the question is related to the below one:
Swift nested class properties

Swift nested class properties

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)

What is the benefit of nesting functions (in general/in Swift)


So if the purported benefit is to "organize the code", why not just have the nested function independently, outside of the outer function? That, to me, seems more organized.

Oh, I totally disagree. If the only place where the second function is needed is inside the first function, keeping it inside the first function is much more organized.

Real-life examples here: http://www.apeth.com/swiftBook/ch02.html#_function_in_function

Plus, a function in a function has the local environment in scope. Code inside the nested function can "see" local variables declared before the nested function declaration. This can be much more convenient and natural than passing a bunch of parameters.

However, the key thing that a local function lets you do that you could not readily do in any other way is that you can form the function in real time (because a function is a closure) and return it from the outer function.

http://www.apeth.com/swiftBook/ch02.html#_function_returning_function

Best way to store nested data in swift

IMHO, there isn't a best way for creating this kind of stuff. It depends on what you like, you data you want to store, and how deep the nesting is.

I'll show a few here.

  1. Classes and Structs

You just created some classes and/or structs that have the properties and sub-objects you want. Your example object's class might look like this

class ObjectA {
let name: String
let subObj: SubObject

init(name: String, subObj: SubObject) {
self.name = name
self.subObj = subObj
}
}

class SubObject {
let name: String
init(name: String) {
self.name = name
}
}

Of course, you should name your classes meaningfully.


  1. Tuples

This is best for objects which have only 1 level of nesting. You can do this with 2 but it looks kind of ugly. Don't do it with more than 3, no one understands.

This is a nested tuple:

let tuple: (String, (String, String)) = ...

If you do it with more than 3 levels of nesting, it will take people a long time to figure out what you mean:

let tuple: (String, (String, (String, (String, (String, String))))) = ...

  1. Property lists

This can be best used to store a large amount of constant data that is too verbose to create in code. Just add a new property list file into your project and read it with one of the NSBundle methods.

Your object's property list will look like this:

Sample Image


  1. JSON

Personally, I think JSON is so convenient when combined with JSON parsing libraries like SwiftyJSON. Your object's JSON representation is something like this:

objA: {
name: "A Name",
subObj: {
name: "A Sub Name"
}
}

Swift - Best practice to pass nested arrays into a function

Following the suggestions from this answer, you can try the following (Hints are in the code comments) :

var multiArray = [[1,2,3],[4,5,6],[7,8,9]]

// loop through your multiArray
for array in multiArray {

// Turning an array of Int into String with a separator between numbers
// In your case its only "" because you don't want to have separator like a dash for example
let stringArray = array.flatMap({ String($0) })
let stringRepresentationOfArray = stringArray.joinWithSeparator("")

print(stringRepresentationOfArray)
}

// Output
// 123
// 456
// 789


Related Topics



Leave a reply



Submit