繁体   English   中英

用于遍历数组中的属性

[英]For Looping through Property in an Array

我试图遍历名为songs数组,该数组包含iPod库中用户歌曲的列表,但是要获取标题,我需要这样做(获取歌曲标题的NSString ):

[[songs objectAtIndex:i] valueForProperty:MPMediaItemPropertyTitle]

我正在尝试创建tableView的索引,但是我停留在这一点:

for (NSString *title = MPMediaItemPropertyTitle in songs)
{
    rowTitle= [title substringToIndex:1];  //modifying the statement to its first alphabet
    if ([rowTitle isEqualToString:sectionTitle])  //checking if modified statement is same as section title
    {
        [rowContainer addObject:title];  //adding the row contents of a particular section in array
    }
}

我哪里出错了

-[MPConcreteMediaItem substringToIndex:]:无法识别的选择器已发送到实例

在这一行: rowTitle= [title substringToIndex:1];

如何遍历歌曲以获取MPMediaItemPropertyTitle,然后获取歌曲标题的首字母? 我以为我在声明NSString的title并循环浏览歌曲中的所有标题。 显然我不是:S。

我正在关注本教程 有人可以帮我吗? 谢谢。

这个...

for (NSString *title = MPMediaItemPropertyTitle in songs)

应该...

for (MPMediaItem *song in songs) {
    NSString *title = [song valueForProperty:MPMediaItemPropertyTitle];
}

您的原始代码将title引用指向MPConcreteMediaItem项目。

尝试这个。

for (NSString *title in songs)
   {
     rowTitle= [title substringToIndex:1];  //modifying the statement to its first alphabet

       if ([rowTitle isEqualToString:sectionTitle])  //checking if modified statement is same as section title
             {
                [rowContainer addObject:title];  //adding the row contents of a particular section in array
              }
            }

for..in循环浏览songs数组中的对象。 它不会自动发送valueForProperty消息,因此您必须自己执行以下操作:

for (MPMediaItem *song in songs)
{
    NSString *title = [song valueForProperty:MPMediaItemPropertyTitle];
    rowTitle= [title substringToIndex:1];  //modifying the statement to its first alphabet
    if ([rowTitle isEqualToString:sectionTitle])  //checking if modified statement is same as section title
    {
        [rowContainer addObject:title];  //adding the row contents of a particular section in array
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM