layout: post
title: 關(guān)于在iOS中使用Google Protocol Buffer的使用體驗(yàn)
關(guān)于在iOS中使用Google Protocol Buffer的使用體驗(yàn)
Google protocol Buffer 環(huán)境
Google protocol Buffer 安裝
Google protocol Buffer 使用
Google protocol Buffer 環(huán)境
Google protocol Buffer 安裝前需要的環(huán)境有GCC蛮浑,和autoreconf的矿酵,安裝GCC的的直接使用Xcode安裝即可婴梧。
安裝autoreconf,使用mac下命令:
sudo brew install autoconf automake libtool
Google protocol Buffer 安裝
Google protocol Buffer 的文檔與地址是http://code.google.com/p/protobuf/
下載Protocol Buffers將下載解壓后的文件存放至Applications目錄下辨绊,進(jìn)到ProtocolBuffers-2.2.0-Source目錄看看會(huì)發(fā)現(xiàn)有個(gè)src目錄奶栖。
下載地址: http://code.google.com/p/metasyntactic/downloads/list
-
在終端中切換到管理員身份,在終端下輸入:su 然后輸入密碼门坷,如果提示 su:Sorry宣鄙,表明系統(tǒng)安全設(shè)置不允許,如果不想去更改默蚌,可以試著輸入:sudo su,輸入密碼后如果看到sh-3.2#這種樣式冻晤,表明成功。
注:切換到管理員身份不是必須的敏簿,理論上所有命令都可以通過前面加sudo來執(zhí)行。但我全部通過sudo來安裝宣虾,在自己指定目錄也能看到安裝文件惯裕,也有protoc文件,但是提示命令無法識(shí)別绣硝,切換到文件所在目錄也不行蜻势,沒找到原因。
用命令切換至ProtocolBuffers-2.2.0-Source目錄下鹉胖。
使用:
./autogen.sh
4.在終端下輸入
./configure (如果說沒有權(quán)限握玛,chmod +x configure)
如果不是管理員身份,需要輸入:./configure - -prefix=$INSTALL_DIR 后面表示你要把protobuf安裝的路徑甫菠,需要是絕對(duì)路徑挠铲。
- 依次在終端下輸入:
make
make check
make install
全部執(zhí)行完后再輸入protoc - - version檢查是否安裝成功。
**注: 使用make時(shí)會(huì)出現(xiàn)一些錯(cuò)誤寂诱,這時(shí)需在message.m中的頭文件中增加#include "istream"
Google protocol Buffer 使用
- 生成Object-C代碼
創(chuàng)建一個(gè)Person.proto文件把該文件存放至想要的文件夾中拂苹,文件內(nèi)容如下:
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;
}
-
在ProtocolBuffers-2.2.0-Source下創(chuàng)建這樣一個(gè)子目錄build/objc以便存放我們生成的classes
現(xiàn)在執(zhí)行命令:
protoc --proto_path=srcFolder --objc_out=desFolder
srcFolder/Person.proto
成功后會(huì)在desFolder下生成Person.pd.h 和 Person.pb.m 兩個(gè)Object-C文件
添加編譯環(huán)境
在Xcode中使用ProtocolBuffer
將步驟2中protobuf-obj/src/runtime/Classes目錄導(dǎo)入到Xcode項(xiàng)目中,導(dǎo)入時(shí)痰洒,選中”Copy items into destination group‘s folder(if needed)“瓢棒。
導(dǎo)入位置選擇項(xiàng)目根目錄。導(dǎo)入完畢后丘喻,項(xiàng)目根目錄下將會(huì)出現(xiàn)Classes目錄脯宿,將該目錄改名為ProtocolBuffers(注意最后的s): mv Classes ProtocolBuffers
修改項(xiàng)目屬性中”Build Settings–>Search Paths–>Header Search Paths”矿瘦,將項(xiàng)目根目錄“.”,"./ProtocolBuffer"添加到頭文件搜索路徑中去器罐。
這樣ProtocolBuffer for Objective-C的工作環(huán)境就配置好了。
工程中使用
- 將步驟3中編譯輸出的Person.pb.h 和Person.pb.m添加到項(xiàng)目中
- 將Person.pb.h 中的 #import <ProtocolBuffers/ProtocolBuffers.h> 改為#import”ProtocolBuffers/ProtocolBuffers.h”
- 在需要使用的地方引入頭文件:#import “Person.pb.h”
使用
Person *person = [[[[[Person builder] setName:@"極致"]
setId:1]
setEmail:@"abc@163.com"] build];
NSData *data = [person data];
NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [basePath stringByAppendingFormat:@"/person.data"];
if ([data writeToFile:path atomically:YES]) {
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);
}
}
輸出打印的結(jié)果如下:
2014-08-16 11:46:15.502 protocol[18670:60b]
id 1
name: 極致
email: abc@163.com