【发布时间】:2014-10-06 05:15:54
【问题描述】:
更新:已解决。代码已更新。
我正在编写一些代码来处理 Objective-C++ 中的 plist。
当我将函数传递给 XML plist 的路径时,一切正常,输出显示:
2014-08-12 17:06:47.932 plist_tests[96368:507] plist was in xml format
当我将函数传递给二进制 plist 的路径时,我收到以下错误:
2014-08-12 17:02:23.598 plist_tests[95709:507] could not deserialize plist: Error Domain=NSCocoaErrorDomain Code=3840 "The data couldn’t be read because it isn’t in the correct format." (Unexpected character b at line 1) UserInfo=0x7f9f2040cd60 {NSDebugDescription=Unexpected character b at line 1, kCFPropertyListOldStyleParsingError=The data couldn’t be read because it isn’t in the correct format.}
这是代码。请注意,我的用例不能使用 dictionaryWithContentsOfFile。
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#import <Foundation/Foundation.h>
class Status {
public:
Status(int c, std::string m) : code_(c), message_(m) {}
public:
int getCode() { return code_; }
std::string getMessage() { return message_; }
bool ok() { return getCode() == 0; }
std::string toString() { return getMessage(); }
private:
int code_;
std::string message_;
};
Status readFile(const std::string& path, std::string& content) {
if (!boost::filesystem::exists(path)) {
return Status(1, "File not found");
}
std::ifstream file_h(path);
if (file_h) {
file_h.seekg (0, file_h.end);
int len = file_h.tellg();
file_h.seekg (0, file_h.beg);
char *buffer = new char [len];
file_h.read(buffer, len);
if (!file_h) {
return Status(1, "Could not entire file");
}
content.assign(buffer, len);
} else {
return Status(1, "Could not open file for reading");
}
return Status(0, "OK");
}
void parsePlist(const std::string& path) {
std::string file_content;
Status readFileStatus = readFile(path, file_content);
if (!readFileStatus.ok()) {
NSLog(@"Couldn't read file");
return;
}
NSData *plist_content = [NSData dataWithBytes:file_content.c_str()
length:file_content.size()];
NSError *error;
NSPropertyListFormat plist_format;
id plist = [NSPropertyListSerialization propertyListWithData:plist_content
options:NSPropertyListImmutable
format:&plist_format
error:&error];
if (plist == nil) {
NSLog(@"could not deserialize plist: %@", error);
} else {
switch (plist_format) {
case NSPropertyListOpenStepFormat:
NSLog(@"plist was in openstep format");
break;
case NSPropertyListXMLFormat_v1_0:
NSLog(@"plist was in xml format");
break;
case NSPropertyListBinaryFormat_v1_0:
NSLog(@"plist was in binary format");
break;
default:
NSLog(@"plist was in unknown format");
break;
}
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cout << "Usage: plist <filename>" << std::endl;
return 1;
}
parsePlist(argv[1]);
return 0;
}
编译:g++ -lboost_system -lboost_filesystem -lglog -fobjc-arc -fobjc-link-runtime -framework Foundation plist.mm -o plist
我头疼的原因是,如果我在二进制 plist 上执行 plutil -convert xml1 file.plist -o -,它就可以正常工作。
【问题讨论】:
标签: c++ ios objective-c macos plist