简体   繁体   中英

String conversion for elements in NSMutableArray

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.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:@""]]];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM