ios - Adding Array to PLIST -
i trying add , array root array in plist:
and not working. here's code:
-(ibaction)addname:(id)sender{ nsarray *arrayvalues = [nsarray arraywithobjects: namelabel.text, namedate.text, namevalue.text, nil]; nsstring *plistpath = [[nsbundle mainbundle] pathforresource:@"names" oftype:@"plist"]; nsmutablearray *namesnew = [[nsmutablearray alloc] initwithcontentsoffile:plistpath]; [namesnew addobject:arrayvalues]; [namesnew writetofile:plistpath atomically:yes]; }
what doing wrong? thanks!
you need move file nsdocumentdirectory. edit plist file.
for example:
moving nsdocumentdirectory:
-(nsdictionary *)copybundletodocuments { nsfilemanager *filemanager = [nsfilemanager defaultmanager]; nsarray *documentpaths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [documentpaths objectatindex:0]; nsstring *documentplistpath = [documentsdirectory stringbyappendingpathcomponent:@"names.plist"]; nsstring *bundlepath = [[nsbundle mainbundle] bundlepath]; nsstring *bundleplistpath = [bundlepath stringbyappendingpathcomponent:@"names.plist"]; //if file exists in documents directory, if([filemanager fileexistsatpath:documentplistpath]) { nsmutabledictionary *documentdict = [nsmutabledictionary dictionarywithcontentsoffile:documentplistpath]; return documentdict; } //if file not exist, create existing plist else { nserror *error; bool success = [filemanager copyitematpath:bundleplistpath topath:documentplistpath error:&error]; if (success) { nsmutabledictionary *documentdict = [nsmutabledictionary dictionarywithcontentsoffile:documentplistpath]; return documentdict; } return nil; } }
then plist:
-(void)plistarray:(nsarray*)array { //get documents directory: nsarray *paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; //getting plist file name: nsstring *plistname = [nsstring stringwithformat:@"%@/names.plist", documentsdirectory]; nsmutablearray *namesnew = [[nsmutablearray alloc] initwithcontentsoffile:plistname]; [namesnew addobject:arrayvalues]; [namesnew writetofile:plistname atomically:yes]; return nil; }
Comments
Post a Comment