xcode - Writing and reading text files on the iPhone -
as practice, trying write app similar built-in notes app.
cannot figure out how save file , display in uitableview
.
right now, have uitextview
user can type in. have save button.
when user taps save button, want save it, , later have displayed in table view.
lost if know of relevant tutorials etc. appreciated.
as noted commenters in real world, you're going want @ core data or other data persistence strategy. if you're dead set on pursuing learning experience, should solve problem:
- (void)writestringtofile:(nsstring*)astring { // build path, , create if needed. nsstring* filepath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring* filename = @"mytextfile.txt"; nsstring* fileatpath = [filepath stringbyappendingpathcomponent:filename]; if (![[nsfilemanager defaultmanager] fileexistsatpath:fileatpath]) { [[nsfilemanager defaultmanager] createfileatpath:fileatpath contents:nil attributes:nil]; } // main act... [[astring datausingencoding:nsutf8stringencoding] writetofile:fileatpath atomically:no]; } - (nsstring*)readstringfromfile { // build path... nsstring* filepath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring* filename = @"mytextfile.txt"; nsstring* fileatpath = [filepath stringbyappendingpathcomponent:filename]; // main act... return [[[nsstring alloc] initwithdata:[nsdata datawithcontentsoffile:fileatpath] encoding:nsutf8stringencoding] autorelease]; }
Comments
Post a Comment