Search code examples
iosswift2audio-playermpmediaitemmpmediaitemcollection

How to create MPMediaCollection from NSMutableArray?


So I make an app that allows users to create their own playlist, and I'm using MediaPlayer framework from iOS. I have no trouble before I update my X Code to latest version, However I got bellow error warning when I updated my X Code :

..../playlistV2VC.swift:325:37: Cannot invoke initializer for type 'MPMediaItemCollection' with an argument list of type '(items: MPMediaItem?)'

and here is my code:

currentCollectionArray.addObject(User.currentSongAdded!)
var newCollection = MPMediaItemCollection(items: [currentCollectionArray] as? MPMediaItem)

User.currentSongAdded! is a new MPMediaItem.

Give me any suggestion doesnt matter obj-c or swift


Solution

  • If currentCollectionArray is a Swift array of MPMediaItem, then just say MPMediaItemCollection(items: currentCollectionArray).

    If it isn't, then it should be. There is no need for you to be working with NSMutableArray here.

    If it is an NSMutableArray then you will have to double-cast: MPMediaItemCollection(items: currentCollectionArray as NSArray as! [MPMediaItem]).

    But seriously, don't use NSMutableArray unless you have to. It isn't worth the pain.