Trying to write to a binary plist format from Python (w / PyObjC) that will be retrieved and read using Cocoa Touch

I am trying to use a list of search result properties for my iPhone application. The server is a prototype written in Python.

At first I found Python's built-in plistlib, which is awesome. I want to allow search by your type, so I need it to be as small as possible and the xml to be too large. The binary plastic format seems like a good choice. Unfortunately, plistlib does not execute binaries, so step up PyObjC.

(Segue: I am very open to any other considerations on how to perform a real-time search. I have already simplified the data as much as possible, including only displaying enough results to fill the window with the iPhone keyboard up, which is 5).

Unfortunately, although I know Python and is pretty decent with Cocoa, I still don't get PyObjC.

This is the Cocoa equivalent of what I want to do:

NSArray *plist = [NSArray arrayWithContentsOfFile:read_path];
NSError *err;
NSData *data = [NSPropertyListSerialization dataWithPropertyList:plist
                   format:NSPropertyListBinaryFormat_v1_0
                  options:0 //  docs say this must be 0, go figure
                    error:&err];
[data writeToFile:write_path atomically:YES];

I thought I could do something similar, but dataWithPropertyList is not on the dir () list of the NSPropertyListSerialization object list. I probably should also convert the list to NSArray. I tried PyObjC docs, but it is so much about my real work that I thought I would try SO SOS too.

from Cocoa import NSArray, NSData, NSPropertyListSerialization, NSPropertyListBinaryFormat_v1_0
plist = [dict(key1='val', key2='val2'), dict(key1='val', key2='val2')]
NSPropertyListSerialization.dataWithPropertyList_format_options_error(plist,
    NSPropertyListBinaryFormat_v1_0,
    ?,
    ?)

This is how I read in the plist on the iPhone side.

NSData *data = [NSData dataWithContentsOfURL:url];
NSPropertyListFormat format;
NSString *err;
id it = [NSPropertyListSerialization
         propertyListFromData:data
         mutabilityOption:0
         format:&format
         errorDescription:&err];

We are happy to clarify whether this makes sense.

+3
2

,

NSPropertyListSerialization.dataWithPropertyList_format_options_error_

- :.

(BTW, , -writeToFile:atomically: plist ( XML).)

+4

KennyTM, . PyObjC Objective-C (dataWithPropertyList:format:options:error:) ( !). dataWithPropertyList_format_options_error_ ( ). , error None. :

bplist = NSPropertyListSerialization.dataWithPropertyList_format_options_error_(
                                     plist,
                                     NSPropertyListBinaryFormat_v1_0, 
                                     0, 
                                     None)

# bplist is an NSData object that you can operate on directly or
# write to a file...
bplist.writeToFile_atomically_(pathToFile, True)

, , Binary PList, :

Jagaroth:~/Desktop $ file test.plist 
test.plist: Apple binary property list
+2

Source: https://habr.com/ru/post/1734652/


All Articles