如何知道当前Object的索引名称

时间:2015-05-28 06:19:45

标签: ios objective-c

我有一个名为poemcollection的数组

poemcollection = [[NSMutableArray alloc] initWithObjects:
                  [[NSBundle mainBundle] pathForResource:@"twinkle_twinkle" ofType:@"mp3"],
                  [[NSBundle mainBundle] pathForResource:@"baa_baa_black_sheep" ofType:@"mp3"],
                  [[NSBundle mainBundle] pathForResource:@"johny_johny" ofType:@"mp3"],
点击按钮

我想知道当前从这个数组和该对象的索引播放的对象。 我想在日志中打印这个在播放器上播放的当前文件的名称和索引

4 个答案:

答案 0 :(得分:0)

[poemcollection indexOfObject:YOUR_CURRENT_PLAYING_OBJECT];

答案 1 :(得分:0)

这样的东西?

-(void)printCurrentlyPlaying
{

    NSString *mediaFileName = [[self.player.url absoluteString] lastPathComponent];

    BOOL found = NO;

    int poemIndex = -1;

    for(int i = 0; i < poemcollection.count && !found; i++)
    {
        NSString *filePath = poemcollection[i];

        NSString *fileName = [filePath lastPathComponent];

        if([fileName isEqualToString:mediaFileName])
        {
            found = YES;
            poemIndex = i;
        }
    }

    if(found)
    {
        NSLog(@"Currently playing: %@, track number: %d", mediaFileName, poemIndex);
    }

}

答案 2 :(得分:0)

您可以保留媒体名称的映射字典,例如

    poemCollection = @[ @{"Twinkle twinkle": [[NSMutableArray alloc] initWithObjects:
                  [[NSBundle mainBundle] pathForResource:@"twinkle_twinkle" ofType:@"mp3"]},
                  @{@"Baa baa black sheep": [[NSBundle mainBundle] pathForResource:@"baa_baa_black_sheep" ofType:@"mp3"]},
                  @{"Johny Johny": [[NSBundle mainBundle] pathForResource:@"johny_johny" ofType:@"mp3"]}];

//And read the title like below :
NSString *title = [poemCollection[index] allKeys][0]
NSString *musicFile = [poemCollection[index] allValues][0]

答案 3 :(得分:0)

尝试使用此代码获取当前文件名和对象。

NSString *currentFile = [[self.player.url absoluteString] lastPathComponent];
[poemcollection indexOfObject:currentFile];