0

In DataModel. swift i have a class called DataModel with this struct :

struct pippo {
var x : Int
var y : Int
}

struct peppe {
var z : Int
var v : Int
}

struct franco {
var a : Int
var b : Int
}

Now i want to create an Array of Array of struct like this :

var array : [[DataModel.pippo],[DataModel.peppe],[DataModel.franco]] = [[],[],[]]

Is there a method ?

7
  • I think you are looking for a tuple instead: var array : ([DataModel.pippo],[DataModel.peppe],[DataModel.franco]) = ([],[],[]) Am I right? Commented Apr 2, 2020 at 12:46
  • yes man you're right ahahah thank you Commented Apr 2, 2020 at 12:48
  • how can i write element in DataModel.pippo array inside array for example Commented Apr 2, 2020 at 12:50
  • Maybe you could improve your question and expalin how you will be using this array or tuple of arrays. What is the correlation between the arrays, for example why can't you have 3 separate variables instead for your arrays. Commented Apr 2, 2020 at 12:55
  • i want to populate the array inside these for make table divided in sections Commented Apr 2, 2020 at 13:29

2 Answers 2

2

Make another struct

struct Name {
    var pippos = [Pippo]()
    var peppes = [Peppe]()
    var francos = [Franco]()
}
// Then array of names
var names = [Names]()
Sign up to request clarification or add additional context in comments.

2 Comments

how can i write inside pippos array ?
let pippo = Pippo() then names.append(Name(pippos: [Pippo()]))
0
struct pippo {
    var x : Int
    var y : Int
}

struct peppe {
    var z : Int
    var v : Int
}

struct franco {
    var a : Int
    var b : Int
}
struct Names {
    var arrayPippo = [pippo]()
    var arrayPeppe = [peppe]()
    var arrayFranco = [franco]()
}

//After that create a array

    class ViewController: UIViewController {
    var names: [Names]()

    override func viewDidLoad() {
        super.viewDidLoad()
        for name in names {
            print(name.arrayFranco)
            print(name.arrayPeppe)
            print(name.arrayPippo)


            for franco in name.arrayFranco {
                print(franco.a)
            }
        }

    }


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.