前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >再说swift namespace

再说swift namespace

作者头像
大话swift
发布2019-07-04 11:09:21
5680
发布2019-07-04 11:09:21
举报
文章被收录于专栏:大话swift大话swift

之前写过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 可以定义一个具体的类型,也可定义个模糊的类型 github: https://github.com/skeyboy/SwiftVaporPB

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-12-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 大话swift 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 返回数据格式
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档