.plist Data can not be Loaded From NSArray to NSDictionary - uitableview

I am fairly new to Objective-C.
I have created on .plist in which all data stored in Response Dictionary.
NSString *myListPath = [[NSBundle mainBundle] pathForResource:#"OffersList" ofType:#"plist"];
dic = [NSDictionary dictionaryWithContentsOfFile:myListPath];
tableData = [dic objectForKey:#"Response"];
Now i have converted that tabledata to Dictionary.
NSDictionary *dict = [tableData objectAtIndex:indexPath.row];
cell.titleLabel.text = [dict objectForKey:#"title"];
cell.nowLabel.text = [dict objectForKey:#"price"];
cell.saveLabel.text = [dict objectForKey:#"rondel"];
Now, Problem is that it only load's first 10 data.
I am also tried to print in log but after 10th data it's values seen as NULL.

Try to make a dump of the whole dic so that you can check exactly what data it contains:
NSLog(#"Content of tableData", [dic description]);
Then double check the log content for the tableData element with the .plist content.

Related

iOS - extracting data from a plist not working

This is a routine exercise. I have done it a number of times in my current project and it has worked fine. I copied the code line for line, same initializations. My plist data goes into a dictionary but then it does not go into its respective arrays in their initializations. I have a method called initArraysPlist
-(void)initArraysPlist{
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"trainerProfile" ofType:#"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict1 = [[NSDictionary alloc] initWithContentsOfFile:path1];
trainerNames = [dict1 objectForKey:#"Names"];
trainerIcons = [dict1 objectForKey:#"Icons"];
trainerFactSheet= [dict1 objectForKey:#"Fact Sheet"];
trainerFocus = [dict1 objectForKey:#"Focus"];
trainerContactInfo= [dict1 objectForKey:#"Contact Info"];
}
Ive done this a few times and it currently works in my code. all the values are correct. Ive checked it many times. when
Please read the comments for the each line.
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"trainerProfile" ofType:#"plist"]; // **check if your plist is actually added in Bundle.If its there move to second line , if not then add plist in bundle.**
NSDictionary *dict1 = [[NSDictionary alloc] initWithContentsOfFile:path1];// **if plist is added in bundle , then check if you are getting value for dict1 . If no then you might be making some mistake in plist structure.**
For more clarifications please post your plist if possible.
Please try this code it may be helpful to you
// Read plist from bundle and get Root Dictionary out of it
NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"]];
// Your dictionary contains an array of dictionary
// Now pull an Array out of it.
NSArray *arrayList = [NSArray arrayWithArray:[dictRoot objectForKey:#"catlist"]];
// Now a loop through Array to fetch single Item from catList which is Dictionary
[arrayList enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
// Fetch Single Item
// Here obj will return a dictionary
NSLog(#"Category name : %#",[obj valueForKey:#"category_name"]);
NSLog(#"Category id : %#",[obj valueForKey:#"cid"]);
}];

Lazy load plist dictionary element(s)

The usual method for loading data from a dictionary contained in a plist is as below:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
NSDictionary *data= [NSDictionary dictionaryWithContentsOfFile:path];
Is there a way to import only the element(s) specified in a key / set of keys, like:
NSDictionary *data= [NSDictionary dictionaryWithContentsOfFile:path forKey:key];
The idea is to perform lazy loading of dictionary contents by key.
So based on my comment above, you could add a class method to the NSDictionary via a category. You could do something like (not tested BTW).
+ (NSDictionary *)dictionaryWithContentsOfFile:(NSString *)path forKeys:(NSArray *)keys
{
NSMutableDictionary *newDictionary = nil;
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
if (dictionary) {
newDictionary = [NSMutableDictionary dictionary];
for (id key in dictionary.allKeys) {
if ([keys containsObject:key]) {
newDictionary[key] = dictionary[key];
}
}
}
return [newDictionary copy];
}
If you did this, you'd see your spike in memory, but it should subside once dictionary is freed.
Alternatively, take a look at YAJL (https://github.com/lloyd/yajl). I've used this when dealing with very large JSON files. This was mainly the stream it in chunks. It is event driven, so you should be able to stream it in and detect the keys you want (hopefully).
please try the below method.
- (void)viewDidLoad
{
NSMutableArray *arry;
arry = [[NSMutableArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Catalog" ofType:#"plist"];
NSDictionary *dic = [menuArray objectAtIndex:indexPath.row];
lblName.text = [dic objectForKey:#"MenuName"];
}

Array of array in Objective-C

i have a NSArray of twitter_timeline using this tutorial http://tutorials.veasoftware.com/2013/09/20/twitter-api-version-1-1-user-timeline-in-ios-7/
I get my timeline using this code on cellForRowAtIndexPath method:
NSdictionary *tweet=_twitter_feed[indexPath.Row];
cell.labeltext.text=tweet[#"text"];
Everything works fine, BUT i want to create another array (NSMutableArray) and insert my _twitter_feed(NSArray) into it and get the same access to twitter_feed.
Something like that:
NSMutableArray *mainarray=[[NSMutableArray alloc]init];
[mainarray addObject:_twitter_feed];
but i don't know how to get text from twitter_array from main array
NSDictionary *tweet=[[mainarray objectAtIndex:0]indexPath.row];//??????
cell.labeltext.text=tweet[#"text"];
This is doesn't work.
If the _twitter_feed is a NSArray, you can simply use [[mainarray objectAtIndex:0] objectAtIndex:indexPath.row] to get it.
Try using NSMutableDictionary: (untested)
NSMutableDictionary *mainDict= [NSMutableDictionary dictionary];
[mainDict setObject: _twitter_feed forKey:[NSString stringWithFormat:#"%#", indexPath.row]];
and retrieve like:
NSdictionary *tweet= [mainDict objectForKey:[NSString stringWithFormat:#"%#", indexPath.row]];
cell.labeltext.text=tweet[#"text"];
Consider the line that is giving you the error:
NSDictionary *tweet = [[mainarray objectAtIndex:0]indexPath.row];
Now you've added the reference stored in _twitter_feed into the your mainarray, so this line is effectively:
NSDictionary *tweet = [_twitter_feed indexPath.row];
Compare this to your working line:
NSDictionary *tweet = _twitter_feed[indexPath.row];
Those two are not the same. Maybe you meant to type:
NSDictionary *tweet = [mainarray objectAtIndex:0][indexPath.row];
You can shorten that to:
NSDictionary *tweet = mainarray[0][indexPath.row];
HTH

iOS Populating UITableView with Data from Plist

I am trying to populate a UTTableView with the contents of my Data.plist file.
I need to load this into an NSMutableArray.
This is how far I came:
(View Did Load):
NSString *PlistPath = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
Array = [[NSMutableArray alloc] initWithContentsOfFile:PlistPath];
[Array addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:#"Testing 1", #"name", nil]];
And of course at CellForRowAtIndexPath:
cell.textLabel.text = [Array objectAtIndex:indexPath.row];
I wrote this in my Data.plist file (picture):
Now when I run the App. My TableView remains empty.
Thanks for your time and help!
Currently your plist's root is set to Dictionary. Change that to array first.
Then use this code to show the data:
cell.textLabel.text = [[Array objectAtIndex:indexPath.row] objectForKey:#"name"];

how to load plist data into array in IOS property list

NSString *path = [[NSBundle mainBundle] pathForResource:#"recipes" ofType:#"plist"]];
NSDictionary *dict = [[NSDictionary alloc] initwithContentOfFile:path];
NSArray *textData=[NSArray new];
textData = [dict objectForkey:#"TableData"];
textData is my array name,
recipes is plist name
after excuting my text data is being empty...
where is the mistake.
The problem is that you converting .plist to dictionary in the wrong way. Check dict property, it should be nil. Code to create NSDictionary from .plist file is listed below:
CFPropertyListRef plist = CFPropertyListCreateFromXMLData(kCFAllocatorDefault,
(__bridge CFDataRef)propertyListData,
kCFPropertyListImmutable, NULL);
NSDictionary *settings = (__bridge NSDictionary *)plist;
Try this:
textData = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"recipes" ofType:#"plist"]];
Anyways, you may can take a look of this tutorial:
http://ios-blog.co.uk/tutorials/how-to-populate-a-uitableview-from-a-plist-property-list/

Resources