ios - Check if an array of NSDictionary contains a NSDictionary -
in app i've check if in array of nsdictionary
there nsdictionary
. in other words, made json file , store in documents.
i'm parsing json insert new entry, before insert entry in json need check if entry it's stored in file. parsed json so:
// first read json file folder documents nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *filepath = [documentsdirectory stringbyappendingpathcomponent:@"data.json"]; nsstring *content = [nsstring stringwithcontentsoffile:filepath encoding:nsutf8stringencoding error:null]; nsdata *data = [content datausingencoding:nsutf8stringencoding]; nsdictionary *dictcontent = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:nil]; // json parsing nsmutablearray *array = [[nsmutablearray alloc]init]; nsmutablearray *arrayofdictdata = [[nsmutablearray alloc]init]; if (dictcontent) { nsdictionary *sites = [dictcontent objectforkey:@"sites"]; nsarray *site = [sites objectforkey:@"site"]; array = [site mutablecopy]; }
then i've nsdictionary
(dictforjson
) in inserted data parsed html site, need check if data in dictionary dictforjson
still in json file.
how can check that?
the dictforjson has structure:
dictforjson = @{@"name": htmltitle, @"src": path, @"expirydate": expiredate};
the nsarray site has structure:
site: ( { expirydate = "29 ago 2013"; name = sito4; src = "/users/redoddity/library/application support/iphone simulator/6.1/applications/2d9ebe71-4365-448d-8ad2-a08749b8dbc1/documents/sito4.html"; } )
have idea? tried [array containsobject:dictforjson]
, nsdictionary isn't ordered when use method returns 0 , other times returns 1 maybe it's useful build json following code:
- (void)jsoncreateoredit { nsdictionary *dictsites = [[nsdictionary alloc]init]; nsdictionary *dictsite = [[nsdictionary alloc]init]; nsmutablearray *site = [[nsmutablearray alloc]init]; nsdictionary *dictforjson = [[nsdictionary alloc]init]; dictforjson = @{@"name": htmltitle, @"src": path, @"expirydate": expiredate}; nsstring* documentspath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring *filepath = [documentspath stringbyappendingpathcomponent:@"data.json"]; bool fileexist = [[nsfilemanager defaultmanager] fileexistsatpath:filepath]; if (!fileexist) { [site addobject:dictforjson]; dictsite = @{@"site": site}; dictsites = @{@"sites": dictsite}; // creo il file json nslog(@"il file non esiste"); sbjsonwriter *writer = [[sbjsonwriter alloc]init]; nsstring *jsoncommand = [writer stringwithobject:dictsites]; [createfile createfilein:documentspath with:@"data.json" anddata:jsoncommand]; // class method made create file else { // here there instructions update file data.json nslog(@"il file esiste"); // leggo il file json nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *filepath = [documentsdirectory stringbyappendingpathcomponent:@"data.json"]; nsstring *content = [nsstring stringwithcontentsoffile:filepath encoding:nsutf8stringencoding error:null]; nsdata *data = [content datausingencoding:nsutf8stringencoding]; nsdictionary *dictcontent = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:nil]; // faccio il parsing del json nsmutablearray *array = [[nsmutablearray alloc]init]; nsmutablearray *arrayofdictdata = [[nsmutablearray alloc]init]; if (dictcontent) { nsdictionary *sites = [dictcontent objectforkey:@"sites"]; nsarray *site = [sites objectforkey:@"site"]; array = [site mutablecopy]; // here must check if in array site there same information i'm trying insert } }
nspredicate *apredicate = [nspredicate predicatewithformat:@"%k like[c] %@", @"name", @"htmltitle"]; nsarray *thefilteredarray = [mainarrayofdictionry filteredarrayusingpredicate:apredicate]; if ([thefilteredarray count]){ nslog(@"dictionay exists"); }else{ nslog(@"dictionay not exists"); }
in first statement first parameter "key" , second parameter actual value of dictionay.
i assuming following :
nsdictionary *dictforjson = @{@"name": htmltitle, @"src": path, @"expirydate": expiredate}; nsarray *mainarrayofdictionry = ( "dictforjson" : @{@"name": htmltitle, @"src": path, @"expirydate": expiredate}; ), (), (), (), . . . )
*hope you.*array
Comments
Post a Comment