Programming in Swift · Classes vs. Structures | Ray Wenderlich


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5994-programming-in-swift/lessons/48

Catie and Jessy,

Your videos, well all of videos on this site, are extremely informative and helpful. As I go through the videos, as well as the books, I try to change the code in a manner that would be consistent with apps that I may build at some point in the future. With regard to this lesson on Classes vs Structures, I was trying to create a multidimensional array instead of a single array but, swift either doesn’t allow this, or I am not doing something right (my guess the latter).

Below is the Playground code that I tried. Everything works until I try to append the individual arrays to the mdArray. It runs without errors but it creates an empty multidimensional array. Is it possible to accomplish what I am trying in this manner?

Some of the copied code is not displaying correctly. The the TodoList array for var mdArray is contained in [[]] and the TodoList array for var item is contained in []

All of the var arrayX are contained in []

Thank you,
James

class TodoList {
var itemText = “”
}

var mdArray = [TodoList]
var items = TodoList

var array1 = TodoList
var array2 = TodoList
var array3 = TodoList

var item1 = TodoList()
item1.itemText = “Dentist”
array1.append(item1)

var item2 = TodoList()
item2.itemText = “Home Depot”
array2.append(item2)

var item3 = TodoList()
item3.itemText = “Teleconferences”
array3.append(item3)

mdArray.append(array1)
mdArray.append(array2)
mdArray.append(array3)

1 Like

Hi James!

In order to see where you’re actually starting from, can you please select your code and get it looking like it does in your playground?

  1. The best way is to use Markdown syntax. For that, use surrounding lines of three backticks like so:
    38%20AM

  2. You can also us the “Preformatted text” button, but that doesn’t render syntax as well on the forum.

Hello Jessy,

I tried the preformatted text button but the code is still being modified when I copy into the forum text box. I will keep goggling. I am sure the answer is out there!

Thank you,
James

The array declarations should look like this:

var mdArray: [[TodoList]] = []
var array1: [TodoList] = []
var array2: [TodoList] = []
var array3: [TodoList] = []

Then, after you append your stuff, you should be able to print something out:

print(mdArray[0][0].itemText)
print(mdArray[1][0].itemText)
print(mdArray[2][0].itemText)

@sgerrard

Thank you very much for the feedback. The print statement that you provided solved the issue I was having. Playground is now displaying the content within mdArray.

Thank you,
James