travis travis - 1 month ago 12
Python Question

Trying to replicate a Python list in Swift

In Python, I'm able to do this:

day0 = 0
day1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25]

day2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25]

day3 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25]

day4 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25]

month0 = 0
january = [day0, day1, day2, day3, day4]
february = [day0, day1, day2, day3, day4]
march = [day0, day1, day2, day3, day4]
april = [day0, day1, day2, day3, day4]
may = [day0, day1, day2, day3, day4]
june = [day0, day1, day2, day3, day4]
july = [day0, day1, day2, day3, day4]
august = [day0, day1, day2, day3, day4]
september = [day0, day1, day2, day3, day4]
october = [day0, day1, day2, day3, day4]
november = [day0, day1, day2, day3, day4]
december = [day0, day1, day2, day3, day4]

calendar = [month0, january, february, march, april, may, june, july, august, september,
october, november, december]


then this:

print calendar[2][3[0][3]]


and I would get the number 3 returned back to me. I'm having a hard time figuring out how to do this in Swift.

I can create the lists in basically the same way in Swift

var day0 = 0
var day1 = [0, 8, 2, 3, 4]
var day2 = [0, 1, 2, 3, 4]
var day3 = [0, 1, 2, 3, 4]
var day4 = [0, 1, 2, 3, 4]

var month0 = 0
var january = [[day0, day1, day2, day3, day4]]
var february = [day0, day1, day2, day3, day4]
var march = [day0, day1, day2, day3, day4]
var april = [day0, day1, day2, day3, day4]
var may = [day0, day1, day2, day3, day4]
var june = [day0, day1, day2, day3, day4]
var july = [day0, day1, day2, day3, day4]
var august = [day0, day1, day2, day3, day4]
var september = [day0, day1, day2, day3, day4]
var october = [day0, day1, day2, day3, day4]
var november = [day0, day1, day2, day3, day4]
var december = [day0, day1, day2, day3, day4]

var calendar = [month0, january, february, march, april, may, june, july, august, september, october, december]


But when I try to call an element, I get an error

BedTimeLabel.text = String(january[1],][1])
*** Expected expression in list of expressions ***


Or when I try it this way

BedTimeLabel.text = String(format: january[1],arguments: [1])


I get this error

Cannot convert value of type 'NSArray' to expected argument type 'String'

Answer

I have no knowledge about Python so I'm not entirely sure I understand your question.

From what I understood you have a multi dimensional array which just looks like this in Swift:

let day1 = [0, 1, 2, 3, 4, 5, 6, 7]
let day2 = [0, 1, 2, 3, 4, 5, 6, 7]
let day3 = [0, 1, 2, 3, 4, 5, 6, 7]
let day4 = [0, 1, 2, 3, 4, 5, 6, 7]

let jan = [day1, day2, day3, day4]
let feb = [day1, day2, day3, day4]
let mar = [day1, day2, day3, day4]
let apr = [day1, day2, day3, day4]

let calendar = [jan, feb, mar, apr]

let result = calendar[2][3][3]        // 3