NSNumber for MPMediaItemPropertyPersistentID for NSString and vice versa

I browse all the songs from the iPhone music library using the following code:

NSArray * songs = [[NSArray alloc] initWithArray:[[MPMediaQuery songsQuery] collections]]; for (MPMediaItemCollection * item in songs){ NSString * persistentID = [[[item representativeItem] valueForProperty:MPMediaItemPropertyPersistentID] stringValue]; // Do something with it. } [songs release]; 

Pretty simple stuff.

I get PersistentID as NSString because I need to write it to an XML file (for transmission over the network to another device). Therefore, I can’t just leave it as NSNumber .

Then another device will ask the iPhone to play the track, again transmitting PersistentID.

At this point, the iPhone has the NSString PersistentID of the track to play.

It would be funny to repeat each song and compare PersistentID until I find the right track, so I'm trying to use MPMediaPropertyPredicate to find the iPhone for me.

I use the following code to search:

 MPMediaPropertyPredicate * predicate = [MPMediaPropertyPredicate predicateWithValue:persistentID forProperty:MPMediaItemPropertyPersistentID]; MPMediaQuery * songsQuery = [[MPMediaQuery alloc] init]; [songsQuery addFilterPredicate:predicate]; if ([[songsQuery items] count]){ MPMediaItem * item = [[songsQuery items] objectAtIndex:0]; // Play item. } [songsQuery release]; 

Where persistentID is NSString from earlier.

Strange, this works for some songs, not for others. those. sometimes the items array is not empty, although I pass in an NSString , not an NSNumber .

I am wondering if there is a way to convert my NSString back to NSNumber , where it came from, and how I can do it.

UPDATE : I tried NSNumberFormatter, I also tried something like:

 [NSNumber numberWithFloat:[persID floatValue]]; 

I tried all the standard ways to do this without prevailing.

+5
source share
4 answers

This works very well, no problem so far:

 unsigned long long ullvalue = strtoull([persistentID UTF8String], NULL, 0); NSNumber * numberID = [[NSNumber alloc] initWithUnsignedLongLong:ullvalue]; MPMediaPropertyPredicate * predicate = [MPMediaPropertyPredicate predicateWithValue:numberID forProperty:MPMediaItemPropertyPersistentID]; [numberID release]; // And so on. 

Hope this helps anyone who comes across this issue.

+15
source

If you understand correctly, you are trying to convert NSString to NSNumber . You can use NSNumberFormatter . Take a look at the numberFromString: method.

NSNumberFormatter Class Description

0
source

I just needed to do the same. The request predicate for MPMediaItemPropertyPersistentID must be passed as NSNumber. I found this answer when converting NSString to NSNumber from another StackOverflow post:

 NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; [f setNumberStyle:NSNumberFormatterDecimalStyle]; NSNumber * persistentIDasNumber = [f numberFromString:persistantID]; [f release]; MPMediaPropertyPredicate * predicate = [MPMediaPropertyPredicate predicateWithValue:persistentIDasNumber forProperty:MPMediaItemPropertyPersistentID]; 

It worked for me.

0
source

I came across something very similar and developed the following:

 NSNumber *musicIdentifier = [[[NSNumberFormatter alloc] init] numberFromString: persistentID]; MPMediaQuery *query = [[MPMediaQuery alloc] initWithFilterPredicates:[NSSet setWithObject:[MPMediaPropertyPredicate predicateWithValue:musicIdentifier forProperty:MPMediaItemPropertyPersistentID]]]; MPMediaItem *item = query.items.firstObject; 
0
source

Source: https://habr.com/ru/post/911758/


All Articles