One Quickie


Finding things like user's Document directory. (NSFIleManager->General)
NSFileManager's URLForDirectory is how you can find the location of things like the user's Documents directory. The domain is what domain, like NSDocumentDirectory, NSUserDomainMask will give you the URL to the iOS sandbox for your app's user files. appropriateForURL is is advanced - mainly of use if you're going to be doing an atomic swap of files.

In Swift 4:

        let fm = FileManager()
        do {
            let documentDirectoryURL = try fm.url(for: .documentDirectory, 
                                                  in: .userDomainMask, 
                                                  appropriateFor: nil, 
                                                  create: true)
            // use documentDirectoryURL
        } catch {
            // handle error as you see fit
        }
ObjC:
    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *error;

    NSURL *localDocumentDirectory =
        [fm URLForDirectory: NSDocumentDirectory
            inDomain: NSUserDomainMask
            appropriateForURL: nil
            create: YES
            error: &error];

    if (localDocumentDirectory == nil) {  // never compare error == nil
        NSLog (@"could not url for directory, dude - %@", error);
    }
(looking how to do it with paths? )



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com