开发者

String conversion for elements in NSMutableArray

开发者 https://www.devze.com 2023-03-09 19:03 出处:网络
So I have one NSArray that has names of documents to be presented in a UITableView.The NSStrings in the NSArray have spaces so an entry would look like, \"John Smith\".Then I have pdfs that correspond

So I have one NSArray that has names of documents to be presented in a UITableView. The NSStrings 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:@""]]];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消