Objective-C Download PLIST and use within table - ios

I'm fairly new to Objective-C programming. I'm trying to build an app for personal use to view event info which is contained on my web server. I wish for each Event Title string to be shown as a separate row within my UITableView.
I'm using CFPropertyList to create the plist from MySQL. Here's what my plist looks like:
I'm downloading the plist like so:
NSString *stringURL = #"http://www.example.com/events.plist";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"events.plist"];
[urlData writeToFile:filePath atomically:YES];
}
Then this is how I'm trying to load the info into their arrays:
events = [[NSDictionary alloc] initWithContentsOfFile:filePath];
ID = [events objectForKey:#"Event ID"];
title = [events objectForKey:#"Event Title"];
date = [events objectForKey:#"Event Date"];
price = [events objectForKey:#"Price"];
totalTickets = [events objectForKey:#"Total Tickets"];
ticketsSold = [events objectForKey:#"Tickets Sold"];
ticketsRemaining = [events objectForKey:#"Tickets Remaining"];
postStatus = [events objectForKey:#"Post Status"];
postContent = [events objectForKey:#"Post Content"];
Now here's where I'm having trouble. Now when I'm debugging the number of rows the 'events' dictionary has it returns 0.
Therefore I cannot set the 'numberOfRowsInSection'. Nor does [title objectAtIndex:indexPath.row] work.
I believe the issue lays with how I'm setting up the NSDictionary, but due to my lack of Objective-C and array knowledge, I'm unable to overcome this issue.

Here's your problem:
events = [[NSDictionary alloc] initWithContentsOfFile:filePath];
Your plist is an array of dictionaries and you're trying to load that array as a dictionary. Try this instead:
events = [NSArray arrayWithContentsOfFile:filePath];
Edit: To extract the titles into an array so you can use them in table cells, do this:
NSMutableArray *titles = [NSMutableArray array];
for (NSDictionary *event in events) {
NSString *title = [event objectForKey:#"Event Title"];
[titles addObject:title];
}

Related

iOS Pragmatically Create Plist with 15 Array Objects

I hope someone can help, before asking we're using Plist to slimline the whole app. I am creating a plist when somebody selects a player. However what I want to do is create a plist with 15 blank array objects, this way I can just replace and edit as the user goes.
Any help would be appreciated. Thanks!
To create plist with 15 arrays you can use:
NSMutableArray * mainArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 15; i++)
{
NSArray *array = [NSArray alloc] init];
[mainArray addObject:array];
}
[self saveToFile:mainArray];
Method for saving:
- (void)saveToFile:(NSArray*)ar
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"data.plist"];
[ar writeToFile:path atomically:YES];
}
Hope you find it useful.

how to insert a Dictionary into an array

I am quite new to iOS and Objective-c.
I am trying to auto generate a pList in my app that looks like this.
I've so far been able to create the file making it a normal Value => Key file if i replace my for loop by
for (NSString* exercisePictureName in bigPictureData) {
[data setObject:exercisePictureName forKey:exercisePictureName];
}
but my problem is that I have no idea how to structure the logic at the end of my loop to create a file structure like shown in the picture. As it has to be exact.
Could anyone point me in the right direction on how to structure my loop so that it creates the file with the right format????
- (void) createImageListFromSource {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"exercisePictures.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: #"exercisePictures.plist"] ];
}
//To insert the data into the plist
NSArray* bicepPictureData = [self getAllimagesThatStartWith:#"bicep-"];
NSArray* tricepPictureData = [self getAllimagesThatStartWith:#"tricep-"];
NSArray* absPictureData = [self getAllimagesThatStartWith:#"abs-"];
NSArray* chestPictureData = [self getAllimagesThatStartWith:#"chest-"];
NSArray* backPictureData = [self getAllimagesThatStartWith:#"back-"];
NSArray* bigPictureData = [bicepPictureData arrayByAddingObjectsFromArray:tricepPictureData];
bigPictureData = [bigPictureData arrayByAddingObjectsFromArray:absPictureData];
bigPictureData = [bigPictureData arrayByAddingObjectsFromArray:chestPictureData];
bigPictureData = [bigPictureData arrayByAddingObjectsFromArray:backPictureData];
NSArray* finalData = [[NSArray alloc] init];
for (NSString* exercisePictureName in bigPictureData) {
NSDictionary* data = [[NSDictionary alloc] initWithObjectsAndKeys:exercisePictureName,#"text",exercisePictureName,#"image", nil];
[finalData arrayByAddingObject:data];
NSLog(#"%#",data);
}
NSLog(#"%#",finalData);
[finalData writeToFile: path atomically:YES];
}
What you have is an array of dictionaries. Pseudocode to show the structure:
NSMutableArray* arr = [NSMutableArray array];
for (...) {
NSDictionary* d = #{#"image": something, #"text": somethingelse};
[arr addObject:d];
}
When you are all done, just save the array directly with writeToURL....

Plist dicionaries keys

I am using this code to add content into a plist :
//////////// Save data into plist /////////////////
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"datatesting.plist"];
NSLog(#"path='%#'",path);
NSFileManager *nfm = [[NSFileManager alloc] init];
if([nfm fileExistsAtPath:path])
{
// if file exists, get its contents, add more entries and write back
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSArray *keys = [NSArray arrayWithObjects:#"Title",#"Description",#"Coordinate",nil];
[array addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSString stringWithFormat:#"%#",titlestring],[NSString stringWithFormat:#"%#",descriptionstring],[NSString stringWithFormat:#"%#",coordinadastring], nil] forKeys:keys]];
NSLog(#"modified array=%#",array);
BOOL ok = [array writeToFile:path atomically:YES];
if(!ok){
NSLog(#"Unable to write appended file");
return;
}
} else {
// if file doesn't exist, create a new one
NSMutableArray *array = [[NSMutableArray alloc] init];
NSArray *keys = [NSArray arrayWithObjects:#"Title",#"Description",#"Coordinate",nil];
[array addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSString stringWithFormat:#"%#",titlestring],[NSString stringWithFormat:#"%#",descriptionstring],[NSString stringWithFormat:#"%#",coordinadastring], nil] forKeys:keys]];
NSLog(#"new array=%#",array);
BOOL ok = [array writeToFile:path atomically:YES];
if(!ok){
NSLog(#"Unable to write new file");
return;
}
}
Now I am having issues in using the content of the plist. So my two questions are :
- What is the Keys of the dictionaries of the current plist ?
- What is the way to read the content in a Tableview ?
You have an array of dictionaries. You need to iterate on the contents of the array, then look at each dictionary. You can do this with block enumerators or the older for() style. I'll use the latter now as you may not know blocks.
NSArray *array = ...; // read in the array from the file
for(NSDictionary *dict in array) {
NSString *title = [dict objectForKey:#"Title"];
... etc
}
When you want to show this data in a tablview, the number of rows is the number of dictionaries (ie [array count]). If you wanted to show just the title for each cell, you would get the cell row number, then get the dictionary by [array objectAtIndex:cellRow]), then pull out the title as above.

Load data from plist to tableview [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to populate UITableView with plist
I save data to plist like this:
- (IBAction)save:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"data.plist"];
NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistPath];
if (nil == data) {
data = [[NSMutableArray alloc] initWithCapacity:0];
}
else {
[data retain];
}
NSMutableDictionary *array = [[NSMutableDictionary alloc]init];
[array setObject:label1.text forKey:#"Test1"];
[array setObject:label2.text forKey:#"Test2"];
[array setObject:label3.text forKey:#"Test3"];
[array setObject:label4.text forKey:#"Test4"];
NSLog(#"%#", array);
[data addObject:array];
[array release];
[data writeToFile:plistPath atomically: TRUE];
[data release];
}
NSLog:
2012-07-01 18:52:19.566 testapp[22651:707] {
Test1 = 40;
Test2 = 102;
Test3 = 153;
Test4 = 255;
}
How can I load saved data to uitableview?
To read it, do this:
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
You should then be able to read the values for all the keys like this:
NSString *myValue = [plistDict objectForKey:#"someKey"];
edit:
After rereading your code it seems like you might be doing something you don't want to do. Your current plist structure is a top level array that adds a dictionary to itself every time you invoke "save". Is this what you're trying to do? If so, what represents a cell in your table, an index in the array?
If so, then you want to do
// top level array
NSMutableDictionary* plistArray = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
// individual dictionary at one array index (such as indexPath.row in cellForRowAtIndexPath)
NSMutableDictionary *dictionary = [plistArray objectAtIndex:yourIndex];
In any case, the object you have named "array" is actually a dictionary so you should rename it (or change it to be an array if that's what you want).

Can I read just the key from plist?

Can I read just the key from plist without its value, also if I know the value can I read the key ?
Reading .plist:
NSString *path = [[NSBundle mainBundle] pathForResource:#"myPlist" ofType:#"plist"];
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
Getting all keys and all values:
NSArray* allmyKeys = [myDictionary allKeys];
NSArray* allmyValues= [myDictionary allValues];
Getting all keys for an values object:
NSArray* allmyKeys = [myDictionary allKeysForObject:myValueObject];
As an alternative, you can use allKeysForObject: method which returns,
A new array containing the keys corresponding to all occurrences of anObject in the dictionary. If no object matching anObject is found, returns an empty array.
From that array you can get the key by invoking the objectAtIndex: method.
Use -[NSDictionary allKeysForObject:]*.
Example
NSArray *keys = [myDict allKeysForObject:#"My Value"];
if ([keys count] != 0) { // to prevent out-of-bounds crashes
NSString *key = [keys objectAtIndex:0];
return key;
} else {
return nil;
}
*Dunno why it returns an NSArray object instead of an NSSet object, because keys are not ordered. Oh well.
In order to read the values at app's installed folder:
NSString *PListName=#"ExamplePlist";
NSString *_PlistNameWithExtension=[NSString stringWithFormat:#"%#.plist",PlistName];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:_PlistNameWithExtension]; //3
NSDictionary *myDictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
NSLog(#"%#",[myDictionary description]);
NSArray *AllKeys=[myDictionary allKeys];
Jhaliya's method has not worked for me, then I tried this method.

Resources