2. 在iOS(Mac OS X)中使用protobufhtml
2.0 构建protocjava
A) 下载最新的protobuf版本python
B) 依据README中的步骤依次进行 ios
2.1 添加protobuf到工程中 git
A) 打开Xcode iOS 工程,点击"add file ..."选中"google"目录 (选中"copy items into destination group's folder (if needed) ")github
B) 在"Build Settings"将google头文件目录(例如:/Users/XiaoKL/protobuf-2.5.0/src)添加到"Header Search Paths"中objective-c
C) 将config.h添加到工程中redux
D) 从"google"这个group中删除unittestiphone
E) 从"google"这个group中删除compiler和java, 删除后的结果如R[1] 中的图所示ui
F) 构建工程,如今报告"tr1/unordered_map"这个文件找不到。
G) 须要作几处修改: config.h中
将"#define HASH_NAMESPACE std::tr1"替换为"#define HASH_NAMESPACE std" R[2]
将"#define HASH_MAP_H <tr1/unordered_map>"替换为"#define HASH_MAP_H <unordered_map>"
将"#define HASH_MAP_H <tr1/unordered_set>"替换为"#define HASH_MAP_H <unordered_set>"
添加libz库
到这里build经过。
2.2 使用protobuf
A) 定义.proto文件,参考R[4]
B) 生成目标文件
CPP: protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/xxx.proto
Python: protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/xxx.proto
C) 将生成的目标文件添加到工程中
2.3 protobuf在Objective-C中的难用之处
为了不C++代码进入Objective-C代码中,给开发工做带来复杂性。通常用Objective-C类来对protobuf的C++
类进行转换。这无疑增长了不少无聊的代码,这足以让开发人员沮丧。Ref6,Ref7中提到的protobuf-objc (protobuf
的Objective-C实现)代码库时间比较久远,是否是一个好的解决方法有待进一步确认。
若是,你有更好的在Objective-C中应用protobuf的方法请告诉我。
3. Dive into Protobuf
google::protobuf
Core components of the Protocol Buffers runtime library.
Reference:
1. http://stackoverflow.com/questions/10277576/google-protocol-buffers-on-ios
2. http://www.kotancode.com/2012/10/14/using-google-protocol-buffers-in-objective-c-on-ios-and-the-mac/
5. protobuf的Objective-C运行环境
http://www.cnblogs.com/tara/archive/2012/03/20/2407951.html
6. http://www.kotancode.com/2013/02/09/google-protocol-buffers-on-ios-and-mac-redux
7. https://github.com/regwez/protobuf-objc-iOS5
Reference:
1. http://www.pearltrees.com/davidb/protobuf/id6076142
2. https://code.google.com/p/protobuf/wiki/ThirdPartyAddOns