0

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.

2
  • Do your shopping lists need to have a name or not? Commented Jun 16, 2015 at 13:55
  • YES they need a name. Commented Jun 16, 2015 at 13:59

3 Answers 3

5

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)
Sign up to request clarification or add additional context in comments.

5 Comments

How do i create new lists, like list4?
@nachshonfertel let list4 = ShoppingList(name: "List 4", items: ["any", "items"])
@nachshonfertel Or, shoppingListsArray.append(ShoppingList(name: "Some name", items: ["any", "items"]))
@nachshonfertel I've added an example of this to my answer. Why do you keep changing which answer you accept? You've now accepted another answer which you can't get the name of the shopping list from.
I made my answer before he added the comment. I didn't add the new requirements because you already did in your answer when I came back to see this thread, so I thouht OP would choose yours anyway. Maybe he finally decided that he would choose the first right answer to the first version of the question, before the comment... I upvoted your answer anyway, it was a good idea to add names.
2

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"

1 Comment

How would i access the apples in the first array? like println((shoppingListsArray[0][1]")?
1

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

1 Comment

Thats if you want to append the contents array(Add a new item to shopping list), My question is, How do i create new contents array for each shopping list? For example, the use will create a new shopping list called "shopping list 4", then how will i create a new array to contain the items in that shopping list4?

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.