tags:

views:

473

answers:

1

Hi all, What is the datatype you use to fetch items whose type is dictionary in plist i.e. nsmutabledictionary or nsdictionary? Because I'm using following code to retrieve dictionary objects from an array of dictionaries in plist.

NSMutableDictionary *_myDict = [contentArray objectAtIndex:0]; //APP CRASHES HERE

NSLog(@"MYDICT : %@",_myDict);
NSString *myKey = (NSString *)[_myDict valueForKey:@"Contents"] ; 

[[cell lblFeed] setText:[NSString stringWithFormat:@"%@",myKey]];

Here, on first line it's showing me objc_msgsend. ContentArray is an nsarray and it's contents are showing 2 objects that are there in plist. In plist they are dictionary objects. Then why this error?

EDIT :

Basically, the contents of my contentArray in console are as shown below :

CONTENT ARRAY :  
(
    {
    favourites = 0;
    id = 0;
    story = "This is my first record";
    timestamp = 324567;
},
    {
    favourites = 0;
    id = 1;
    story = "This is my second record";
    timestamp = 321456;
}
)

I want to retrieve these dictionary objects from content array.

Can anybody please help?

This' really urgent.

Thanx in advance.

+2  A: 

NSDictionary. You can't simply say

NSMutableDictionary *_myDict = [contentArray objectAtIndex:0]; 

and hope, that it's a mutable dictionary now. It's still a normal immutable distionary. So, you should write something like:

NSMutableDictionary *_myDict = [NSMutableDictionary dictionaryWithDictionary:[contentArray objectAtIndex:0]];

That'll create mutable dictionary from one that is in the plist.

You can read about it in the "Property List Programming Guide", http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/PropertyLists/index.html

Update:

Also you have a strange plist contents. Available xml-plist types are mentioned here: http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html#//apple_ref/doc/uid/10000048i-CH3-SW1

And overall xml-plist structure is described here: http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/PropertyLists/UnderstandXMLPlist/UnderstandXMLPlist.html#//apple_ref/doc/uid/10000048i-CH6-SW1

Working piece of code

void test() {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSMutableArray *arrayIWillWrite = [NSMutableArray array];
    NSMutableDictionary *dictionary;

    dictionary = [NSMutableDictionary dictionary];
    [dictionary setObject:[NSNumber numberWithInt:0] forKey:@"favourites"];
    [dictionary setObject:[NSNumber numberWithInt:0] forKey:@"id"];
    [dictionary setObject:@"This is my first record" forKey:@"story"];
    [dictionary setObject:[NSNumber numberWithInt:324567] forKey:@"timestamp"];
    [arrayIWillWrite addObject:dictionary];

    dictionary = [NSMutableDictionary dictionary];
    [dictionary setObject:[NSNumber numberWithInt:0] forKey:@"favourites"];
    [dictionary setObject:[NSNumber numberWithInt:1] forKey:@"id"];
    [dictionary setObject:@"This is my second record" forKey:@"story"];
    [dictionary setObject:[NSNumber numberWithInt:321456] forKey:@"timestamp"];
    [arrayIWillWrite addObject:dictionary];

    [arrayIWillWrite writeToFile:@"/Users/alex/test.plist" atomically:NO];

    NSArray *arrayThatWasRead = [NSArray arrayWithContentsOfFile:@"/Users/alex/test.plist"];
    NSLog(@"%@", arrayThatWasRead);

    NSDictionary *dictionaryFromArrayThatWasRead = [arrayThatWasRead objectAtIndex:0];
    NSLog(@"%@", dictionaryFromArrayThatWasRead);

    [pool release];
}
Alexander Babaev
Alexander Babaev, I tried doing what you have suggested, but with no success, It's still showing me objc_msgsend at the same line. I want to fetch the first dictionary object from nsarray.
neha
Can you show exact message that it shows you? What message is wrong and what object is it sent to?
Alexander Babaev
It shows me objec_msgsend. Nothing in console. Here I'm creating an array of nsdictionaries. Is that ok? Or it should be a dictionary of arrays?
neha
Look at the documents in the answer update.
Alexander Babaev
Please check my edited question.
neha
You can look through the piece of code I've inserted in an answer. Just change path to whatever you want (it'll work like this in a Simulator, to make it work on an iPhone you'll have to use documents folder). There is an example of writing plist with an array down and reading from it.
Alexander Babaev
Thanks Alexander Babaev...
neha