MPMediaItemPropertyPersistentID 的 NSNumber 到 NSString 并再次返回

发布于 2024-10-12 14:18:25 字数 1603 浏览 3 评论 0原文

我使用以下代码循环播放 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

煮茶煮酒煮时光 2024-10-19 14:18:25

这工作得很好,到目前为止没有任何问题:

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.

希望这可以帮助遇到此问题的其他人。

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.

通知家属抬走 2024-10-19 14:18:25

如果我理解正确的话,您正在尝试将 NSString 转换为 NSNumber。为此,您可以使用 NSNumberFormatter。看一下 numberFromString: 方法。

NSNumberFormatter 类参考

If I understand you correctly, you're trying to convert an NSString to an NSNumber. To do this, you can use a NSNumberFormatter. Take a look at the numberFromString: method.

NSNumberFormatter Class Reference

小…红帽 2024-10-19 14:18:25

我只得做同样的事情。 MPMediaItemPropertyPersistentID 的查询谓词必须作为 NSNumber 传入。我在另一篇 StackOverflow 帖子中找到了将 NSString 转换为 NSNumber 的答案:

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * persistentIDasNumber = [f numberFromString:persistantID];
[f release];

MPMediaPropertyPredicate * predicate = [MPMediaPropertyPredicate   predicateWithValue:persistentIDasNumber  forProperty:MPMediaItemPropertyPersistentID];

这对我有用。

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:

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * persistentIDasNumber = [f numberFromString:persistantID];
[f release];

MPMediaPropertyPredicate * predicate = [MPMediaPropertyPredicate   predicateWithValue:persistentIDasNumber  forProperty:MPMediaItemPropertyPersistentID];

This worked for me.

遮云壑 2024-10-19 14:18:25

我遇到了非常相似的事情并得出了以下结论:

NSNumber *musicIdentifier = [[[NSNumberFormatter alloc] init] numberFromString: persistentID];
MPMediaQuery *query = [[MPMediaQuery alloc] initWithFilterPredicates:[NSSet setWithObject:[MPMediaPropertyPredicate predicateWithValue:musicIdentifier forProperty:MPMediaItemPropertyPersistentID]]];

MPMediaItem *item = query.items.firstObject;

I ran into something very similar and worked out 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文