Why does the for-loop break when iterating through a multidimensional array? -
what i'm trying create editable calendar made of arrays.
first create variables:
import uikit ///////////////////////////////////////////////////////////////////// // variables var username = "string" var bedtime: int = 0 var waketime: int = 7 var sleepmonth = "string" var quantifiedmonth: int = 1
then calendar via multidimensional arrays:
var day0: [int] = [0] var day1: [int] = [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] var day2: [int] = [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] var day3: [int] = [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] var day4: [int] = [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] 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]
then try use nested for-loops allow user when asleep:
@ibaction func setvariablesbutton(_ sender: uibutton) { bedtime = int(bedtimetextfield.text!)! waketime = int(waketimetextfield.text!)! quantifiedmonth = int(monthtoedittextfield.text!)! var hourx = bedtime var hourz = waketime var dayx = 1 let dayz = 1 let lengthmonth = calendar[quantifiedmonth].count _ in 1...lengthmonth // following many days // in month { _ in calendar[1][dayx][hourx]...calendar[1][dayx][24] { calendar[1][dayx][hourx] = 0 // loops amount hourx = hourx + 1 // specified 2 lines above } _ in calendar[1][dayz][0]...calendar[1][dayz][hourz] { calendar[1][dayx][hourz] = 0 hourz = hourz - 1 } dayx = dayx + 1 // loops amount specified 8 lines above } bedtimelabel.text = "\(calendar[1][1])" }
when building, had
var quantifiedmonth
set 0, allowed program work. realized if set 1, breaks. can't wrap head around why, because of output got when program worked.
say had
var quantifiedmonth = 0 bedtime = 20 waketime = 7
then output be
[0, 0, 0, 0, 0, 0, 0, 0, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 0, 0, 0, 0]
but when
var quantifiedmonth = 1
the program crashes. can't wrap head around why is. mind, shouldn't able output when
var quantifiedmonth = 0
please help. tried thorough in question, if i'm being vague something, please let me know.
the problem you're declaring variables outside of for-loop should declared inside of it.
var hourx = bedtime var hourz = waketime
should declared so:
_ in 1...lengthmonth { var hourx = bedtime var hourz = waketime
otherwise variables aren't being set should be.
you need change lengthmonth to
let lengthmonth = calendar[quantifiedmonth].count - 1
since start for-loop @ 1, when array starts @ 0. otherwise you'll "index out of range" error.
Comments
Post a Comment