Question

I am creating an iPhone application which makes a iphone act as a pendrive for easy file sharing.

In the first stage, i have some files(png, pdf, jpg, zip) in a directory and i made them display in the tableview in the form of mutable array. It displays in the tableView as shown below,

.DS_Store
.localized
gazelle.pdf
Hamburger_sandwich.jpg
IITD TAJ Picture 028_jpg.jpg
iya_logo_final_b&w.jpg
manifesto09-eng.pdf
RSSReader.sql
SimpleURLConnections.zip
SQLTutorial 

I just want to display the name of the files and i do not want to display the extensions. I know that it is possible to extract the extensions of a file in NSFileManager. But i do not know how. Please help me to make my table view look like this

.DS_Store
.localized
gazelle
Hamburger_sandwich
IITD TAJ Picture 028_jpg
iya_logo_final_b&w
manifesto09-eng
RSSReader
SimpleURLConnections
SQLTutorial 
Was it helpful?

Solution

Look into NSString's documentation, there you'll find stringByDeletingPathExtension.

The basic idea is that you do not want to update objects in existing array, but create a new array with filenames without extension, and throw the original array away.

This is how it can be done:

NSArray *stuff = [NSArray arrayWithObjects:@"image1.jpg", @"image2.png", @"image3.tiff", nil];
NSLog(@"input %@", stuff);

stuff = [stuff valueForKey:@"stringByDeletingPathExtension"];
NSLog(@"output %@", stuff);

I do not expect you to understand that code, therefore here's a more straightforward version:

NSArray *stuff = [NSArray arrayWithObjects:@"image1.jpg", @"image2.png", @"image3.tiff", nil];
NSLog(@"input %@", stuff);

NSMutableArray *output = [NSMutableArray array];
for (NSString *filename in stuff) {
    NSString *filenameWithoutExtension = [filename stringByDeletingPathExtension];
    [output addObject:filenameWithoutExtension];
}
NSLog(@"output %@", output);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top