objective c - NSFilemanager doesn't create file -
i have problem nsfilemanager, because can store file application documents directory, want create file sub directory don't think why, couldn't create. code below:
+(nsstring *)applicationdocumentsdirectory { return [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) lastobject]; } +(bool)storefile:(nsdata*)file withname:(nsstring*)name atdirectory:(nsstring*)dir{ nsfilemanager *filemgr; nsstring *docsdir; nsstring *newdir; bool create=no; filemgr =[nsfilemanager defaultmanager]; docsdir = [storagemanager applicationdocumentsdirectory]; newdir = [docsdir stringbyappendingpathcomponent:dir]; if(![filemgr fileexistsatpath:newdir]){ if([filemgr createdirectoryatpath:newdir withintermediatedirectories:no attributes:nil error:nil]){ create=yes; } }else create=yes; if(create){ if(![filemgr createfileatpath:newdir contents:file attributes:nil]){ [filemgr release]; return yes; } } [filemgr release]; return no; }
i not sure why file not created. @ first glance, looks code should work. may overlooking something. depends on passing arguments storefile:withname:atdirectory: method.
nevertheless posting answer because did spot error in code: should not send release filemgr since did not send retain first , did not create object. in case, there no need send retain either, since using locally within method. may want review apple developer connection document "cocoa core competencies: memory management".
i don't think error explains why file not created; though i'm surprised application doesn't crash because of it.
Comments
Post a Comment