Joe Glover Joe Glover - 1 year ago 141
Objective-C Question

Spotify API to Swift

May be a simple question, but I am trying to use Spotify's API which is documented in Objective C but I have been working on it in Swift as far as I can.

@property (nonatomic, readonly, copy) NSArray *genres
is one of their API protocols for the SPTartist class, however; I can't figure out how to use it in a working manner in swift.

Here is example code of something I have gotten to work.

func updateTrackLength(){
if player?.currentTrackMetadata == nil {
trackLength.text = ""
let length = player?.currentTrackMetadata[SPTAudioStreamingMetadataTrackDuration] as Int
let convert = length / 60
let remainder = length % 60
var newlength = String(convert)
var newRemainder = String(remainder)

dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.trackLength.text = "Track length: " + newlength + " Minutes " + newRemainder + " Seconds"

This uses the currentTrackMetaData and whatever SPT protocol to track the string output. When I try using SPTArtistGenres or SPTArtistGenre in place of SPTAudioStreamingMetaDataTrackDuration, it does not work.
To specify my question, I want to use the currentTrackMetaData to call the genres property up above. Any advice is greatly appreciated!

Answer Source

This has absolutely nothing to do with Swift - if you tried to do what you're doing in Objective-C it'd fail as well.

The player's currentTrackMetadata dictionary returns bare URLs - if you just println() the response to player.currentTrackMetadata, you'll see that it contains nothing but URLs.

The genres property is on the SPTArtist class. To convert your bare URL into an SPTArtist, use the SPTRequest class (specifically, the requestItemAtURI method). Once you have an SPTArtist object, you'll be able to get all the metadata you need from it, including genres.

Please see the documentation shipped with the library for details. In addition, the Simple Player demo app included with the SDK does a similar thing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download