简体   繁体   中英

Getting value from NSDictionary to NSMutableArray

I have this NSDictionary:

Categorys =     {
        Category =         (
                        {
                categoryDescription =                 {
                    text = "  Description";
                };
                categoryIconPath =                 {
                    text = "  7.jpg";
                };
                categoryId =                 {
                    text = "  25";
                };
                categoryName =                 {
                    text = "  My Photos";
                };
                categoryStatus =                 {
                    text = "  ON";
                };
                publicPrivate =                 {
                    text = "  Private";
                };
                userId =                 {
                    text = "  2";
                };
            },
.................. 

I want to get the categoryName (like here My Photos) into my NSArray.

How can I do this?

I am unable to get it

I have tried this code from http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/

NSString *category = [[[[xmlDictionary objectForKey:@"Categorys"] objectForKey:@"Category"] objectForKey:@"categoryName"] stringForKey:@"text"];

but it gives error

2012-03-27 15:30:35.320 File[1481:903] -[NSCFArray objectForKey:]: unrecognized selector sent to instance 0x1462c0

In Categories, the object referenced by the Category key is an array, you can tell by the fact that its contents are bracketed by parentheses ( ... ) not braces { ... } .

Looking at your code and breaking it down:

NSString *category = [[[[xmlDictionary objectForKey:@"Categorys"] // This is the outermost dictionary 
                            objectForKey:@"Category"]             // This is an NSArray
                                objectForKey:@"categoryName"] stringForKey:@"text"]; 
                                                  // This breaks NSArray doesn't respond to objectForKey:

Breaking it down a bit, you need:

NSDictionary* categorys = [xmlDictionary objectForKey:@"Categorys"];
NSArray* categoryArray = [categorys objectForKey: @"Category"];
NSDictionary* category = [categoryArray objectAtIndex: i];  // i is the index of the category you want e.g. 0
// etc

The answer is in the error message. One of the objects that you are getting from

NSString *category = [[[[xmlDictionary objectForKey:@"Categorys"] objectForKey:@"Category"] objectForKey:@"categoryName"] stringForKey:@"text"];

Doesn't have the selector objectForKey .

Now you need to break down your code and do some debugging to find out where you are going wrong.

Was getting the same error "[NSCFArray objectForKey:]: unrecognized selector sent to instance"

When I looked at the structure of my plist file that was loaded from the bundle at runtime, it did not seem possible that I was using the wrong selectors. The problem is that when I ran the app, it was not pulling the original plist from the bundle, it was pulling a saved plist from my apps directory. Because the saved plist had an incorrect, and completely different structure than my original plist, I indeed was using the wrong selectors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM