Protocol Buffers在Objective-C上的使用
来源:互联网 发布:泛型编程与stl 编辑:程序博客网 时间:2024/06/05 16:44
1.下载protobuf-ios https://github.com/mingchen/protobuf-ios,保存到本地目录protobuf
2.打开protobuf目录,编译protobuf,具体步骤见https://github.com/mingchen/protobuf-ios上的使用指南
3.编写addressbook.proto,有关protocol buffers参见https://developers.google.com/protocol-buffers/docs/style
// See README.txt for information and build instructions.
package tutorial;
option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";
message Person {
required string name = 1;
required int32 id = 2; // Unique ID number for this person.
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;
}
// Our address book file is just one of these.
message AddressBook {
repeated Person person = 1;
}
4.从addressbook.proto获取oc代码,执行以下命令:
protoc --objc_out=. addressbook.proto
会在当前目录生成Addressbook.pb.h和Addressbook.pb.m文件
5.Xcode新建一个Single View Application,名为ProtobufDemo
6.在ProtobufDemo下新建一个Group名为protobuf, protobuf下新建两个group分别为model、protobuf-ios
其中, 将第四步生成的Addressbook.pb.h,Addressbook.pb.m添加到model目录下
将第一步下载的protobuf-ios,Classes目录下的所有文件添加到protobuf-ios
2.打开protobuf目录,编译protobuf,具体步骤见https://github.com/mingchen/protobuf-ios上的使用指南
3.编写addressbook.proto,有关protocol buffers参见https://developers.google.com/protocol-buffers/docs/style
// See README.txt for information and build instructions.
package tutorial;
option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";
message Person {
required string name = 1;
required int32 id = 2; // Unique ID number for this person.
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;
}
// Our address book file is just one of these.
message AddressBook {
repeated Person person = 1;
}
4.从addressbook.proto获取oc代码,执行以下命令:
protoc --objc_out=. addressbook.proto
会在当前目录生成Addressbook.pb.h和Addressbook.pb.m文件
5.Xcode新建一个Single View Application,名为ProtobufDemo
6.在ProtobufDemo下新建一个Group名为protobuf, protobuf下新建两个group分别为model、protobuf-ios
其中, 将第四步生成的Addressbook.pb.h,Addressbook.pb.m添加到model目录下
将第一步下载的protobuf-ios,Classes目录下的所有文件添加到protobuf-ios
7.在ProtobufDemo-Prefix.pch文件里,添加 #import <Foundation/Foundation.h>,注意在Xcode6上创建的工程默认是没有ProtobufDemo-Prefix.pch文件的,
需要手动创建,并设置Prefix Header参数
8.在ViewController里使用proton生成的类
<span style="background-color: rgb(255, 255, 255);"><span style="background-color: rgb(204, 204, 204);"><span style="font-size:14px;">- (void)viewDidLoad{ [super viewDidLoad]; Person_Builder *builder = [[Person_Builder alloc] init]; [builder setName:@"sword"]; [builder setId:12]; [builder setEmail:@"hjzhou@isoftstone.com"]; Person *person = [builder build];// NSLog(@"send person name:%@ userid %d email %@", person.name, person.id, person.email); Person_Builder *builder2 = [[Person_Builder alloc] init]; [builder2 setName:@"sword1"]; [builder2 setId:13]; [builder2 setEmail:@"hjzhou@isoftstone.co2m"]; Person *person2 = [builder2 build]; // NSLog(@"send person2 name:%@ userid %d email %@", person2.name, person2.id, person2.email); // NSData *personData = [person data]; Person *receivePerson = [Person parseFromData:personData]; NSLog(@"receive person name:%@ userid %d email %@", receivePerson.name, receivePerson.id, receivePerson.email); // Do any additional setup after loading the view, typically from a nib.}</span></span></span>源代码:https://github.com/heavensword/ProtobufDemoForiOS
0 0
- Protocol Buffers在Objective-C上的使用
- Objective C Protocol Buffers 编译
- protocol buffers c语言的使用
- Protocol Buffers在mac下的使用
- 【Protocol Buffers】在Flash中使用Google Protocol Buffers
- Protocol Buffers 的使用 (一)
- protocol buffers的使用示例
- protocol buffers的使用示例
- Wire protocol buffers的使用
- Protocol Buffers的安装使用和C++/Python入门示例
- 在C++中使用Google的“Protocol Buffers”
- Protocol Buffers在iOS中使用的简单介绍
- 在Flash中使用Google Protocol Buffers
- 在C#中使用Google Protocol Buffers
- 在 iOS 中使用 Protocol Buffers
- 在C++中使用Protocol Buffers
- 在Java中使用Protocol Buffers
- 在Android中使用Protocol Buffers
- 使用 HTML5 webSocket API实现即时通讯的功能
- 安卓各大系统属性设置,及屏幕锁定,点亮屏幕,连接wifi设置
- 使用jQuery中的getJSON()方法获取数据
- jQueryValidate的js效果出不来需要注意的几点
- InputFilter方法filter 解释
- Protocol Buffers在Objective-C上的使用
- 第一篇文章
- MFC 动态曲线 支持缩放 显示图例(CStatic派生类)
- ym——Android开发编码规范(自用)
- SpringMVC Controller 介绍
- 十一前后的思考
- ibatis中动态SQL_delete
- hdu 2068 RPG的错排 (错排,递推)
- VS2010项目受源代码管理向源代码管理注册此项目时出错