开发者

iOS - using a base name for files and appending the extension

I have lots of image file names stored in an array in my RootViewController.m that the user can select from.

myImages = [[NSMutableArray alloc] init];
[myImages addObject:@"MR-MONO2-12-shoulder"];
[myImages addObject:@"brain_01"];
[myImages addObject:@"brain_02"];
[myImages addObject:@"brain_03"];
[myImages addObject:@"brain_04"]; ...

Once selected, a thumbnail image and the corresponding text file should be displayed in the detailView. I've named all the corresponding image files and text files similarly so, for example, file1.jpg corresponds to file1.txt. I want to make the base name of the file is a string on its own, to which I can append the appropriate extension when needed.

I'm pretty sure I'm overcom开发者_如何学Cplicating this a lot, but with the numerous image and text files I don't know how to write this so that the base name used is dependent upon the user's selection from the table. Is it possible to use the array I created to store all my images? The elements in the array are what I would make the base names for the files.


You have a couple of options. If you just want to append a path extension, you can use NSString's instance method - (NSString *)stringByAppendingPathExtension:(NSString *)ext. If the file you're looking for is in a bundle you can use NSBundle's instance methods like - (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension or - (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension inDirectory:(NSString *)subpath if the resource is in a folder reference somewhere underneath the top-level bundle directory.

If you want to add more files later without needing to modify your code, you might consider loading the resource names from an array in a property list (.plist) resource using NSDictionary's - (id)initWithContentsOfFile:(NSString *)aPath.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜