nachshon f
nachshon f

Reputation: 3680

How to i create new arrays using swift?

I'm not exactly sure what I am looking for, so I'm just going to give you an example...

Inside App

Lets say I am making a shopping list app. In the app, you can create as many shopping lists as you like, and in those shopping lists, you can add items to the list.

Inside Code

So basically, I'm trying to create a new array of items within an array of shopping lists. How do I do that? Here is my code if I have a set shopping list app. But how do I allow the user to create new shopping lists (In code, A new array)?

//users can keep on appending this array every time they create a shopping list

var shoppingListsArray:[String] = ["Shopping list 1", "Shopping list 2", "Shopping list 3"]

var contentsOfShoppingList1 = ["Item1", "Item2", "Item3"]

var contentsOfShoppingList2 = ["Item1", "Item2", "Item3"]

var contentsOfShoppingList3 = ["Item1", "Item2", "Item3"]

When the user creates a new shopping list and appends the 'shoppingListsArray', how do I create a new array of the contents of that shopping list?

I hope I did a good job explaining my question. Please feel free to edit this question or leave a comment if you don't understand something.

Upvotes: 0

Views: 230

Answers (3)

Ben Kane
Ben Kane

Reputation: 10040

You mentioned that it's a requirement for your shopping lists to have a name. You could use an NSDictionary for this, but then all the shopping lists would have to have unique names.

The easiest and most flexible way to approach this would be to make a ShoppingList struct that holds the name of the list and an array of the items in the list. You would then make your shoppingListsArray an array of ShoppingList structs.

Here's a small example:

struct ShoppingList {
    let name: String
    let items: [String]
}

let list1 = ShoppingList(name: "List 1", items: ["Item 1", "Item 2", "Item 3"])
let list2 = ShoppingList(name: "List 2", items: ["Item 1", "Item 2", "Item 3"])
let list3 = ShoppingList(name: "List 3", items: ["Item 1", "Item 2", "Item 3"])
var shoppingListsArray = [list1, list2, list3]

If you want to access the second item in the second shopping list, you would use this code:

let item = shoppingListsArray[1].items[1]

If you wanted to access the name of the second shopping list, you would use this code:

let name = shoppingListsArray[1].name

If you want to add another shopping list to the array later, just make another list and append it like so:

let anotherList = ShoppingList(name: "foo shopping list", items: ["foo", "bar", "baz"])
shoppingListsArray.append(anotherShoppingList)

Upvotes: 5

Eric Aya
Eric Aya

Reputation: 70098

Because your shopping lists are arrays of strings, and you want to put these arrays into a new array of shopping lists, you need an array of arrays of strings.

Like this:

let contentsOfShoppingList1 = ["Bananas", "Apples", "Oranges"]
let contentsOfShoppingList2 = ["Eggs", "Bread", "Salt"]

var shoppingListsArray = [[String]]()

shoppingListsArray.append(contentsOfShoppingList1)
shoppingListsArray.append(contentsOfShoppingList2)

println(shoppingListsArray) // [[Bananas, Apples, Oranges], [Eggs, Bread, Salt]]

UPDATE:

Access the datas like this:

let apples = shoppingListsArray[0][1] // "Apples"
let salt = shoppingListsArray[1][2] // "Salt"

Upvotes: 2

Code Different
Code Different

Reputation: 93161

Edit: respond to the new requirement.

I think you should keep shoppingListsArray as a Dictionary:

var shoppingListsArray = Dictionary<String, [String]>()

var contentsOfShoppingList1 = ["Item 1", "Item 2", "Item 3"]
var contentsOfShoppingList2 = ["Item 1", "Item 2", "Item 3"]
var contentsOfShoppingList3 = ["Item 1", "Item 2", "Item 3"]

shoppingListsArray["Shopping List 1"] = contentsOfShoppingList1
shoppingListsArray["Shopping List 2"] = contentsOfShoppingList2
shoppingListsArray["Shopping List 3"] = contentsOfShoppingList3

// When user create new shopping list
var newShoppingList = ["Computer", "Monitor"]

shoppingListArray["Computer parts"] = newShoppingList

Upvotes: 1

Related Questions