尝试从 Python (w/PyObjC) 写入二进制 plist 格式,以便由 Cocoa Touch 获取和读取

发布于 2024-08-23 12:44:03 字数 1581 浏览 9 评论 0原文

我正在尝试向我的 iPhone 应用程序提供搜索结果的属性列表。该服务器是一个原型,用 Python 编写。

首先我找到了Python内置的plistlib,非常棒。我想尝试一下“键入即搜索”,所以我需要它尽可能小,而 xml 太大了。二进制 plist 格式似乎是一个不错的选择。不幸的是 plistlib 不处理二进制文件,所以直接使用 PyObjC。

(Segue:对于如何完成实时搜索的任何其他想法,我都非常开放。我已经尽可能地减少了数据,包括仅显示足够的结果来填充 iPhone 键盘向上的窗口,即 5。)

不幸的是,尽管我了解 Python 并且对 Cocoa 的了解也相当不错,但我仍然不懂 PyObjC。

这就是我想要做的 Cocoa 等效项:

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];

我认为我应该能够做这样的事情,但是 dataWithPropertyList 不在 NSPropertyListSerialization 对象 dir() 列表中。我还应该将列表转换为 NSArray。我尝试了 PyObjC 文档,但它与我的实际工作太不相干了,所以我想我也应该尝试一下 SO SOS。

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,
    ?,
    ?)

这就是我在 iPhone 端的 plist 中阅读的方式。

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

很高兴澄清其中是否有任何意义。

I'm trying to serve a property list of search results to my iPhone app. The server is a prototype, written in Python.

First I found Python's built-in plistlib, which is awesome. I want to give search-as-you-type a shot, so I need it to be as small as possible, and xml was too big. The binary plist format seems like a good choice. Unfortunately plistlib doesn't do binary files, so step right up PyObjC.

(Segue: I'm very open to any other thoughts on how to accomplish live search. I already pared down 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 am getting 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 should be able to do something like this, but dataWithPropertyList isn't in the NSPropertyListSerialization objects dir() listing. I should also probably convert the list to NSArray. I tried the PyObjC docs, but it's so tangential to my real work that I thought I'd try an 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'm reading 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];

Happy to clarify if any of this doesn't make sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

在你怀里撒娇 2024-08-30 12:44:03

我相信正确的函数名称是

NSPropertyListSerialization.dataWithPropertyList_format_options_error_

因为结尾 :

(顺便说一句,如果对象始终是数组或字典,-writeToFile:atomically: 将已经写入 plist(作为 XML 格式)。)

I believe the correct function name is

NSPropertyListSerialization.dataWithPropertyList_format_options_error_

because of the ending :.

(BTW, if the object is always an array or dictionary, -writeToFile:atomically: will write the plist (as XML format) already.)

虐人心 2024-08-30 12:44:03

正如 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)

如果您测试生成的文件,您将看到它是一个二进制 PList 文件,如所需:

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

As KennyTM said, you're missing the trailing underscore in the method name. In PyObjC you need to take the Objective-C selector name (dataWithPropertyList:format:options:error:) and replace all of the colons with underscores (don't forget the last colon, too!). That gives you dataWithPropertyList_format_options_error_ (note the trailing underscore). Also, for the error parameter, you can just use None. That makes your code look like this:

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)

If you test the resulting file, you'll see that it's a Binary PList file, as desired:

Jagaroth:~/Desktop $ file test.plist 
test.plist: Apple binary property list
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文