serialization - cannot create a file for writing - iOS -
so have encoded bunch of objects writing, i'm having trouble file management. can't seem open existing file, or create new file.
i've tried creating empty file of same name, or creating new file. doesn't seem respond (or @ matter).
edit i've followed of advice here, , i'm getting bad access crash.
here's path:
nsstring *documentsdirectory = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; filepath = [documentsdirectory stringbyappendingpathcomponent:@"/bucketlistdata.dat"];
and here's modified method.
-(void) writefile { nsdata *encodedobject; encodedobject = [nskeyedarchiver archiveddatawithrootobject: bucketitems]; nsfilehandle *file; file = [nsfilehandle filehandleforreadingatpath:filepath]; if(file == nil) { nslog(@"failed open file handle writing. attempting create file."); [[nsfilemanager defaultmanager] createfileatpath:filepath contents:nil attributes:nil]; file = [nsfilehandle filehandleforreadingatpath:filepath]; if(file == nil) { nslog(@"unable create new file."); } } [file writedata: encodedobject]; [file closefile]; }
you don't have permissions write root, can access sandbox, and creating read-only handle. try way:
nsstring *docdir = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring *filepath = [docdir stringbyappendingpathcomponent:@"bucketlistdata.dat"]; nsfilehandle *file = [nsfilehandle filehandleforwritingatpath:filepath]; if (!file) { nslog(@"attempting create file"); if (![[nsfilemanager defaultmanager] createfileatpath:filepath contents:nil attributes:nil]) { nslog(@"failed create file"); } else { file = [nsfilehandle filehandleforwritingatpath:filepath]; } } nslog(@"file handle: %@", file);
Comments
Post a Comment