月数组将覆盖而不是附加新值

时间:2019-06-02 16:11:02

标签: arrays swift for-loop

基本上,这就是发生的情况。 该代码按如下方式追加:

1
22
333
4444
55555

代替:

1
12
123
1234
12345... 

有帮助吗?

在下面的代码中,我用所需的值填充对象tempmonth,并将其附加到month:[Month]

尝试构建自定义日历应用程序。我尝试使用month[no]直接编辑数组,但是即使使用month[0],它也会显示索引超出范围。

class Year{
    var month = [Month]()

    func getMonths(){
        let tempMonth = Month()

        //get current date
        let date = Date()
        let calendar = Calendar.current
        var components = calendar.dateComponents([.year, .month, .day], from: date)

        for no in 1...12{
            tempMonth.month_no = no
            components.day = 1
            components.month = no
            tempMonth.allDays.removeAll()

            //find the first day of the week 
            let mydate = NSCalendar.current.date(from: components)!
            let weekdaySample = Calendar.current.component(.weekday, from: mydate)

            tempMonth.firstWeekday = weekdaySample
            //set the total days
            tempMonth.collectDays(tempMonth.month_no,tempMonth.firstWeekday)

            //Fix here 
            month.append(tempMonth)
        }
    }
}

我有12月的12月,而不是12个不同的月。

0 个答案:

没有答案
相关问题