Monday, April 04, 2011

Xcode snippet #1: Reading the contents of a directory

Below you will find a code snippet which reads the contents of a specific directory (in this case the documents directory) from your device. It looks for file with has a suffix .list, stripes the filename from it with the substringToIndex method and adds that name to another array.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; 
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:nil];
for (NSString *dir in dirContents) {
  if ([dir hasSuffix:@".list"]) {
      NSRange range = [dir rangeOfString:@"."];
      NSString *name = [dir substringToIndex:range.location];
      if (![name isEqualToString:@""]) {
          MyListItem *listItem = [[MyListItem alloc] init];
          listItem.name = name;
          [lists addObject:listItem];
          [listItem release];
      }
  }
} 

2 comments:

http://essay4money.com/ said...

I know already what directory to apply your guidance to! As always, It proved out to be an extreme help!

Anonymous said...

I cannot thank Mr Benjamin service enough and letting people know how grateful I am for all the assistance that you and your team staff have provided and I look forward to recommending friends and family should they need financial advice or assistance @ 1,9% Rate for Business Loan .Via Contact : .  lfdsloans@outlook.com. WhatsApp...+ 19893943740. Keep up the great work.
Thanks, Busarakham.

Use an image as your UIBarButtonItem

Using an image as your UIBarButtonItem in your navigationcontroller bar can only be achieved by using a common UIButton as the BarButtonItem...