• 【iMessage苹果相册推送苹果相册推】ProtocolBuffer拥有多项比XML更高档的序列化结构数据的特性


    推荐内容IMESSGAE相关

    作者推荐内容iMessage苹果推软件 *** 点击即可查看作者要求内容信息
    作者推荐内容1.家庭推内容 *** 点击即可查看作者要求内容信息
    作者推荐内容2.相册推 *** 点击即可查看作者要求内容信息
    作者推荐内容3.日历推 *** 点击即可查看作者要求内容信息
    作者推荐内容4.虚拟机安装简单 *** 点击即可查看作者要求内容信息
    作者推荐内容5.iMessage *** 点击即可查看作者要求内容信息

    一、阐明 1.协定声明了能够被任何类实现的法子 2.协议不是类,它是界说了一个其余工具可以实现的接口 3.若是在某个类中实现了协议中的某个方法,也便是这个类实现了阿谁协议。 4.协议常常用来实现拜托对象。一个委托对象是一种用来协同大概代表其他对象的特别对象。 5:委托,就是挪用本身定义方法,此外类来实现。

    6.新特征说明 @optional预编译指令:暗示可以挑选实现的方法 @required预编译指令:表示必需逼迫实现的方法 你起首必要在一个 .proto 文件中定义你需要做序列化的数据布局信息。每一个ProtocolBuffer信息是一小段逻辑记实,包括一系列的键值对。这里有个非常简略的.proto 文件定义了个人信息: 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; } 如你所见,动静格局很简单,每个消息范例具有一个或多个特定的数字字段,每个字段拥有一个名字和一个值类型。值类型可以是数字(整数或浮点)、布尔型、字符串、原始字节或者其他ProtocolBuffer类型,还容许数据结构的多层次嵌套。你可以指定可选字段,必选字段和反复字段。

    在这里插入图片描述

    你可以在(https://developers.google.com/protocol-buffers/docs/proto )找到更多对于若何编写.proto 文件的信息。 一旦你定义了自己的报文格式(message),你就可以运转ProtocolBuffer编译器,将你的.proto 文件编译成特定说话的类。这些类供给了简单的方法拜候每个字段(像是name() 和set_name() ),像是访问类的方法同样将结构序列化或反序列化。比方你可以选择C++语言,运行编译如上的协议文件天生类叫做Person 。随后你就可以在利用中利用这个类来序列化的读取报文信息。

    你可以这么写代码: Person person; person.set_name(“John Doe”); person.set_id(1234); person.set_email(“jdoe@example.com”); fstream output(“myfile”, ios::out | ios::binary); person.SerializeToOstream(&output); 而后,你可以读取报文中的数据: fstream input(“myfile”, ios::in | ios::binary); Person person; person.ParseFromIstream(&input); cout << "Name: " << person.name() << endl; cout << "E-mail: " << person.email() << endl; 你可以在不影响向后兼容的环境下随便给数据结构增长字段,在反序列化的时辰,旧有的数据会疏忽新的字段。以是如果使用ProtocolBuffer作为通信协议,你可以不必担忧粉碎现有代码的情况下扩大协议。 你可以在API参考(https://developers.google.com/protocol-buffers/docs/reference/overview ) 中找到完备的参考,而且关于ProtocolBuffer的报文格式编码则可以在(https://developers.google.com/protocol-buffers/docs/encoding )中找到参考。 为何不消XML?

    ProtocolBuffer拥有多项比XML更高档的序列化结构数据的特性,ProtocolBuffer:更简单 · 小3-10倍 · 快20-100倍 · 更少的歧义 · 可以便利的生成数据存取类 例如,让咱们看看如何在XML中建模Person的name和email字段: John Doe jdoe@example.com 对应的ProtocolBuffer报文则以下: # Textual representation of a protocol buffer. # This is not the binary format used on the wire. person { name: “John Doe” email: “jdoe@example.com” } 当这个报文编码到ProtocolBuffer的二进制格式 (https://developers.google.com/protocol-buffers/docs/encoding )时(下面的文本仅用于调试和编纂),它只需要28字节和100-200ns的剖析时候。而XML的版本需要69字节(撤除空缺)和5000-10000ns的解析时间。
    在这里插入图片描述

    固然,操纵ProtocolBuffer也很简单: cout << "Name: " << person.name() << endl; cout << "E-mail: " << person.email() << endl; 而XML的你需要: cout << "Name: " << person.getElementsByTagName(“name”)->item(0)->innerText() << endl; cout << "E-mail: " << person.getElementsByTagName(“email”)->item(0)->innerText() << endl; 当然,ProtocolBuffer并不是在任何时候都比XML更合适,例如ProtocolBuffer无法对一个基于标识表记标帜文本的文档建模,由于你底子没法方便的在文本中插入结构。别的,XML是便于人类浏览和编辑的,而ProtocolBuffer则不是。

    另有XML是自解释的,而ProtocolBuffer仅在你拥有报文格式定义的.proto 文件时才故意义。 二、定义 .h @protocol ContactCtrlDelegate -(void)DismissContactsCtrl; @end @interface ContactsCtrl : UIViewController { id delegate; } @property (nonatomic, assign) id delegate; .m @synthesize delegate; 三、例子 例如:UITextView @protocol UITextViewDelegate @optional - (BOOL)textViewShouldBeginEditing:(UITextView *)textView; - (BOOL)textViewShouldEndEditing:(UITextView *)textView; - (void)textViewDidBeginEditing:(UITextView *)textView; - (void)textViewDidEndEditing:(UITextView *)textView; - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)

    text; - (void)textViewDidChange:(UITextView *)textView; - (void)textViewDidChangeSelection:(UITextView )textView; @end 如果要调用以上这些方法,就必须配置UITextView的委托:TextView.delegate = self; SBRemoteLocalNotificationAlertDelegate协议的实现进程 1.SBRemoteLocalNotificationAlertDelegate-Protocol.h p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008700} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo} p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #b80fa2} span.s1 {color: #b80fa2} / *

    Generated by class-dump 3.3.3 (32 bit). * * class-dump is Copyright © 1997-1998, 2000-2001, 2004-2010 by Steve Nygard. / @protocol SBRemoteLocalNotificationAlertDelegate - (void)remoteLocalNotificationAlertShouldLaunch:(id)arg1 forApplication:(id)arg2; @optional - (void)remoteLocalNotificationAlertShouldSnooze:(id)arg1 forApplication:(id)arg2; @end 2.使用协议 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008700} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #cf231d} p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo} p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #b80fa2} span.s1 {color: #77482b} span.s2 {color: #b80fa2} span.s3 {color: #000000} span.s4 {color: #008700} / * Generated by class-dump 3.3.3 (32 bit). * * class-dump is Copyright © 1997-1998, 2000-2001, 2004-2010 by Steve Nygard. */ #import “SBAlertItem.h” @class NSString, NSTimer, SBApplication; @interface SBRemoteLocalNotificationAlert : SBAlertItem { id _delegate; } @property(nonatomic) id delegate; // @synthesize delegate=_delegate; @end 3.m 文件实现协议

  • 相关阅读:
    实战二十六:基于字符串匹配的实体对齐任务 代码+数据 (可作为毕设)
    【Makefile】报错:undefined reference to symbol ‘pthread_spin_init@@GLIBC_2.2.5‘
    text2sql、nl2sql框架总结
    鹅长微服务发现与治理巨作PolarisMesh实践-上
    Linux 三剑客grep sed 与 awk
    【C++】命名空间深度理解
    Java的抽象类 & 接口
    Android移动安全攻防实战 ApkTool工具源码分析
    QT实现tcp服务器客户端
    计算机毕业设计之java+javaweb的烘焙爱好者网站
  • 原文地址:https://blog.csdn.net/IMEAX/article/details/126463470