ios - Write to plist file (In Particular row/Item) -
i have .plist
file have structure,
i want add or replace item 5. using code
nserror *error ; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *path = [documentsdirectory stringbyappendingpathcomponent:@"introduction.plist"]; nsmutablearray *data = [[nsmutablearray alloc] initwithcontentsoffile:path]; nsstring *comment = str; [data replaceobjectatindex:1 withobject:comment]; [data writetofile:path atomically:yes]; nsfilemanager *filemanager = [nsfilemanager defaultmanager]; if (![filemanager fileexistsatpath:path]) { nsstring *bundle = [[nsbundle mainbundle] pathforresource:@"introduction" oftype:@"plist"]; [filemanager copyitematpath:bundle topath:path error:&error]; }
if change replaceobjectatindex:
5
changes plist structure, , don't want that.
how can insert/replace text @ row 5 (item 5) of particular index?
you structure has array of arrays.
[data replaceobjectatindex:1 withobject:comment];
by code replacing array @ index 1 string. need insert 5th index or need add existing sub array?
nsmutablearray *subarray = [data[sectionindex] mutablecopy]; if (!subarray) { subarray = [nsmutablearray array]; } if (rowindex<[subarray count]) { [subarray replaceobjectatindex:rowindex withobject:comment]; }else{ [subarray addobject:comment]; } [data replaceobjectatindex:sectionindex withobject:subarray]; [data writetofile:path atomically:no];
access subarray, make mutable , add or insert @ specific index. if trying insert don't forget include check if index not greater count of array.
Comments
Post a Comment