NSArray cannot be cast to NSDictionary

110 views
Skip to first unread message

Jorisk

unread,
Jul 23, 2013, 11:20:16 AM7/23/13
to plist-...@googlegroups.com
Hello,

I am using your library to parse a plist file in an Android project. I try to use your parsing example, but I have an error at this line:
NSDictionary rootDict = (NSDictionary) PropertyListParser.parse(plist); 

This is the error in my Logcat 
java.lang.ClassCastException: com.dd.plist.NSArray cannot be cast to com.dd.plist.NSDictionary

If it can help, the plist parameter in the PropertyListParser.parse() method is a File stored in my app data folder : /data/data/com.project.my/files/store.plist

Do you have an idea how to resolve this problem ?

Thanks by advance!

Daniel Dreibrodt

unread,
Jul 23, 2013, 11:23:44 AM7/23/13
to plist-discuss
Hi,

the property list you are trying to parse obviously has a NSArray as root object, not a NSDictionary.

Use

NSArray rootArray = (NSArray)PropertyListParser.parse(plist);

You should always know beforehand how the property list you want to parse is made up.

Regards,
Daniel

--
You received this message because you are subscribed to the Google Groups "plist-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plist-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jorisk

unread,
Jul 23, 2013, 11:39:10 AM7/23/13
to plist-...@googlegroups.com
Thanks for your fast answer Daniel, it is really helpful. 
Could you explain me how to do an array > dict > key/string parsing?
For example, I try to parse this kind of plist file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>ID</key>
<string>01</string>
<key>Title</key>
<string>The first title</string>
</dict>
</array>
<array>
<dict>
<key>ID</key>
<string>02</string>
<key>Title</key>
<string>The second title</string>
</dict>
</array> 
...

I know that I have to do something with rootArray.getArray() function, but I am not sure that I have to use the NSObject[] class here.

Daniel Dreibrodt

unread,
Jul 23, 2013, 11:57:12 AM7/23/13
to plist-discuss
You know what you expect inside the NSArray, so cast the NSObjects in the NSArray to the appropriate class, e.g. NSDictionary. The elements of the dictionary you should then cast to NSString or whatever is inside.

Can't give you more help right now, I'm quite busy. But it should be pretty straightforward, these are just simple data structures.

Jorisk

unread,
Jul 23, 2013, 12:45:47 PM7/23/13
to plist-...@googlegroups.com
Thank you very much for your explanations, I made it work!

For those who need the code:

NSArray rootArray = (NSArray)PropertyListParser.parse(plist);
                    NSObject[] obj = rootArray.getArray();
                    for(NSObject param:obj) {
                        NSDictionary title = (NSDictionary) param;
                        Log.d("Title", title.objectForKey("Title").toString());
                    }

Thanks again!
Reply all
Reply to author
Forward
0 new messages