ios - How to add a key and string in dictionary? -
my plist file:
nsarray *documentpaths = nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes); nsstring *path = [[documentpaths lastobject] stringbyappendingpathcomponent:@"data.plist"]; dict = [nsdictionary dictionarywithcontentsoffile:path];
loop through array, adding annotations , calculate distance:
ann = [dict objectforkey:@"blue"]; [resultarray addobject:@"blue"]; for(int = 0; < [ann count]; i++) { nsstring *coordinates = [[ann objectatindex:i] objectforkey:@"coordinates"]; double reallatitude = [[[coordinates componentsseparatedbystring:@","] objectatindex:1] doublevalue]; double reallongitude = [[[coordinates componentsseparatedbystring:@","] objectatindex:0] doublevalue]; myannotation *myannotation = [[myannotation alloc] init]; cllocationcoordinate2d thecoordinate; thecoordinate.latitude = reallatitude; thecoordinate.longitude = reallongitude; myannotation.coordinate=cllocationcoordinate2dmake(reallatitude,reallongitude); myannotation.title = [[ann objectatindex:i] objectforkey:@"name"]; myannotation.subtitle = [[ann objectatindex:i] objectforkey:@"address"]; myannotation.icon = [[ann objectatindex:0] objectforkey:@"icon"]; [mapview addannotation:myannotation]; [annotations addobject:myannotation]; cllocation *pinlocation = [[cllocation alloc] initwithlatitude:reallatitude longitude:reallongitude]; cllocation *userlocation = [[cllocation alloc] initwithlatitude:mapview.userlocation.coordinate.latitude longitude:mapview.userlocation.coordinate.longitude]; cllocationdistance distance = [pinlocation distancefromlocation:userlocation]; nslog(@"distance: %4.0f m.", distance); }
my plist structure:
now need add dictionaries
in "blue" array
new key
named "distance" string
distance
you can write these lines inside for() loop :
nsstring *dist = [nsstring stringwithformat:@"%4.0f m.", distance]; nsmutabledictionary *indict = [[nsmutabledictionary alloc] init]; indict = [ann objectatindex:i]; [indict setvalue:dist forkey:@"distance"];
goodluck !!!
Comments
Post a Comment