OpenSource.iOS.ProtobufWithObjective-C

2. 在iOS(Mac OS X)中使用protobuf

2.0 构建protoc

A) 下载最新的protobuf版本

B) 依据README中的步骤依次进行 

2.1 添加protobuf到工程中 

A) 打开Xcode iOS 工程,点击"add file ..."选中"google"目录 (选中"copy items into destination group's folder (if needed) ")

B) 在"Build Settings"将google头文件目录(例如:/Users/XiaoKL/protobuf-2.5.0/src)添加到"Header Search Paths"中

C) 将config.h添加到工程中

D) 从"google"这个group中删除unittest

E) 从"google"这个group中删除compiler和java, 删除后的结果如R[1] 中的图所示

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.

This file contains classes which describe a type of protocol message.

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/ 

3. http://www.giraffe-games.com/using-protobuf-protocol-buffers-on-iphone-ios/ 
4. https://developers.google.com/protocol-buffers/docs/proto 

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

---
原文地址:https://www.cnblogs.com/cwgk/p/2620754.html