So I have one NSArray
that has names of documents to be presented in a UITableView
. The NSString
s in the NSArray
have spaces so an entry would look like, "John Smith". Then I have pdfs that correspond to each of the table entries. These pdf entries are not the same name. They would be something like, "JohnSmith开发者_Go百科.pdf". I created a method to basically convert the names to the pdfs in order to present the appropriate pdfs. In my method, I basically hard coded the values
NSUInteger loopCount = 0;
for ( ; loopCount < [array count]; loopCount++) {
if ([[array objectAtIndex:loopCount] isEqualToString:@"John Smith"]) {
[array replaceObjectAtIndex:loopCount withObject:@"JohnSmith.pdf"];
}
}
Is there a better way to do this? That's all I could think of since the data was already made to have different names. Thx.
maybe you can use something like this:
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:@""]]];
}
精彩评论