The curious case of UIApplication.sharedApplication().scheduledLocalNotifications

Derrick Ho
1 min readMay 5, 2016

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?

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response