The curious case of UIApplication.sharedApplication().scheduledLocalNotifications
if you have spent some times with local notifications you will have undoubtedly have come across
UIApplication.sharedApplication().scheduledLocalNotifications
It is a property that give you all the UILocalNotifications that are scheduled. However, did you know that every time you call it, it will give you a copy?
Like most people playing with arrays I saved it into a local variable
var notifs = UIApplication.sharedApplication().scheduledLocalNotifications
I figured that I’d do some logic and then cancel some like so
UIApplication.sharedApplication().cancelLocalNotification(notifs[0])
where notifs[0] is an arbitrary one I picked for the example to cancel.
To my surprise, the notification that I wanted to remove did not get removed. basically…. it wasn’t part of the current set so it was a no-op.
So here is what I did… I called UIApplication.sharedApplication().scheduledLocalNotifications followed by UIApplication.sharedApplication().scheduledLocalNotifications
and took a look at the pointers… and they were different. so that means instead of saving it to a variable array, I should actually be doing any cancelation directly on this helper method rather than my array.
This is behaviour i would expect If it were an array of structs, but it is an array of references so shouldn’t a copy of the array have the same references?