NSMutableArray中的元素的字符串转换

问题描述

| 因此,我有一个“ 0”,其中包含要在“ 1”中显示的文档名称。 ѭ0中的ѭ2具有空格,因此条目看起来像\“ John Smith \”。然后,我有对应于每个表条目的pdf。这些pdf条目名称不同。它们将类似于\“ JohnSmith.pdf \”。我创建了一种将名称基本上转换为pdf的方法,以呈现适当的pdf。在我的方法中,我基本上将值硬编码
NSUInteger loopCount = 0;
for ( ; loopCount < [array count]; loopCount++) {
    if ([[array objectAtIndex:loopCount] isEqualToString:@\"John Smith\"]) {
        [array replaceObjectAtIndex:loopCount withObject:@\"JohnSmith.pdf\"];
    }
}
一个更好的方法吗?这就是我想到的全部,因为数据已经被命名为不同的名称。谢谢。     

解决方法

        也许您可以使用类似以下的内容:
NSString *filename = [[name stringByReplacingOccurrencesOfString:@\" \" withString:@\"\"] stringByAppendingPathExtension:@\"pdf\"];
    ,        
 NSUInteger loopCount = 0;
 for ( ; loopCount < [array count]; loopCount++) {
    NSString* name = [array objectAtIndex:loopCount];
    [array replaceObjectAtIndex:loopCount withObject:[NSString stringWithFormat@\"%@.pdf\",[name stringByReplacingOccurrencesOfString:@\" \" withString:@\"\"]]];
}