Ios 如何知道具有名称的当前对象的索引
我有一个名为poemcollection的数组Ios 如何知道具有名称的当前对象的索引,ios,objective-c,Ios,Objective C,我有一个名为poemcollection的数组 poemcollection = [[NSMutableArray alloc] initWithObjects: [[NSBundle mainBundle] pathForResource:@"twinkle_twinkle" ofType:@"mp3"], [[NSBundle mainBundle] pathForResource:@"baa_baa_black_she
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"],
点击按钮
我想知道这个数组中当前正在播放哪个对象,以及这个对象的索引。
我想在日志中打印在播放器上播放的当前文件的名称和索引
[poemcollection indexOfObject:YOUR_CURRENT_PLAYING_OBJECT];
-(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);
}
}
-(无效)打印当前播放
{
NSString*mediaFileName=[[self.player.url absoluteString]lastPathComponent];
BOOL-found=NO;
int poemIndex=-1;
对于(int i=0;i
您可以为媒体名称保留映射字典,例如-
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]
请尝试使用此代码获取当前文件名和对象
NSString *currentFile = [[self.player.url absoluteString] lastPathComponent];
[poemcollection indexOfObject:currentFile];
NSString*mediaFileName=[[self.player.url absoluteString]lastPathComponent];我已经尝试了这段代码来获取当前mp3的名称。它可以工作,但我如何从poemcollection数组中知道当前mp3的索引?为什么不在数组中循环并将文件名与每个数组对象路径中的文件名进行比较?如果有匹配项,你就可以得到你的索引。