#MDCC Swift 链式语法应 用 陈乘

Similar documents
Understanding IO patterns of SSDs

ICP Enablon User Manual Factory ICP Enablon 用户手册 工厂 Version th Jul 2012 版本 年 7 月 16 日. Content 内容

<properties> <jdk.version>1.8</jdk.version> <project.build.sourceencoding>utf-8</project.build.sourceencoding> </properties>

Previous on Computer Networks Class 18. ICMP: Internet Control Message Protocol IP Protocol Actually a IP packet

1. DWR 1.1 DWR 基础 概念 使用使用 DWR 的步骤. 1 什么是 DWR? Direct Web Remote, 直接 Web 远程 是一个 Ajax 的框架

三 依赖注入 (dependency injection) 的学习

如何查看 Cache Engine 缓存中有哪些网站 /URL

第二小题 : 逻辑隔离 (10 分 ) OpenFlow Switch1 (PC-A/Netfpga) OpenFlow Switch2 (PC-B/Netfpga) ServerB PC-2. Switching Hub

Triangle - Delaunay Triangulator

Microsoft RemoteFX: USB 和设备重定向 姓名 : 张天民 职务 : 高级讲师 公司 : 东方瑞通 ( 北京 ) 咨询服务有限公司

测试基础架构 演进之路. 茹炳晟 (Robin Ru) ebay 中国研发中心

实验三十三 DEIGRP 的配置 一 实验目的 二 应用环境 三 实验设备 四 实验拓扑 五 实验要求 六 实验步骤 1. 掌握 DEIGRP 的配置方法 2. 理解 DEIGRP 协议的工作过程

Ganglia 是 UC Berkeley 发起的一个开源集群监视项目, 主要是用来监控系统性能, 如 :cpu mem 硬盘利用率, I/O 负载 网络流量情况等, 通过曲线很容易见到每个节点的工作状态, 对合理调整 分配系统资源, 提高系统整体性能起到重要作用

<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <!--- global properties --> <property>

Build a Key Value Flash Disk Based Storage System. Flash Memory Summit 2017 Santa Clara, CA 1

Chapter 11 SHANDONG UNIVERSITY 1

PCU50 的整盘备份. 本文只针对操作系统为 Windows XP 版本的 PCU50 PCU50 启动硬件自检完后, 出现下面文字时, 按向下光标键 光标条停在 SINUMERIK 下方的空白处, 如下图, 按回车键 PCU50 会进入到服务画面, 如下图

public static InetAddress getbyname(string host) public static InetAddress getlocalhost() public static InetAddress[] getallbyname(string host)

Chapter 1 (Part 2) Introduction to Operating System

Command Dictionary CUSTOM

学习沉淀成长分享 EIGRP. 红茶三杯 ( 朱 SIR) 微博 : Latest update:

AvalonMiner Raspberry Pi Configuration Guide. AvalonMiner 树莓派配置教程 AvalonMiner Raspberry Pi Configuration Guide

libde265 HEVC 性能测试报告

IPC 的 Proxy-Stub 设计模式 ( c)

IP unnumbered 实验讲义 一. 实验目的 : 二. 实验设备 : 三. 实验拓扑 : 四. 实验内容 :

Spark Standalone 模式应用程序开发 Spark 大数据博客 -

NyearBluetoothPrint SDK. Development Document--Android

New Media Data Analytics and Application. Lecture 7: Information Acquisition An Integration Ting Wang

A Benchmark For Stroke Extraction of Chinese Characters

Apache Kafka 源码编译 Spark 大数据博客 -

2. Introduction to Digital Media Format

计算机组成原理第二讲 第二章 : 运算方法和运算器 数据与文字的表示方法 (1) 整数的表示方法. 授课老师 : 王浩宇

Logitech G302 Daedalus Prime Setup Guide 设置指南

Software Engineering. Zheng Li( 李征 ) Jing Wan( 万静 )

WSV 让网站更加安全的几个小 妙招 徐栋 北京中达金桥技术服务有限公司

TBarCode OCX Microsoft ActiveX compliant Barcode Control

绝佳的并行处理 - FPGA 加速的根本基石

Digital Asset Management 数字媒体资源管理理 2. Introduction to Digital Media Format

Multiprotocol Label Switching The future of IP Backbone Technology

朱晔和你聊 Spring 系列 S1E2: SpringBoot 并不神秘

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

OTAD Application Note

Silverlight 3 概览 俞晖市场推广经理微软 ( 中国 ) 有限公司

XML allows your content to be created in one workflow, at one cost, to reach all your readers XML 的优势 : 只需一次加工和投入, 到达所有读者的手中

The Design of Everyday Things

Virtual Memory Management for Main-Memory KV Database Using Solid State Disk *

上汽通用汽车供应商门户网站项目 (SGMSP) User Guide 用户手册 上汽通用汽车有限公司 2014 上汽通用汽车有限公司未经授权, 不得以任何形式使用本文档所包括的任何部分

nbns-list netbios-type network next-server option reset dhcp server conflict 1-34

OpenCascade 的曲面.

NetScreen 概念与范例. ScreenOS 参考指南 第 7 卷 : 虚拟系统. ScreenOS 编号 SC 修订本 E

1. Spring 整合 Jdbc 进行持久层开发

Technology: Anti-social Networking 科技 : 反社交网络

Jbuilder 2007 开发 EJB3.0 Entity 罗代均 ldj_work#126.com 2007 年 8 月

深入了解 Redis. 宋传胜

密级 : 博士学位论文. 论文题目基于 ScratchPad Memory 的嵌入式系统优化研究

畅联科技锁掌柜开放接口 - Android 版本 用户手册

Decode Zend. Darkness/Airsupply

Skill-building Courses Business Analysis Lesson 3 Problem Solving

VAS 5054A FAQ ( 所有 5054A 整合, 中英对照 )

CloudStack 4.3 API 开发指南!

RPG IV 新特性概览. 钟嘉田 DB2 for i Performance, IBM 中国系统与科技研发中心

DEV Office 客户端开发增强

S 1.6V 3.3V. S Windows 2000 Windows XP Windows Vista S USB S RGB LED (PORT1 PORT2 PORT3) S I 2 C. + 表示无铅 (Pb) 并符合 RoHS 标准 JU10 JU14, JU24, JU25

漂亮的测试. By Alberto Savoia 代码之美 第七章 史际帆

Murrelektronik Connectivity Interface Part I Product range MSDD, cable entry panels MSDD 系列, 电缆穿线板

Chapter2 Instruction Sets

Declaration of Conformity STANDARD 100 by OEKO TEX

Presentation Title. By Author The MathWorks, Inc. 1

[ 电子书 ]Spark for Data Science PDF 下载 Spark 大数据博客 -

CHINA VISA APPLICATION CONCIERGE SERVICE*

Computer Networks. Wenzhong Li. Nanjing University

我们应该做什么? 告知性分析 未来会发生什么? 预测性分析 为什么会发生 诊断性分析 过去发生了什么? 描述性分析 高级分析 传统 BI. Source: Gartner

Autodesk Backburner 2011 安装手册

MFUN [ 文档编号 :MFUN-DMSJ ]

Wireless Presentation Pod

China Next Generation Internet (CNGI) project and its impact. MA Yan Beijing University of Posts and Telecommunications 2009/08/06.

Safe Memory-Leak Fixing for C Programs

Smart Services Lucy Huo (Senior Consultant, UNITY Business Consulting) April 27, 2016

Altera 器件高级特性与应用 内容安排 时钟管理 时钟管理 片内存储器 数字信号处理 高速差分接口 高速串行收发器. 时钟偏斜 (skew): 始终分配到系统中到达各个时钟末端 ( 器件内部触发器的时钟输入端 ) 的时钟相位不一致的现象 抖动 : 时钟边沿的输出位置和理想情况存在一定的误差

ngx_openresty: an Nginx ecosystem glued by Lua

Keygen Codes For Photoshop Cs6 ->>> DOWNLOAD

Compile times - assert macros

/* 说明 : 由于在实现算法的过程中, 我们无法验证自己的思想的正确性, 所以用 c++ 实现了并进行了验证 数据结构以突出思想, 体现结构的逻辑结构为主, 所以在看算法的时候, 主要看思想和逻辑,

云计算入门 Introduction to Cloud Computing GESC1001

Outline. Motivations (1/3) Distributed File Systems. Motivations (3/3) Motivations (2/3)

Congestion Control Mechanisms for Ad-hoc Social Networks 自组织社会网络中的拥塞控制机制

Tizen v2.3 Graphics & UI Frameworks. Embedded Software SKKU

本文列出 Git 常用命令, 点击下图查看大图

TW5.0 如何使用 SSL 认证. 先使用 openssl 工具 1 生成 CA 私钥和自签名根证书 (1) 生成 CA 私钥 openssl genrsa -out ca-key.pem 1024

赛灵思技术日 XILINX TECHNOLOGY DAY 用赛灵思 FPGA 加速机器学习推断 张帆资深全球 AI 方案技术专家

Green Computing Cloud Computing LSD Tech Co., Ltd SSD server & SSD Storage Cloud SSD Supercomputer LSD Tech Co., LTD

新一代 ODA X5-2 低调 奢华 有内涵

在数据中心中加速 AI - Xilinx 机器学习套件 (Xilinx ML Suite )

基于 Davinci 平台的视频应用开发 沈燕飞

测试 SFTP 的 问题在归档配置页的 MediaSense

BlueCore BlueTunes Configuration Tool User Guide

F03_c. 观摩 : SurfaceView 小框架 基於軟硬整合觀點. 的特殊性设计 ( c) By 高煥堂

Vista 程序员的好帮手. XXX DPE, Microsoft China

Packaging 10Apr2012 Rev V Specification MBXL HSG 1. PURPOSE 目的 2. APPLICABLE PRODUCT 适用范围

Transcription:

#MDCC 2016 Swift 链式语法应 用 陈乘 方 @ENJOY

关于我 Swift 开发者 ENJOY ios 客户端负责 人 两年年 Swift 实际项 目开发经验 微博 ID: webfrogs Twitter: nswebfrog

Writing code is always easy, the hard part is reading it.

链式语法?

链式语法 可以连续不不断地进 行行 方法调 用的 一种语法形式

Objective-C 链式语法 RAC [[[[client login] then:^{ return [client loadcachedmessages]; ] flattenmap:^(nsarray *messages) { return [client fetchmessagesaftermessage: messages.lastobject]; ] subscribeerror:^(nserror *error) { [self presenterror:error]; completed:^{ NSLog(@"Fetched all messages."); ]; Masonry make.top.equalto(self.view).with.offset(80);

Swift 链式语法 Optional Chaining let dic: [String: String] = ["url": "http://www.apple.com"] let request = dic["url"].flatmap { URL(string: $0).map { URLRequest(url: $0)

链式语法优点 简洁 高复 用性 高可读性 减少中间变量量

实现 方式 函数的返回值类型与 自身类型相同 举例例 : extension Int { func add(_ value: Int) -> Int { return self + value let sum = 3.add(2).add(6)

应 用实例例 链式 UI 代码 链式 网络结果处理理

UI 代码 let firstlabel = UILabel() view.addsubview(firstlabel) firstlabel.snp.makeconstraints { (make) in make.top.equalto(self.view).offset(80) make.centerx.equalto(self.view) firstlabel.backgroundcolor =.clear firstlabel.font = UIFont.systemFont(ofSize: 20) firstlabel.textcolor =.red firstlabel.text = "firstlabel" let secondlabel = UILabel() view.addsubview(secondlabel) secondlabel.snp.makeconstraints { (make) in make.top.equalto(firstlabel.snp.bottom).offset(20) make.centerx.equalto(self.view) secondlabel.backgroundcolor =.clear secondlabel.font = UIFont.systemFont(ofSize: 20) secondlabel.textcolor =.red secondlabel.text = "secondlabel"

UI 代码 let firstlabel = UILabel() view.addsubview(firstlabel) firstlabel.snp.makeconstraints { (make) in make.top.equalto(self.view).offset(80) make.centerx.equalto(self.view) firstlabel.backgroundcolor =.clear firstlabel.font = UIFont.systemFont(ofSize: 20) firstlabel.textcolor =.red firstlabel.text = "firstlabel" let secondlabel = UILabel() view.addsubview(secondlabel) secondlabel.snp.makeconstraints { (make) in make.top.equalto(firstlabel.snp.bottom).offset(20) make.centerx.equalto(self.view) secondlabel.backgroundcolor =.clear secondlabel.font = UIFont.systemFont(ofSize: 20) secondlabel.textcolor =.red secondlabel.text = "secondlabel"

UI 代码 1. 创建 view let firstlabel = UILabel() view.addsubview(firstlabel) firstlabel.snp.makeconstraints { (make) in make.top.equalto(self.view).offset(80) make.centerx.equalto(self.view) firstlabel.backgroundcolor =.clear firstlabel.font = UIFont.systemFont(ofSize: 20) firstlabel.textcolor =.red firstlabel.text = "firstlabel" 2. 添加到 父视图 3. 设置约束 4. 配置 view 属性 let secondlabel = UILabel() view.addsubview(secondlabel) secondlabel.snp.makeconstraints { (make) in make.top.equalto(firstlabel.snp.bottom).offset(20) make.centerx.equalto(self.view) secondlabel.backgroundcolor =.clear secondlabel.font = UIFont.systemFont(ofSize: 20) secondlabel.textcolor =.red secondlabel.text = "secondlabel"

链式 UI 代码 let firstlabel = UILabel.ccf_create(withSuperView: view).ccf_layout { (make) in make.top.equalto(self.view).offset(80) make.centerx.equalto(self.view).ccf_config { (label) in label.backgroundcolor =.clear label.font = UIFont.systemFont(ofSize: 20) label.textcolor =.red label.text = "firstlabel" let secondlabel = UILabel.ccf_create(withSuperView: view).ccf_layout { (make) in make.top.equalto(firstlabel.snp.bottom).offset(20) make.centerx.equalto(self.view).ccf_config { (label) in label.backgroundcolor =.clear label.font = UIFont.systemFont(ofSize: 20) label.textcolor =.red label.text = "secondlabel"

链式 UI 代码 let commonlabelconfig: (UILabel) -> Void = { label in label.backgroundcolor =.clear label.font = UIFont.systemFont(ofSize: 20) label.textcolor =.red let firstlabel = UILabel.ccf_create(withSuperView: view).ccf_layout { (make) in make.top.equalto(self.view).offset(80) make.centerx.equalto(self.view).ccf_config(commonlabelconfig).ccf_config { (label) in label.text = "firstlabel" let secondlabel = UILabel.ccf_create(withSuperView: view).ccf_layout { (make) in make.top.equalto(firstlabel.snp.bottom).offset(20) make.centerx.equalto(self.view).ccf_config(commonlabelconfig).ccf_config { (label) in label.text = "secondlabel"

链式 UI 代码 let commonlabelconfig: (String?) -> ((UILabel) -> Void) = { text in return { label in label.backgroundcolor =.clear label.font = UIFont.systemFont(ofSize: 20) label.textcolor =.red label.text = text let firstlabel = UILabel.ccf_create(withSuperView: view).ccf_layout { (make) in make.top.equalto(self.view).offset(80) make.centerx.equalto(self.view).ccf_config(commonlabelconfig("firstlabel")) let secondlabel = UILabel.ccf_create(withSuperView: view).ccf_layout { (make) in make.top.equalto(firstlabel.snp.bottom).offset(20) make.centerx.equalto(self.view).ccf_config(commonlabelconfig("secondlabel"))

链式 UI 代码 Demo Talk is cheap. Show me the code. Linus Torvalds

应 用实例例 链式 UI 代码 链式 网络结果处理理

网络结果处理理 目的 :JSON 数据转换为 Model 对象

网络结果处理理 let jsondata: Any = ["key": "value"] guard let jsondic = jsondata as? [String: Any] else { // TODO: data format error return guard let model = DemoModel(jsonDic: jsondic) else { // TODO: data format error return // do something

链式 网络结果处理理 封装 网络结果 抽象数据处理理过程 提供基础处理理组件

链式 网络结果处理理 demoresult.flatmap(responsedatatodic).flatmap(responsedictoapimodel).successhandler { (data: DemoResponseModel) in // TODO: success print(data).failurehandler { (error) in // TODO: failure print(error)

链式 网络结果处理理 网络结果封装 : enum APIResult<T> { case success(t) case failure(error)

链式 网络结果处理理 extension APIResult { func flatmap<u>(_ transform: (T) -> APIResult<U>) -> APIResult<U> { let result: APIResult<U> switch self { case let.success(value): result = transform(value) case let.failure(error): result =.failure(error) return result func map<u>(_ transform: (T) -> U) -> APIResult<U> { let result: APIResult<U> switch self { case let.success(value): result =.success(transform(value)) case let.failure(error): result =.failure(error) return result

链式 网络结果处理理 extension APIResult { func successhandler(_ success: (T) -> Void) -> APIResult<T> { if case let.success(value) = self { success(value) return self func failurehandler(_ failure: (Error) -> Void) -> APIResult<T> { if case let.failure(error) = self { failure(error) return self

链式 网络结果处理理 Demo Talk is cheap. Show me the code. Linus Torvalds

命名空间式扩展 去除 Objective-C 时代的前缀式扩展 方法命名 目前 几乎所有主流的 Swift 三 方框架都已 支持 SnapKit: view.snp_makeconstraints { (make) in view.snp.makeconstraints { (make) in

命名空间式扩展 protocol NamespaceCompatible { associatedtype CompatibleType var ccf: CompatibleType { get static var ccf: CompatibleType.Type { get struct Namespace<Base> { let base: Base init(_ base: Base) { self.base = base extension NamespaceCompatible { var ccf: Namespace<Self> { return Namespace(self) static var ccf: Namespace<Self>.Type { return Namespace.self extension NSObject: NamespaceCompatible {

命名空间式扩展 UI 链式代码的适配 对 Struct 进 行行扩展 比较麻烦

命名空间式扩展 Demo Talk is cheap. Show me the code. Linus Torvalds

总结 了了解链式代码 如何编写 自 己的链式代码 链式代码实例例 Swifty 的命名空间 方式扩展 Demo 工程地址 :https://github.com/mdcc2016/swiftchainingdemo

思考 如何在实际的编程中应 用链式语法 链式代码与 RxSwift 结合 如何让 自 己的代码更更加 Swifty

扩展资料料 WWDC 2016 session 403: Swift API Design Guidelines WWDC 2015 session 408: Protocol-Oriented Programming in Swift 读 Swift 源码, 理理解 Monad

ENJOY Swift ~ Thanks!