0

I'm using Swift to create an array that needs to match this format:

let items = [["1", "red", "33", "canada"], ["2", "blue", "66", "usa"]]

In my code, I query a database and have multiple rows returned with the pertinent info:

let items = [id+" - "+colour+" - "+number+" - "+home_location]

I use a loop to .append the array but the format comes out like this instead:

["1 - red - 33 - canada", "2 - blue - 66 - usa"]

What do I need to do to create the required array structure?

0

2 Answers 2

2

For each row of the database, instead of

let items = [id+" - "+colour+" - "+number+" - "+home_location]

say

let items = [id, colour, number, home_location]

Now append that to a var array of [[String]].

Sign up to request clarification or add additional context in comments.

1 Comment

I would also rename items to item. :)
0

create model for your data like this

class ModelClass {

    var id = ""
    var colour = ""
    var number = ""
    var home_location = ""

}

and then create the object of your model class like this

let objmodel : ModelClass = ModelClass()
 objmodel.id = "1"
 objmodel.colour = "red"
 objmodel.number = "33"
 objmodel.home_location = "canada"

then create your main array and append this model object to your model array

var arrData = [ModelClass]()
arrData.append(objmodel)

1 Comment

1. Your class should be a struct. 2. Class and struct names should start with uppercase letters. 3. arrData needs to be var, not let. 4. Let Swift determine a variable's type. Just do var arrData = [ModelClass]().

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.