假设您想在苹果平台上执行此操作,您可以使用 CFPropertyListCreateFromStream、CFPropertyListCreateWithData 或 CFPropertyListCreateWithStream,它们是 CoreFoundation 框架的一部分:
所有这些函数都有以下参数:
format:指定属性列表格式的常量。有关可能的值,请参阅属性列表格式。
CFPropertyListCreateFromStream 也有以下参数:
stream:数据包含内容的流。流必须打开和配置——这个函数只是从流中读取字节。流可能包含任何受支持的属性列表类型(请参阅属性列表格式)。
The CFProperty constants definition 定义如下:
enum CFPropertyListFormat {
kCFPropertyListOpenStepFormat = 1,
kCFPropertyListXMLFormat_v1_0 = 100,
kCFPropertyListBinaryFormat_v1_0 = 200
};
typedef enum CFPropertyListFormat CFPropertyListFormat;
这往往表明上面提到的方法实际上可以读取二进制 plist。
二进制 plist 实现细节也已由 Apple here 开源。
苹果还有一些sample code,其中的小号是:
CFDataRef resourceData;
SInt32 errorCode;
Boolean status = CFURLCreateDataAndPropertiesFromResource(
kCFAllocatorDefault, fileURL, &resourceData,
NULL, NULL, &errorCode);
if (!status) {
// Handle the error
}
// Reconstitute the dictionary using the XML data
CFErrorRef myError;
CFPropertyListRef propertyList = CFPropertyListCreateWithData(
kCFAllocatorDefault, resourceData, kCFPropertyListImmutable, NULL, &myError);
// Handle any errors
CFRelease(resourceData);
CFRelease(myError);