TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Dictionary and NSNumber overflow errors

Forums > Swift

I am getting these errors when I run the below code in my playground:

warning: iTunesLibrary.playground:48:37: warning: string interpolation produces a debug description for an optional value; did you mean to make this explicit?
print("Playlist \(playlistName) = \(playlistDict[playlistName]![persistentID])\n")

iTunesLibrary.playground:48:64: note: use 'String(describing:)' to silence this warning
print("Playlist \(playlistName) = \(playlistDict[playlistName]![persistentID])\n")
                                    String(describing:                       )

iTunesLibrary.playground:48:64: note: provide a default value to avoid this warning
print("Playlist \(playlistName) = \(playlistDict[playlistName]![persistentID])\n")
                                                                              ?? <#default value#>

error: iTunesLibrary.playground:46:20: error: integer literal '11065184750558187926' overflows when stored into 'NSNumber'
var persistentID = 11065184750558187926 as NSNumber

Here's the code:

import Foundation
import iTunesLibrary

// Structure for tracks
struct TrackInfo {
    var title = ""
    var artist = ""
    var album = ""
    var totalTime = 0
    var year = 0
    var persistentID = NSNumber()
    var location = URL(string: "")

// Link to the Music Library
let library = try ITLibrary(apiVersion: "1.1")
// Get the location of the Music Library
let media = library.mediaFolderLocation // <-- required
// Get all playlists from the Music library that are prefixed with the word "tests-"
let allPlaylists = library.allPlaylists.filter { $"tests-") } // All playlist names are dropped to lowercase for comparison

// Create the playlistDict with a structure of [playlistName:[persistentID:TrackInfo()]] and place that info playlist
let playlistDict = allPlaylists.reduce(into: [String:[NSNumber:TrackInfo]]()) { playlistResult, playlist in
    // From each playlist in allPlaylists extract the needed trackInfo itemss and place into trackDict
    let trackDict = playlist.items.reduce(into: [NSNumber:TrackInfo]()) { tracksResult, track in
            var trackInfo = TrackInfo()
            trackInfo.title = track.title
            trackInfo.artist = track.artist?.name ?? ""
            trackInfo.album = track.album.title ?? ""
            trackInfo.totalTime = track.totalTime
            trackInfo.year = track.year
            trackInfo.location = track.location
            trackInfo.persistentID = track.persistentID
            tracksResult[trackInfo.persistentID] = trackInfo
    // Remove the prefix tests- from the playlist name
    let name = String(
    // Add the trackDict to the named playlistResult
    playlistResult[name] = trackDict

// Create a variable with a playlist name
var playlistName: String = "1976 Hot Hits-1"
// Create a varible with the persistendID of a track within the playlist
var persistentID = 11065184750558187926 as NSNumber

print("Playlist \(playlistName) = \(playlistDict[playlistName]![persistentID])\n")

Am I not referring to a dictionary element correctly? I also don't understand why I am getting an overflow error for NSNumber, when it is coming directly from the iTunes Library code that way.


The first three aren't errors, they're warnings, and they're because fetching a value from a Dictionary returns an Optional since there may or may not actually be a value for the key you pass in. You can ignore these if you like or follow XCode's advice to silence them; it doesn't really matter.

As for the fourth issue, I think it's because since NSNumber is technically a class cluster that can be one of several different kinds of numbers behind the scenes, the compiler doesn't know what kind you actually want when you try to cast it.

You can fix the error by directly initializing your NSNumber with a UInt64:

var persistentID = NSNumber(value: 11065184750558187926 as UInt64)


Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.