0

我是 iphone 新手。我有一个数组,其中包含“04_Num”下面的对象,

  "04_Num/04Num.m3u",
    "04_Num/04Num001.mp3",
    "04_Num/04Num002.mp3",
    "04_Num/04Num003.mp3",
    "04_Num/04Num004.mp3",
    "04_Num/04Num005.mp3",
    "04_Num/04Num006.mp3",
    "04_Num/04Num007.mp3",
    "04_Num/04Num008.mp3",
    "04_Num/04Num009.mp3",
    "04_Num/04Num010.mp3",
    "04_Num/04Num011.mp3",
    "04_Num/04Num012.mp3",
    "04_Num/04Num013.mp3",
    "04_Num/04Num014.mp3",
    "04_Num/04Num015.mp3",
    "04_Num/04Num016.mp3",
    "04_Num/04Num017.mp3",
    "04_Num/04Num018.mp3",
    "04_Num/04Num019.mp3",
    "04_Num/04Num020.mp3",
    "04_Num/04Num021.mp3",
    "04_Num/04Num022.mp3",
    "04_Num/04Num023.mp3",
    "04_Num/04Num024.mp3",
    "04_Num/04Num025.mp3",
    "04_Num/04Num026.mp3",
    "04_Num/04Num027.mp3",
    "04_Num/04Num028.mp3",
    "04_Num/04Num029.mp3",
    "04_Num/04Num030.mp3",
    "04_Num/04Num031.mp3",
    "04_Num/04Num032.mp3",
    "04_Num/04Num033.mp3",
    "04_Num/04Num034.mp3",
    "04_Num/04Num035.mp3",
    "04_Num/04Num036.mp3"

但在这里我只想要包含 .mp3 扩展名的对象,然后我必须将这些对象放入另一个数组中

如果有人知道这怎么可能,请帮助我...

4

3 回答 3

0

您可以迭代并仅获取具有 .mp3 的那个

像这样

//yourArrayThatContainsAllStrings contains all the strings
NSMutableArray *arrayOfMp3 = [[NSMutableArray alloc] init];

for (NSString *str in yourArrayThatContainsAllStrings) {
    if ([str rangeOfString:@".mp3"].location != NSNotFound) {
        [arrayOfMp3 addObject:str];
    }
}

//arrayOfMp3 will contain only the .mp3 files
于 2012-06-18T09:01:58.207 回答
0
NSMutableArray *mpthrees = [[NSMutableArray alloc] initWithCapacity:0];
for (NSString *file in songs) //Where songs is the array with the paths you have provided
{
    BOOL isMpthree = [[file pathExtension] isEqualToString:@"mp3"];
    if (isMpthree) [mpthrees addObject:file];
}
// Now mpthrees array holds only paths pointing to .mp3 files
于 2012-06-18T09:07:44.023 回答
0
// Let's call your array of strings as stringsArray
NSMutableArray *filteredArray = [[NSMutableArray alloc] init];

for (NSString *str in stringsArray) {
    if ([str hasSuffix:@".mp3"]) {
        [filteredArray addObject:str];
    } }

//filteredArray will contain only the strings ending with ".mp3"

PS 由于您刚刚开始使用Objective C,我想重申Objective C NSString 对象总是以@ 符号开头。@"04_Num/04Num001.mp3"

于 2012-06-18T09:25:45.833 回答