ios - Adding Array to PLIST -


i trying add , array root array in plist:

enter image description here

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

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -