5
\$\begingroup\$

I am currently working on an implementation of SoundCloud for a 3rd party iOS app, and as there is no SDK anymore I am doing all my calls via Alamofire.

This is what SC expects you to send in your body params:

{ "playlist":
    {"tracks":[
        {"id":"__"}, 
        {"id":"__"}, 
        {"id":"__"}]
    }
}

I hand over the current playlist where the song should be added in the playlist object to extract all the ids from tracks that are part of it atm - as SC expects you so send all the old track ids + your new one in a .PUT call

This is what the ID dance looks at the moment:

var allTracks = [Int]() //SC track id comes in as an Int

allTracks.append(trackId) //trackId is the new track

for track in playlist.tracks {
    var I = track.id
    allTracks.append(I)
}

var myparams = Dictionary<String,AnyObject>() //my Alamofire body-params

var myTracks = [Dictionary<String,AnyObject>]()

for id in allTracks {
    var D = ["id":id]
    myTracks.append(D)
}

myparams["playlist"] = ["tracks":myTracks]

As I am still fairly new to Swift/iOS Development I was looking for a more elegant way than to just throw stuff around 3x and end up basically appending it the way it came in.

\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

You can make this easier with mapping.

So, let's clarify what we have.

  • playlist.tracks - An array of "tracks" which have an id property (which is what we need).
  • trackID - an integer representing our new track
  • myTracks - an array of dictionaries in the form of key "id" and value being the track ID.
  • myParams - a dictionary with the key of "tracks" and the value being the aforementioned myTracks array.

So we need to turn playlist.tracks into an array of ["id":id] dictionaries with the new track ID also in there.

First, let's ignore the new track, and map the existing tracks into an array of dictionaries:

var trackIDdicts: [[String:Int]] = playlist.tracks.map { ["id":$0.id] }

And now insert our new track ID at the front:

trackIDdicts.insert(["id":trackId], atIndex:0)

And now for myParams?

myParams["playlist"] = ["tracks":trackIDdicts]

Altogether, three simple lines of code:

var trackIDdicts: [[String:Int]] = playlist.tracks.map { ["id":$0.id] }
trackIDdicts.insert(["id":trackId], atIndex:0)
myParams["playlist"] = ["tracks":trackIDdicts]

I'd highly recommend converting your JSON/Dictionary keys into named constant variables however.

struct JSONKeys {
    static let TrackID = "id"
    static let Tracks = "tracks"
    static let Playlist = "playlist"   
}

So now we have:

var trackIDdicts: [[String:Int]] = playlist.tracks.map { [JSONKeys.TrackID:$0.id] }
trackIDdicts.insert([JSONKeys.TrackID:trackId], atIndex:0)
myParams[JSONKeys.Playlist] = [JSONKeys.Tracks:trackIDdicts]
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.