Я пытаюсь построить массив данных с 3000 последовательных дней, но код будет извлекать данные только с одной даты 3000 раз. Когда я печатаю дату, которую я продолжаю увеличивать (theincreasingnumberofdays
), я вижу, что она добавляет один день к дате каждый раз, когда она петли, но когда я читаю массив после цикла, это все те же данные 3000 раз.Попытка получить данные с нескольких последовательных дат
class day
{
var week: Int?
var date: Int?
var month: Int?
var year: Int?
var weekday: Int?
}
@IBAction func pressbuttontodefinearray(sender: AnyObject) {
print("button has been pressed")
if (theArrayContainingAllTheUserData.count > 1) {
print("array contains something allready and we don't do anything")
return
}
print("array is empty and we're filling it right now")
var ScoopDay = day()
var numberofloops = 0
while theArrayContainingAllTheUserData.count < 3000
{
var theincreasingnumberofdays: NSDate {
return NSCalendar.current.date(byAdding: .day, value: numberofloops, to: NSDate() as Date)! as NSDate
}
print(theincreasingnumberofdays)
let myCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
// var thenextdate = calendar.date(byAdding: .day, value: numberofloops, to: date as Date)
numberofloops=numberofloops+1
ScoopDay.week=Int(myCalendar.component(.weekday, from: theincreasingnumberofdays as Date!))
ScoopDay.date=Int(myCalendar.component(.day, from: theincreasingnumberofdays as Date!))
ScoopDay.month=Int(myCalendar.component(.month, from: theincreasingnumberofdays as Date!))
ScoopDay.year=Int(myCalendar.component(.year, from: theincreasingnumberofdays as Date!))
ScoopDay.weekday=Int(myCalendar.component(.weekday, from: theincreasingnumberofdays as Date!))
theArrayContainingAllTheUserData.append(ScoopDay)
}
print("we're done with this looping business. Let's print it")
var placeinarray = 0
while placeinarray < 2998
{
print("Here is", placeinarray, theArrayContainingAllTheUserData[placeinarray].date, theArrayContainingAllTheUserData[placeinarray].month)
placeinarray=placeinarray+1
}
return
}
Побочный Примечание: Ваш цикл очень дорогая. Например, вы создаете 3000 экземпляров календаря того же типа, и все полученные компоненты являются 'Int' в любом случае. – vadian