objective c - How can I avoid EXC_BAD_ACCESS when checking if directories exist? -
objective c - How can I avoid EXC_BAD_ACCESS when checking if directories exist? -
i'm building app caches images in documents directory of app bundle. in order create sure directories exist, want check see if exist and, if don't, create them @ point @ application starts.
currently, i'm doing in didfinishlaunchingwithoptions:
so:
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { nsarray *directoriestocreate = [[nsarray alloc] initwithobjects: @"dira/dira1", @"dira/dira2", @"dirb/dirb2", @"dirb/dirb2", @"dirc", nil]; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentspath = [paths objectatindex:0]; (nsstring *directorytocreate in directoriestocreate) { nsstring *directorypath = [documentspath stringbyappendingpathcomponent:directorytocreate]; nslog(directorypath); if (![[nsfilemanager defaultmanager] fileexistsatpath:directorypath isdirectory:yes]) { nserror *directorycreateerror = nil; [[nsfilemanager defaultmanager] createdirectoryatpath:directorypath withintermediatedirectories:yes attributes:nil error:&directorycreateerror]; } } [window addsubview:navigationcontroller.view]; [window makekeyandvisible]; homecoming yes; }
on first run of application – when none of directories exist – application runs, directories created expected , runs fine.
when application terminated, , runs again, exc_bad_access signal on fileexistsatpath:
phone call on [nsfilemanager defaultmanager]
.
what don't understand why runs fine when directories don't exist, falls on when exist.
can offer assistance?
you're using check function in wrong way. 2nd parameter must pointer boolean variable filled after function called:
you using function this:
[[nsfilemanager defaultmanager] fileexistsatpath:directorypath isdirectory:yes];
but function should used this:
bool isdir; [[nsfilemanager defaultmanager] fileexistsatpath:directorypath isdirectory:&isdir]; if (isdir) { // file exists , directory.
objective-c ios cocoa-touch exc-bad-access nsfilemanager
Comments
Post a Comment