UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 { $0.name.lowercased().contains("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
    //print(playlist.name)
    // 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(playlist.name.suffix(playlist.name.count-6))
    // 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.

3      

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)

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.