MPMediaEntity enumerateValues crashing

The function enumerateValues on MPMediaEntity is crashing internally when a data item is missing. It appears to be accessing a 0 address inside swift_getObjectType. Anybody else seen this?

I am having the same problem. Some of the items do not have artwork, and this crashes on the first one of those:

`func EnumerateValuesCrash() { let propertiesSet: Set<String> = [ MPMediaItemPropertyTitle, MPMediaItemPropertyArtist ] let query = MPMediaQuery.songs() if let items = query.items { let mediaCollection = MPMediaItemCollection(items: items) for song in mediaCollection.items { song.enumerateValues(forProperties: propertiesSet, using: {_,_,_ in }) } } }`

MPMediaEntity.enumerateValues is broken and has been broken in Swift for a very long time. At last one Radar (rdar://40779720) was apparently filed in 2018.

The issue is that the block passed to enumerateValues can be called with a nil value when there are no properties of the requested type. To be clear, it is not that one or more of the arguments to the block may be nil, but rather that the block itself will be called with a nil value. This is an ObjC convention that does not translate to Swift and Apple has never bothered to fix this issue in MPMediaEntity.

The only/best workaround of which I'm aware can be found here: https://gist.github.com/StephenHeaps/40ea93012600f7e9abad9bd9bdc9084b

This fix is in the ObjC domain thus you will have to import these files and update/create a bridging header for use in Swift.

Full disclaimer. This is not my fix but I am greatly thankful to StephenHeaps on GitHub for the GIST.

MPMediaEntity enumerateValues crashing
 
 
Q