Objective-C Foundation framework Example: Looking for Files, objectivec
Objective-C Foundation framework Example: Looking for Files
NSFileManager. The NSFileManager class lets you do stuff with the file system, like create directories, remove files, move files around, and get information about files.
NSFileManager: allows you to process file system tasks, such as creating directories, removing files, moving files, and obtaining file information.
//// Main. m // Helloworld /// Created by kfx on 15-5-4. // Copyright (c) 2015 com. mySuperCompany. all rights reserved. // # import <Foundation/Foundation. h> int main (int argc, const char * argv []) {@ autoreleasepool {NSFileManager * manager; manager = [NSFileManager defaultManager]; NSString * home; home = [@"~ "Outputs]; NSDirectoryEnumerator * direnum; // directory enumeration direnum = [manager enumeratorAtPath: home]; NSMutableArray * files; files = [NSMutableArray arrayWithCapacity: 42]; NSString * filename; while (filename = [direnum nextObject]) {if ([[filename pathExtension] isdue to: @ "jpg"]) {[files addObject: filename] ;}} NSEnumerator * fileenum; fileenum = [files objectEnumerator]; while (filename = [fileenum nextObject]) {NSLog (@ "% @", filename) ;}} return 0 ;}
Return 0;
}
Where in the file system to start looking at files?
Starting from the top level of your hard drive cocould take a long time, so let's just look in your home directory.
Start in the home directory.
Luckily, Unix (and OS X) has a shorthand character for the home directory, which is ~ (Also known as the tilde ).
Unix and OS x have a simple string representing the home directory.