Append multiple object at once

Hello everyone,

I am wondering about how can I append multiple objects class to the array in one line?

For example:


class someClass {

var name: String

var ID: Int

init (name: String, ID: Int){

self.name = name

self.ID = ID }

}

So, I want do something like that:

var array = [someClass]()

array.append(someClass(name:"IOS", ID:5), someClass(name:"Android", ID: 4))


can you help me please?

you should write :

array += [someClass(name:"IOS", ID:5), someClass(name:"Android", ID: 4)]

FYI, I tend to do this with:

array.append(contentsOf: […])

but I don’t know if there’s any actual benefit to this once the optimiser has had its way.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Append multiple object at once
 
 
Q