iphone - plist as persistent storage for storing and retrieving data between closing and restarting app -
i trying save few property plist when user starts app first time, , have retrieve data , show user, when user restart app, don't have ask same data again.
i guessing should straight forward, may missing something.
it nice if 1 can point me sample code doing same thing, nice if code saves , retrieves data dictionary plist , plist dictionary.
i looked apple property list programming guide , used code reading , writing property list data
but, when restart app, don't data have stored.
*edit*
i adding code 1 can point out if making mistake.
i created plist file "data.plist" in project resources folder.
this how save data in 1 of view controller
nsstring *path = [[nsbundle mainbundle] bundlepath]; nsstring *finalpath = [path stringbyappendingpathcomponent:@"data.plist"]; //nsstring *error; nsmutabledictionary *dict = [nsmutabledictionary dictionary]; [dict setobject:@"xxxxxx" forkey:@"phonenumber"]; [dict setobject:@"password" forkey:@"password"]; [dict setobject:@"email" forkey:@"email"]; [dict writetofile:finalpath atomically:no];
this how reading the data on same view controller viewdidload method, because want load data when next time app starts.
nsdictionary *dictionary; // read "data.plist" application bundle nsstring *path = [[nsbundle mainbundle] bundlepath]; nsstring *finalpath = [path stringbyappendingpathcomponent:@"data.plist"]; dictionary = [nsdictionary dictionarywithcontentsoffile:finalpath]; // dump contents of dictionary console (id key in dictionary) { nslog(@"bundle: key=%@, value=%@", key, [dictionary objectforkey:key]); }
before saving file can "po dict" @ gdb , can see there data in file
nslog print following error
assertion failed: (cls), function getname, file /sourcecache/objc4_sim/objc4-427.1.1/runtime/objc-runtime-new.m, line 3939.
because when load data , "po dictionary" @ gdb empty
-[nsdictionary writetofile:atomically:]
returns yes
or no
depending on whether it's successful or not. checking return value?
if had take guess, i'd it's because you're writing file application bundle. should write elsewhere in file system; ~/library/application support/<application name>
usual location.
edit:
you can check return value like:
bool success = [dict writetofile:finalpath atomically:yes]; if (!success) { nslog(@"could not write file"); }
Comments
Post a Comment