MPMediaItemPropertyPersistentID 的 NSNumber 到 NSString 并再次返回
我使用以下代码循环播放 iPhone 音乐库中的所有歌曲:
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];
非常基本的东西。
我以 NSString
形式获取 PercientID,因为我需要将其写入 XML 文件(以便通过网络传输到另一个设备)。因此,我不能将其保留为 NSNumber
。
然后,另一台设备将通过再次传回 PersistentID 来要求 iPhone 播放曲目。
此时,iPhone 已拥有应播放曲目的 PersistentID 的 NSString
。
再次循环每首歌曲并比较 PersistentID 直到找到我想要的曲目会很麻烦,因此我尝试使用 MPMediaPropertyPredicate
让 iPhone 搜索我。
我使用以下代码进行搜索:
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
。
我想知道是否有办法将我的 NSString
转换回它来自的 NSNumber
,以及如何做到这一点。
更新:我已经尝试过 NSNumberFormatter,我也尝试过类似的方法:
[NSNumber numberWithFloat:[persID floatValue]];
我已经尝试了所有标准方法,但没有成功。
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];
Pretty basic stuff.
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.
At this point, the iPhone has an NSString
of the PersistentID of the track it should play.
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];
Where persistentID
is the NSString
from earlier.
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
.
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: 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这工作得很好,到目前为止没有任何问题:
希望这可以帮助遇到此问题的其他人。
This is working pretty well, no problems so far:
Hope this helps anyone else who ran into this problem.
如果我理解正确的话,您正在尝试将
NSString
转换为NSNumber
。为此,您可以使用NSNumberFormatter
。看一下numberFromString:
方法。NSNumberFormatter 类参考
If I understand you correctly, you're trying to convert an
NSString
to anNSNumber
. To do this, you can use aNSNumberFormatter
. Take a look at thenumberFromString:
method.NSNumberFormatter Class Reference
我只得做同样的事情。 MPMediaItemPropertyPersistentID 的查询谓词必须作为 NSNumber 传入。我在另一篇 StackOverflow 帖子中找到了将 NSString 转换为 NSNumber 的答案:
这对我有用。
I just had to do the same thing. The Query Predicate for MPMediaItemPropertyPersistentID has to be passed in as NSNumber. I found this answer on converting NSString to NSNumber from another StackOverflow post:
This worked for me.
我遇到了非常相似的事情并得出了以下结论:
I ran into something very similar and worked out the following: