WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Integer literal overflows when stored into NSNumber

Forums > macOS

So I'm messing around with iTunesLibrary and I am loading test data into a structure. One of the structure properties is an array of track persistentID's. So, I setup the array as such:

var tracks:Array<NSNumber> = [8531377916780748525,10894448951054098798,1913808070485867300,3944816338394235389,930561036379062783,3251308436698647533,14952971713326701209,14970963935055520988,14311328294714471790,8057477945548486586]

Playground does the same thing.

These numbers came from the iTunesLibrary persistentID's for specific tracks, so why don't they work? All I'm doing is manually adding a few track persistentID's to an array for testing purposes. How can an NSNumber not fit within an NSNumber?


I'm not 100% certain this is the answer, but see my post in your previous thread... Dictionary and NSNumber overflow errors

And here's what I did to make your code work:

var tracks:Array<NSNumber> = [
    ].map { $0 as UInt64 as NSNumber }


Yeah, I has seen a post where they recommended using UInt64 because there are issues with NSNumber. I guess I'll just refactor what I've written. It's not like I'll be writing to the iTunesLibrary.


Oh wait. I now see what you did there. I'll give that a try before refactoring!


Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.