I would like to access coordinate values which are latitude and longitude.
NSString *path = [[NSBundle mainBundle] pathForResource:
#"WellList" ofType:#"plist"];
NSMutableArray *cities = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSDictionary *routes =[[NSDictionary alloc]initWithContentsOfFile:path];
NSDictionary *city =[[NSDictionary alloc]init];
NSDictionary *locations =[[NSDictionary alloc]init];
NSDictionary *locationInfo=[[NSDictionary alloc]init];
NSMutableArray *localCities= [[NSMutableArray alloc]init];
city= routes [#"Routes"];
NSLog (#"%#",city);
localCities =[[NSMutableArray alloc] initWithArray:[city allValues]];
locations =[localCities objectAtIndex:1];
I think your problem is that you don't understand how loading a .plist file works. Routes and Cities are being initialized to the same value, because you are loading both of them from the same file. So to access the latitude of Houston's Location 1, you would write
NSMutableDictionary *root = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSString* latitude = [[[[[root objectForKey: #"Routes"] objectForKey: #"Houston" ] objectForKey: #"Location 1"] objectForKey: #"coordinate"] objectForKey: #"latitude"];
Related
Hi i am trying to create a dictionary (socialDict) inside a dictionary (dict) in ios at specified location (ios path) but it showing error i.e The data couldn’t be read because it isn’t in the correct format.
Code Snippet:
NSArray *arrayDirctories=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask,YES);
NSString *libraryPath=[arrayDirctories objectAtIndex:0];
NSString *filePath = [libraryPath stringByAppendingPathComponent:#"Hello/breaches.plist"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *socialDict = [[NSMutableDictionary alloc] init];
[socialDict setObject:arr forKey:#"count"];
[socialDict setObject:sitesArr forKey:#"sites"];
[socialDict setObject:timeArr forKey:#"time"];
[dict setObject:socialDict forKey:#"social"];
[dict writeToFile:filePath atomically:1];
Please can anyone find me way out through this. Thanks in advance.
but it worked for me..
NSArray*arr=[[NSArray alloc]initWithObjects:#"1",#"2",#"3", nil];
NSArray*sitesArr=[[NSArray alloc]initWithObjects:#"1",#"2",#"3", nil];
NSArray*timeArr=[[NSArray alloc]initWithObjects:#"1",#"2",#"3", nil];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *socialDict = [[NSMutableDictionary alloc] init];
[socialDict setObject:arr forKey:#"count"];
[socialDict setObject:sitesArr forKey:#"sites"];
[socialDict setObject:timeArr forKey:#"time"];
[dict setObject:socialDict forKey:#"social"];
// [dict setValuesForKeysWithDictionary:socialDict];
[dict writeToFile:filePath atomically:1];
the .plist file is
NSString *filePath = [libraryPath stringByAppendingPathComponent:#"file.plist"];
NSArray*arr1=[[NSArray alloc]initWithObjects:#"1",#"2",#"3", nil];
NSArray*arr2=[[NSArray alloc]initWithObjects:#"1",#"2",#"3", nil];
NSArray*arr3=[[NSArray alloc]initWithObjects:#"1",#"2",#"3", nil];
NSMutableDictionary *mainDict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *subDict = [[NSMutableDictionary alloc] init];
[subDict setObject:arr forKey:#"count"];
[subDict setObject:sitesArr forKey:#"arr1"];
[subDict setObject:timeArr forKey:#"arr2"];
[mainDict setObject:subDict forKey:#"arr3"];
[mainDict writeToFile:filePath atomically:1];
It is working fine
I am trying to get the child element SHOP from a plist. The plist is as so :
Settings is a section.
I tried using the following but not sure how to put it in a cellForIndexPath
NSDictionary *region = [self.dict valueForKey:#"Settings"];
NSString *name = [region valueForKey:#"Shop"];
NSLog(#"%#", name);
My Plist is as so:
NSString *path = [[NSBundle mainBundle] pathForResource:#"PropertyList" ofType:#"plist"];
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.dict = tempDict;
NSArray *tempArray = [[NSArray alloc] init];
self.namesArray = tempArray;
self.sectionArray = [self.dict allKeys];
If what you need is to read the "Shop" array from the "item 0" dictionary you need to do the following:
NSString *path = [[NSBundle mainBundle] pathForResource:#"PropertyList" ofType:#"plist"];
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.dict = tempDict;
NSArray *settingsArray = self.dict[#"Settings"];
NSDictionary *itemDict = settingsArray[0];
NSArray *shopArray = itemDict[#"Shop"];
Of course you should check that the keys and indexes you're using are valid before indexing to avoid crashes.
I am having trouble reading the data in a plist file. I'm not getting the titleString printed in the console like I'm expecting. What am I doing wrong?
NSString *path = [[NSBundle mainBundle] pathForResource:#"Events" ofType:#"plist"];
NSDictionary *dictPri = [[NSMutableDictionary alloc]initWithContentsOfFile:path];
NSMutableArray *arrEvents = [[NSMutableArray alloc] initWithArray:[dictPri valueForKey:#"Root"]];
for (NSDictionary *dict in arrEvents)
{
NSString *titleString = nil;
NSString *date = nil;
titleString = [NSString stringWithFormat:#"%#",[dict valueForKey:#"Title"]];
date = [NSString stringWithFormat:#"%#",[dict valueForKey:#"Date"]];
NSLog(#"Title String: %#", titleString);
}
Your main element (Root) is Dictionary - not Array - change it in plist by clicking on type next to it.
Also there is a problem in your code - you never access "Root" element by name - it's by default top-level object. Consider taking out additional array initialization which is not required.
Fixed code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Events" ofType:#"plist"];
NSArray* arrEvents = [NSArray arrayWithContentsOfFile:path];
for (NSDictionary *dict in arrEvents)
{
NSString *titleString = [NSString stringWithFormat:#"%#",[dict objectForKey:#"Title"]];
NSString *date = [NSString stringWithFormat:#"%#",[dict objectForKey:#"Date"]];
NSLog(#"Title String: %#", titleString);
}
I have a function meant for an iOS app. The NSString valueand key contain the value and key however it doesnt seem to get assign to the NSMutableDictionary data on the line [data setValue:value forKey:key]
-(NSMutableDictionary *)parseCode:(NSString *)string{
NSArray *array = [string componentsSeparatedByString:#"||"];
NSMutableDictionary *data;
for(id object in array){
NSArray *objectAndKey = [object componentsSeparatedByString:#"::"];
NSString *key =[objectAndKey objectAtIndex:0];
NSString *value = [objectAndKey objectAtIndex:1];
[data setValue:value forKey:key];
[self alertMeWithString:[data valueForKey:key]];
}
return data;
}
You need to initialise and allocate your NSMutableDictionary.
NSMutableDictionary *data = [NSMutableDictionary dictionary];
Or you can explicitly make the calls using
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
I am trying to save in my iOS app some data. I use the following code :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"yourPlist.plist"];
//inserting data
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:categoryField.text forKey:#"Category"];
[dict setValue:nameField.text forKey:#"Name"];
[dict setValue:eventField.text forKey:#"Event"];
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:dict];
[arr writeToFile: path atomically:YES];
//retrieving data
NSMutableArray *savedStock = [[NSMutableArray alloc] initWithContentsOfFile: path];
for (NSDictionary *dict in savedStock) {
NSLog(#"my Note : %#",dict);
}
However the NSLog shows me only the last data... I suppose that i am overwriting here..I cant see why though!
How can i continue to save dictionaries in the array without overwriting? Any ideas?
Since you are making a model object it would be better if you include save, remove, findAll, findByUniqueId kind of logic built into it. Will make working with the model object very simple.
#interface Note : NSObject
#property (nonatomic, copy) NSString *category;
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy) NSString *event;
- (id)initWithDictionary:(NSDictionary *)dictionary;
/*Find all saved notes*/
+ (NSArray *)savedNotes;
/*Saved current note*/
- (void)save;
/*Removes note from plist*/
- (void)remove;
Save a note
Note *note = [Note new];
note.category = ...
note.name = ...
note.event = ...
[note save];
Delete from saved list
//Find the reference to the note you want to delete
Note *note = self.savedNotes[index];
[note remove];
Find all saved notes
NSArray *savedNotes = [Note savedNotes];
Source Code
Replace:
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:dict];
[arr writeToFile: path atomically:YES];
With:
NSMutableArray *arr = [[NSMutableArray alloc] initWithContentsOfFile: path];
[arr addObject:dict];
[arr writeToFile: path atomically:YES];
You need to first read in the data, then APPEND the new dictionary to the old one. So read the file first, then append the new dictionary, then save.
FULL CODE:
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:categoryField.text forKey:#"Category"];
[dict setValue:nameField.text forKey:#"Name"];
[dict setValue:eventField.text forKey:#"Event"];
[self writeDictionary:dict];
- (void)writeDictionary:(NSDictionary *)dict
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"yourPlist.plist"];
NSMutableArray *savedStock = [[NSMutableArray alloc] initWithContentsOfFile: path];
if(!savedStock) {
savedStock = [[[NSMutableArray alloc] initiWithCapacity:1];
}
[savedStock addObject:dict];
// see what';s there now
for (NSDictionary *dict in savedStock) {
NSLog(#"my Note : %#",dict);
}
// now save out
[savedStock writeToFile:path atomically:YES];
}