Python dictionary to Cocoa NSDictionary

79 views
Skip to first unread message

Ivan C Myrvold

unread,
Aug 15, 2012, 4:56:41 AM8/15/12
to blip-p...@googlegroups.com
I am having fun with BLIP as a python server and iPhone as a client. If I am declaring a python dictionary as e.g.

mylist = {}
mylist['hebbe'] = 'lille'
mylist['batman'] = 'robin'

I am sending this back to my iPhone code in Xcode as
response.properties['mylist'] = mylist

Now I can retrieve this in my BLIP iPhone code
NSDictionary *responsedict = response.properties.allProperties;
NSString *mylistStr = [responsedict objectForKey:@"mylist"];
NSDictionary *mylistDict = ???

How can I convert mylistStr to a real NSDictionary? I have looked at BLIPProperties, but couldn't figure out how to do this;

Jens Alfke

unread,
Aug 15, 2012, 12:29:45 PM8/15/12
to Ivan C Myrvold, blip-p...@googlegroups.com
On Aug 15, 2012, at 1:56 AM, Ivan C Myrvold <ivanm...@gmail.com> wrote:

I am sending this back to my iPhone code in Xcode as
response.properties['mylist'] = my list
...
How can I convert mylistStr to a real NSDictionary? I have looked at BLIPProperties, but couldn't figure out how to do this;

Property values in BLIP are strings. So the Python BLIP library must be serializing the dictionary to a string in some format before sending it. I don't know what that format is (I didn't write the Python BLIP library) but you'd need to parse it on the Objective-C side.

It would probably be better to do explicit serialization yourself rather than rely on some internal behavior of the Python library. I recommend JSON since it's simple and widely implemented; so use a Python JSON API to encode 'mylist' to a string, then use NSJSONSerialization on iOS to parse it.

You might want to use the body of the message instead of the properties, though, depending on how large your data can get. Properties are limited to a total size of 64k bytes, while the message body is unlimited.

—Jens
Reply all
Reply to author
Forward
0 new messages