http://code.google.com/intl/zh-CN/apis/protocolbuffers/
ios
編譯安裝(生成的編譯器protoc在/usr/local/bin目錄下):c++
cd protobuf-2.4.1git
./configure
make
make installgithub
二、下載Objective-C compiler for ProtocolBuffer(目前有兩種類型的實現)。api
(1)、針對ProtocolBuffer2.2作修改,使最後生成的.proto文件編譯器(protoc)支持Objective-C類型的文件輸出。
http://code.google.com/p/metasyntactic/wiki/ProtocolBuffers
(2)、針對ProtocolBuffer2.3推出的plugin模式編寫插件,以插件方式支持Objective-C類型的文件輸出。
https://github.com/booyah/protobuf-objc
我選用第(1)種方式,這也是Google推薦的方式。
http://code.google.com/p/metasyntactic/wiki/ProtocolBuffers,下載源碼壓縮包後解壓到相關目錄
進入該目錄,並執行:app
cd ProtocolBuffers-2.2.0-Source測試
./configure
make
make installui
沒有這麼順利google
google/protobuf/message.cc:130:60: error: implicit instantiation of undefined
template 'std::__1::basic_istream<char, std::__1::char_traits<char> >'
return ParseFromZeroCopyStream(&zero_copy_input) && input->eof();
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:109:33: note:
template is declared here
class _LIBCPP_TYPE_VIS_ONLY basic_istream;
^
google/protobuf/message.cc:135:67: error: implicit instantiation of undefined
template 'std::__1::basic_istream<char, std::__1::char_traits<char> >'
return ParsePartialFromZeroCopyStream(&zero_copy_input) && input->eof();
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:109:33: note:
template is declared here
class _LIBCPP_TYPE_VIS_ONLY basic_istream;
^
2 warnings and 2 errors generated.
make[2]: *** [message.lo] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
atom
解決方法以下:
找到文件src/google/protobuf/message.cc 在文件的第一個#include之上的一行,加入
#include <istream>
繼續運行make ,即成功經過。
寫一個person.proto文件:
[cpp] view plaincopy
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phone = 4;
}
編譯該文件:
protoc person.proto --objc_out=/Output/Directory/
protoc會使用該插件編譯.proto文件,最終生成兩個文件:Person.pb.h 、Person.pb.m(不支持ARC)
若是工程中使用了ARC ,因此須要使用-fno-objc-arc來標識相關的文件不使用ARC機制:
這個步驟經過後,說明ProtocoBuffer Compiler for Objective-C能夠正常工做了。
將步驟2中protobuf-obj/src/runtime/Classes目錄導入到Xcode項目中,導入時,選中」Copy items into destination group‘s folder(if needed)「。
導入位置選擇項目根目錄。導入完畢後,項目根目錄下將會出現Classes目錄,將該目錄更名爲ProtocolBuffers(注意最後的s): mv Classes ProtocolBuffers
修改項目屬性中」Build Settings-->Search Paths-->Header Search Paths」,將項目根目錄「.」添加到頭文件搜索路徑中去。
這樣ProtocolBuffer for Objective-C的工做環境就配置好了。
1)、將步驟3中編譯輸出的Person.pb.h 和Person.pb.m添加到項目中
2)、將Person.pb.h 中的 #import <ProtocolBuffers/ProtocolBuffers.h> 改成#import"ProtocolBuffers/ProtocolBuffers.h"
3)、在須要使用的地方引入頭文件:#import "Person.pb.h"
[cpp] view plaincopy
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self writeAndReadProtobuf];
}
- (void)writeAndReadProtobuf{
Person *person = [[[[[Person builder] setName:@"極致"]
setId:1]
setEmail:@"abc@163.com"] build];
NSData *data = [person data];
NSString *docPath = [self applicationDocumentsDirectory];
NSString *path = [docPath stringByAppendingFormat:@"/person.data"];
if ([data writeToFile:path atomically:YES]) {
[self readFileWithPath:path];
}
}
- (NSString *)applicationDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
- (void)readFileWithPath:(NSString *)path {
NSData *data = [NSData dataWithContentsOfFile:path];
Person *person = [Person parseFromData:data];
if (person) {
NSLog(@"\n id %d \n name: %@ \n email: %@ \n",person.id, person.name, person.email);
}
}
輸出打印的結果以下:
在執行 ./autogen.sh時出現錯誤: ./autogen.sh: line 10: autoreconf: command not found
解決辦法:須要安裝automake和autoconf:
brew install automake
brew install autoconf