试图在 Swift 中复制 Python 列表
Trying to replicate a Python list in Swift
在 Python,我可以做到这一点:
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]
然后这个:
print calendar[2][3[0][3]]
我会把数字 3 还给我。我在 Swift 中很难弄清楚如何做到这一点。
我可以用 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]
但是当我尝试调用一个元素时,出现错误
BedTimeLabel.text = String(january[1],][1])
*** Expected expression in list of expressions ***
或者当我这样尝试时
BedTimeLabel.text = String(format: january[1],arguments: [1])
我收到这个错误
Cannot convert value of type 'NSArray' to expected argument type 'String'
我不了解 Python 所以我不能完全确定我理解你的问题。
据我了解,您有一个多维数组,在 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
在 Python,我可以做到这一点:
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]
然后这个:
print calendar[2][3[0][3]]
我会把数字 3 还给我。我在 Swift 中很难弄清楚如何做到这一点。
我可以用 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]
但是当我尝试调用一个元素时,出现错误
BedTimeLabel.text = String(january[1],][1])
*** Expected expression in list of expressions ***
或者当我这样尝试时
BedTimeLabel.text = String(format: january[1],arguments: [1])
我收到这个错误
Cannot convert value of type 'NSArray' to expected argument type 'String'
我不了解 Python 所以我不能完全确定我理解你的问题。
据我了解,您有一个多维数组,在 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