简体   繁体   English

Swift-在将最新通知附加到数组后更改了所有 UILocalNotification 时间

[英]Swift- All UILocalNotification times changed after latest notification is appended to array

I have an array of NSDates that I am iterating through to create an array of UILocalNotification which I then schedule in a batch using scheduledLocalNotifications .我有一个NSDates数组,我正在迭代它以创建一个UILocalNotification数组,然后我使用scheduledLocalNotifications批量scheduledLocalNotifications

When the notification is created it has the correct date.创建通知时,它具有正确的日期。 However, once it is appended to the array, all of the notifications that already exist also take on that date.但是,一旦将其附加到数组中,所有已存在的通知也会在该日期生效。

func scheduleMessageNotifications() {
    let randomTimes = getSemiRandomTimes() //This function generates an array of unique NSDates which all occur after "now" 
    let notification = UILocalNotification()
    var notifications = [UILocalNotification]()

    for time in randomTimes {

        let nextTime = time
        notification.alertBody = "notification"
        notification.fireDate = nextTime
        //Here the notification has the correct date from randomTimes

        notifications.append(notification)
        //Here the array of notifications all contain the same date; the on that the last notification just had
    }
    UIApplication.sharedApplication().scheduledLocalNotifications = notifications
}

I have tried clean build folder, delete derived data, and restarting my computer.我尝试过清理构建文件夹,删除派生数据并重新启动计算机。 I have also tried to make the loop as simple as possible while still keeping its functionality.我还尝试使循环尽可能简单,同时仍保持其功能。 I am creating the array of notifications inside of the function and not calling it or sending it elsewhere.我正在函数内部创建通知数组,而不是调用它或将其发送到其他地方。

Someone suggested that my time might be a reference and therefore affecting the rest of the objects.有人建议我的time可能是一个参考,因此影响了其余的对象。 But after reading some Apple Documentation ( Iterating Over an Array and For-In Loops ), I have no way to verify whether that's true or not.但是在阅读了一些 Apple 文档( 迭代数组For-In 循环)之后,我无法验证这是否正确。 (Or what to do about it.) I did attempt (或者怎么办。)我确实尝试过

let nextTime = time

but it did not seem to make a difference.但这似乎没有什么区别。 Oh, and I know that I am not making more than 64 notifications.哦,我知道我不会发出超过 64 条通知。 (Can't imagine why that would make a difference, but I thought it might come up.) (无法想象为什么这会有所作为,但我认为它可能会出现。)

Any help or ideas would be greatly appreciated.任何帮助或想法将不胜感激。

You are not creating multiple notifications, you are creating one notification and appending it to the array again and again.您不是在创建多个通知,而是在创建一个通知并将其一次又一次地附加到数组中。

Your resulting array contains multiple references to the same object.结果数组包含对同一对象的多个引用。 If you then change date on that object, all the notifications will have the same date because they are the same object.如果您随后更改了该对象的date ,则所有通知都将具有相同的date因为它们是同一个对象。

var notifications = [UILocalNotification]()

for time in randomTimes {
    // move notification creation inside the loop
    let notification = UILocalNotification()

    let nextTime = time
    notification.alertBody = "notification"
    notification.fireDate = nextTime
    //Here the notification has the correct date from randomTimes

    notifications.append(notification)
    //Here the array of notifications all contain the same date; the on that the last notification just had
}

You might also use map call:您也可以使用map调用:

let notifications: [UILocalNotification] = randomTimes.map { time in
    let notification = UILocalNotification()

    let nextTime = time
    notification.alertBody = "notification"
    notification.fireDate = nextTime

    return notification
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM