objective c - How to save text/data in cocoa/xcode to a file? -


i'm sorry obvious question, there should careful when writing data file, because program can read data wrote, while program running, moment stop it, files become empty

tried using nsfilehandle, in order write data it, , close file later, didn't help... currently, i'm using:

nsdata *encodedobject = [nskeyedarchiver archiveddatawithrootobject:newarray]; [encodedobject writetofile:string atomically:yes]; 

and no matter do, can't simplest nsstring stay in file permanently

what do?

thanks @letzflow, didn't solve yet.

now, i'm using:

nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; string = [nsstring stringwithformat:@"%d.rtf",filename]; nsstring *file = [documentsdirectory stringbyappendingpathcomponent:string]; //nsfilehandle *filehandle = [nsfilehandle filehandleforwritingatpath:file]; nsdata *encodedobject = [nskeyedarchiver archiveddatawithrootobject:newarray]; [encodedobject writetofile:file atomically:yes]; //[filehandle writedata:encodedobject]; //[filehandle closefile]; nslog(@"%@",[nsstring stringwithcontentsoffile:file encoding:nsstringencodingconversionallowlossy error:nil]); 

to serialize array of objects, , nslog shows valid array. yet, when @ file later, or try unserialize array (like this):

nsstring *stringx = [nsstring stringwithformat:@"%d.rtf",filename]; nsdata *encodedobjectx = [nsdata datawithcontentsoffile:stringx]; nsarray *newarrayx = [nskeyedunarchiver unarchiveobjectwithdata:encodedobjectx]; turnigbutton *button = [[newarrayx objectatindex:1] objectatindex:5]; nslog(@"%d", button.idnum); 

it prints (null). (when execute 1 after other in single run, unserializes fine)

i appreciate help.

the question write file? because not allowed write files on system in ios.

nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; 

the documents directory place can write files, might want give try.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -