内容简介:之前写过namespace的问题不过后续都是没怎么使用,时下rx,snp…大家都应经不再陌生,也是比较常见的,今天我们结合struct 泛型 class一起看个综合的实例 通常我们使用命名空间都是基于一个具体的实例进行的二次封装(大家公认的)而封装的载体通常是struct,然后对struct进行extension 背景: 最经在搞protocol buffer来进行数据压缩,减轻网络流量,于是有了今天的文章结构定义 在vapor中我们想简单的返回pb数据不是那么简答,因此对返回数据进行了一个包装有定义看到
之前写过namespace的问题不过后续都是没怎么使用,时下rx,snp…大家都应经不再陌生,也是比较常见的,今天我们结合struct 泛型 class一起看个综合的实例 通常我们使用命名空间都是基于一个具体的实例进行的二次封装(大家公认的)而封装的载体通常是struct,然后对struct进行extension 背景: 最经在搞protocol buffer来进行数据压缩,减轻网络流量,于是有了今天的文章
结构定义 在vapor中我们想简单的返回pb数据不是那么简答,因此对返回数据进行了一个包装
1 返回数据格式
public struct PB<T> : Content where T: Message{
var value: Data
init(_ data: T) throws {
self.value = try data.serializedData()
}
}
复制代码
2 request
extension Request{
public func makePB<T>(value: T) throws -> EventLoopFuture<PB<T>> where T: Message{
let result = self.eventLoop.newPromise(PB<T>.self)
let pb = try PB.init(value)
result.succeed(result: pb)
return result.futureResult
}
}
复制代码
有定义看到返回的是个基于Message的泛型struct,我们暂时放置于此,后续使用
namespace包装 1 格式定义
class PBBase<Base, T> where T: Message{
let base: PB<T>
init(_ base: Base) {
self.base = base as! PB<T>
}
}
复制代码
2 协议定义
protocol PBProtocol {
associatedtype PBType
associatedtype T: Message
var pb:PBType{get}
}
extension PBProtocol{
public var pb:PBBase<Self, T>{
return PBBase.init(self)
}
}
复制代码
完成
extension PB : PBProtocol where T: Message{}
复制代码
使用
func testPBBase(){
XCTAssert(pb?.pb.entry != nil, "测试失败")
XCTAssert(pb?.pb.textFormatString() == bookInfo?.textFormatString(), "测试失败")
XCTAssert(pb?.pb.textString == pb?.textString, "测试失败")
}
复制代码
我们再来回顾一下整个过程使用了什么 1 使用 where限定了PBProtocol的使用范围
2 protocol中associatedtype的综合使用
2.1 可以定义多个 2.2 可以定义一个具体的类型,也可定义个模糊的类型 复制代码
3 nameapce的本质是二次封装
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 再说Python面向对象的三大特性
- 不要再说虚拟 DOM 有多快了
- Elasticsearch 是一把梭,用起来再说
- 看了这个别再说找工作难
- 别再说Serializable性能不如Parcelable啦
- 想要吃透大数据?先过了Hadoop这一关再说!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Learning Processing
Daniel Shiffman / Morgan Kaufmann / 2008-08-15 / USD 49.95
Book Description Teaches graphic artists the fundamentals of computer programming within a visual playground! Product Description This book introduces programming concepts in the context of c......一起来看看 《Learning Processing》 这本书的介绍吧!