Protocol buffers是谷歌出品的语言中立、跟平台无关、可扩展的工具,用来序列化/反序列化数据。相比XML和JSON要轻便灵活,原生支持C++,Java和Python。这里主要介绍跟iOS上如何使用Protobuf。
安装ProtoBuf
1.安装Homebrew,终端输入brew -v检查是否安装,如果未安装,输入下行命令安装
1
| ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
|
2.安装Protobuf的主要编译工具
1 2 3
| brew install automake brew install libtool brew install protobuf
|
因为我在执行brew install protobuf
时一直超时,只好从github上把文件下载下来安装,如果你安装成功,则调过这里直接执行第3步。
brew install protobuf
失败解决方案:
下载https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.bz2文件并解压,cd进入文件目录,依次下列命令
1 2 3 4
| ./autogen.sh ./configure make make install
|
3.安装protoc-gen-objc,因为Google原生不支持objc,所以这里需要通过protoc-gen-objc插件来编译生成Objective-C代码
1 2 3
| git clone https://github.com/alexeyxo/protobuf-objc.git cd protobuf-objc ./scripts/build.sh
|
此时/usr/local/bin目录下会有protoc和protoc-gen-objc两个文件,一切顺利。
使用ProtoBuf
1 2 3 4
| cd ~/Desktop/ mkdir ProtoBuf cd ProtoBuf vi User.proto
|
输入以下proto内容示例,然后按esc,输入:wq保存退出
1 2 3 4 5 6 7 8 9 10
| package com.rpc.proto;
message User { required int64 id = 1; required string account = 2; required string passwd = 3; required string nickname = 4; optional string avatar = 5; }
|
使用下列命令生成Objective-C代码
1
| protoc --plugin=/usr/local/bin/protoc-gen-objc *.proto --objc_out="./"
|
新建Xcode工程,添加Podfile,输入下列内容并执行pod install
将ProtoBuf目录下生成的User.pb.h和User.pb.m文件拖入Workspace工程。
序列化示例:
1 2 3 4 5 6 7 8
| UserBuilder *builder = [User builder]; builder.id = 1; builder.account = @"user"; builder.passwd = @"passwd"; builder.nickname = @"cocoa_chen"; builder.avatar = @"http://*****"; //optional User *user = [builder build]; NSData *data = [user data];
|
反序列化示例:
1 2
| NSData *data = ...; User *user = [User parseFromData:data];
|
PB对象属性如何更新:
Protobuf默认编译出来的对象属性都是readonly的,我们没办法直接修改,当然也可以用上面的方法重新build一遍,也可以重新建一个Model文件,将PB对象的内容赋值给我们自定义的Model对象。这里可以用objc的关联属性来实现一种读取+写入的方式,新建一个NSObject的Category,名字为EXTParams,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| //NSObject+EXTParams.h文件内容 #import <Foundation/Foundation.h>
@interface NSObject (EXTParams)
- (void)setExtParams:(id)param forKey:(NSString *)key;
- (id)extParamForKey:(NSString *)key;
- (void)removeExtParamsForKey:(NSString *)key;
- (void)removeAllExtParams;
#pragma mark - PB对象的赋值和读取 - (id)pbValueForKey:(NSString *)key;
- (void)setPBValue:(id)value forKey:(NSString *)key;
@end
//NSObject+EXTParams.m文件内容 #import "NSObject+EXTParams.h" #import <objc/runtime.h>
static char kObjectExtParamsKey;
@implementation NSObject (EXTParams)
- (void)setExtParams:(id)param forKey:(NSString *)key { NSMutableDictionary *extParams = (NSMutableDictionary *)objc_getAssociatedObject(self, & kObjectExtParamsKey); if (!extParams) { extParams = [[NSMutableDictionary alloc] init]; objc_setAssociatedObject(self, &kObjectExtParamsKey, extParams, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } [extParams setValue:param forKey:key]; }
- (id)extParamForKey:(NSString *)key { NSMutableDictionary *extParams = (NSMutableDictionary *)objc_getAssociatedObject(self, &kObjectExtParamsKey); if (!extParams) { return nil; } id param = [extParams valueForKey:key]; return param; } - (void)removeExtParamsForKey:(NSString *)key { NSMutableDictionary *extParams = (NSMutableDictionary *)objc_getAssociatedObject(self, &kObjectExtParamsKey); if (!extParams) return; [extParams setValue:nil forKey:key]; } - (void)removeAllExtParams { objc_setAssociatedObject(self, &kObjectExtParamsKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } #pragma mark - PB对象的赋值和读取 - (id)pbValueForKey:(NSString *)key { id extValue = [self extParamForKey:key]; if (extValue) { return extValue; } return [self valueForKey:key]; } - (void)setPBValue:(id)value forKey:(NSString *)key { [self setExtParams:value forKey:key]; } @end
|
假如需要对上面的User对象更新nickname属性,读取+写入的方法就成了下面的方式:
1 2 3 4 5
| //user为上面的User对象 //更新数据 [user setPBValue:@"another name" forKey:@"nickname"]; //读取时不再使用user.nickname,改为下面的方法读取 NSString *nickName = [user pbValueForKey:@"nickname"];
|
关联是解决对象属性readonly不能更新的方案之一,我们项目中用的是自定义一个对应的Model,其余方法就不涉及了。
参考