2011-05-31 63 views
0

因此,我有一个NSArray,其文档的名称将在UITableView中显示。 NSArray中的NSString有空格,所以条目看起来像“John Smith”。然后我有对应于每个表项的pdf。这些pdf条目不是同一个名称。他们会像“JohnSmith.pdf”一样。我创建了一种基本上将名称转换为pdf的方法,以呈现适当的pdf。在我的方法中,我基本上硬编码的值NSMutableArray中元素的字符串转换

NSUInteger loopCount = 0; 
for (; loopCount < [array count]; loopCount++) { 
    if ([[array objectAtIndex:loopCount] isEqualToString:@"John Smith"]) { 
     [array replaceObjectAtIndex:loopCount withObject:@"JohnSmith.pdf"]; 
    } 
} 

有没有更好的方法来做到这一点?这是我所能想到的,因为数据已经有了不同的名字。谢谢。

+0

是基于第一/姓氏一致的格式文件名? – csano 2011-05-31 17:58:27

回答

6

也许你可以使用这样的事情:

NSString *filename = [[name stringByReplacingOccurrencesOfString:@" " withString:@""] stringByAppendingPathExtension:@"pdf"]; 
1
NSUInteger loopCount = 0; 
for (; loopCount < [array count]; loopCount++) { 
    NSString* name = [array objectAtIndex:loopCount]; 
    [array replaceObjectAtIndex:loopCount withObject:[NSString [email protected]"%@.pdf", [name stringByReplacingOccurrencesOfString:@" " withString:@""]]]; 
} 
+0

看起来你忘了在那里声明并初始化'loopCount'。 – 2011-05-31 18:37:52

+0

谢谢,我已编辑它。无论如何,它的意图是基于代码的问题的代码片段... – sergio 2011-05-31 18:42:11

+0

哎呀,我想我错过了。抱歉。 – 2011-05-31 18:42:53