NSNumber为MPMediaItemPropertyPersistentID到NSString和回来 [英] NSNumber for MPMediaItemPropertyPersistentID to NSString and back again

查看:107
本文介绍了NSNumber为MPMediaItemPropertyPersistentID到NSString和回来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码循环播放来自iPhone音乐库的所有歌曲:

I'm looping through all the songs from an iPhone's 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];

很基本的东西。

将PersistentID作为 NSString ,因为我需要将其写入XML文件(通过网络传输到另一个设备)。因此,我不能把它作为一个 NSNumber

I'm getting the PersistentID as an NSString because I need to write it to an XML file (for transmission over a network to another device). Hence the reason I can't just leave it as an NSNumber.

的原因然后另一个设备将要求

The other device will then ask for the iPhone to play a track by transmitting the PersistentID back again.

此时,iPhone有一个 NSString

At this point, the iPhone has an NSString of the PersistentID of the track it should play.

这将是精彩的循环通过每首歌曲,并比较PersistentIDs,直到我找到我想要的轨道,所以我试着使用 MPMediaPropertyPredicate 让iPhone搜索我。

It would be combersome to loop through every song again and compare PersistentIDs until I find the track I want, so I'm trying to use the MPMediaPropertyPredicate to have the iPhone search for me.

我使用以下代码进行搜索:

I'm using the following code for the 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];

其中 persistentID NSString

奇怪的是,这适用于一些歌曲,而不是其他歌曲。即,有时 items 数组不为空,即使我传递一个 NSString ,而不是 NSNumber

Weirdly, this works for some songs, not for others. i.e, sometimes the items array is not empty, even though I'm passing an NSString, not an NSNumber.

我想知道是否有一种方法可以转换我的 NSString 回到 NSNumber 它来自,我怎么能这样做。

I'm wondering if there's a way to convert my NSString back to the NSNumber it came from, and how I can do that.

UPDATE :我试过NSNumberFormatter,我也试过像:

UPDATE: I've tried the NSNumberFormatter, I've also tried something like:

[NSNumber numberWithFloat:[persID floatValue]];

我已经尝试了所有的标准方法,没有优势。

I've tried all the standard ways of doing it without prevail.

推荐答案

这样运行得很好,到目前为止没有问题:

This is working pretty well, no problems 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 else who ran into this problem.

这篇关于NSNumber为MPMediaItemPropertyPersistentID到NSString和回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆