原文:http://www.cocoachina.com/applenews/devnews/2014/0623/8923.html

Swift 事实上是支持反射的。只是功能略弱。

本文介绍主要的反射使用方法和相关类型。

 
MetaType 和 Type 语法
The metatype of a class, structure, or enumeration type is the name of that type followed by .Type. The metatype of a protocol type—not the concrete type that conforms to the protocol at runtime—is the
name of that protocol followed by .Protocol. For example, the metatype of the class type SomeClass is SomeClass.Type and the metatype of the protocol SomeProtocol is SomeProtocol.Protocol.
 
You can use the postfix self expression to access a type as a value. For example, SomeClass.self returns SomeClass itself, not an instance of SomeClass. And SomeProtocol.self returns SomeProtocol itself,
not an instance of a type that conforms to SomeProtocol at runtime.
  • metatype-type –> type.Type | type.Protocol
  • type-as-value –> type.self
 
当中 metatype-type 出如今代码中须要类型的地方, type-as-value 出如今代码中须要值、变量的地方。
 
Any.Type 类型大家能够猜下它表示什么。
 
基础定义
 
反射信息用 Mirror 类型表示,类型协议是 Reflectable,但实际看起来 Reflectable 没有不论什么作用。


  1. protocol Reflectable { 
  2.   func getMirror() -> Mirror 
  3. protocol Mirror { 
  4.   var value: Any { get } 
  5.   var valueType: Any.Type { get } 
  6.   var objectIdentifier: ObjectIdentifier? { get } 
  7.   var count: Int { get } 
  8.   subscript (i: Int) -> (String, Mirror) { get } 
  9.   var summary: String { get } 
  10.   var quickLookObject: QuickLookObject? { get } 
  11.   var disposition: MirrorDisposition { get } 
 
实际上全部类型都实现了 Reflectable。
 
Mirror 协议相关字段:
  • 1.value 相当于变量的 as Any 操作
  • 2.valueType 获得变量类型
  • 3.objectIdentifier 相当于一个 UInt 作用未知。可能是 metadata 表用到
  • 4.count 子项目个数(能够是类、结构体的成员变量。也能够是字典,数组的数据)
  • 5.subscript(Int) 訪问子项目, 和子项目的名字
  • 6.summary 相当于 description
  • 7.quickLookObject 是一个枚举,这个在 WWDC 有讲到,就是 Playground 代码右边栏的显示内容,比方常见类型,颜色。视图都能够
  • 8.disposition 表示变量类型的性质,基础类型 or 结构 or 类 or 枚举 or 索引对象 or … 例如以下

  1. enum MirrorDisposition { 
  2.   case Struct // 结构体 
  3.   case Class // 类 
  4.   case Enum // 枚举 
  5.   case Tuple // 元组 
  6.   case Aggregate // 基础类型 
  7.   case IndexContainer // 索引对象 
  8.   case KeyContainer // 键-值对象 
  9.   case MembershipContainer // 未知 
  10.   case Container // 未知 
  11.   case Optional // Type? 
  12.   var hashValue: Int { get } 
通过函数 func reflect<T>(x: T) -> Mirror 能够获得反射对象 Mirror 。它定义在 Any 上。全部类型均可用。
 
实际操作
 
.valueType 处理
Any.Type 是全部类型的元类型,所以 .valueType 属性表示类型。实际使用的时候还真是有点诡异:

  1. let mir = reflect(someVal) 
  2. swift mir.valueType { 
  3. case _ as String.Type: 
  4.     println("type = string") 
  5. case _ as Range<Int>.Type: 
  6.     println("type = range of int") 
  7. case _ as Dictionary<Int, Int>.Type: 
  8.     println("type = dict of int") 
  9. case _ as Point.Type: 
  10.     println("type = a point struct") 
  11. default: 
  12.     println("unkown type") 
  13. }     
 
或者使用 is 推断:

  1. if mir is String.Type { 
  2.     println("!!!type => String") 
is String 推断变量是否是 String 类型。而 is String.Type 这里用来推断类型是否是 String 类型。

 
subscript(Int) 处理
实測发现直接用 mir[0] 訪问偶尔会出错。或许是 beta 的原因。

  1. for r in 0..mir.count { 
  2.     let (name, subref) = mir[r] 
  3.     prtln("name: \(name)") 
  4.     // visit sub Mirror here 
通过上面的方法,基本上能够遍历大部分结构。
 
不同类型的处理
 
Struct 结构体、 Class 类
  • .count 为字段个数。
  • subscript(Int) 返回 (字段名,字段值反射 Mirror) 元组
  • summary 为 mangled name
  •  
Tuple 元组
  • .count 为元组子元素个数
  • subscript(Int) 的 name 为 “.0”, “.1” …
  •  
Aggregate 基础类型
包含数字、字符串(含 NSString)、函数、部分 Foundation 类型、 MetaType 。
 
非常奇怪一点是測试发现枚举也被反射为基础类型。

怀疑是没实现完。

  • .count 为 0
 
IndexContainer 索引对象
包含 Array<T>, T[], NSArray 等。能够通过 subscript 訪问。

  • .count 为元组子元素个数
  • subscript(Int) 的 name 为 “[0]”, “[1]” …
 
KeyContainer 键-值对象
包含 Dictionary<T, U>、NSDictionary
  • .count 为元组子元素个数
  • subscript(Int) 的 name 为 “[0]”, “[1]” … 实际訪问是 (name, (reflect(key), reflect(val)))
 
Optional Type?
仅仅包含 Type?

,不包含 Type!。

  • .count 为 0 或者 1 (相应 nil 和有值的情况)
  • subscript(Int) , name 为 “Some”
 
其它
Enum 枚举 看起来是未使用
MembershipContainer // 未知
Container // 未知
 
演示样例代码
 Gist  
 
 
 
我这里再补充几句。

由于考虑到有些编程经验不是非常丰富的朋友对于此问题可能会感觉比較迷糊。

楼主,贴了Apple官方Swift编程指南与编程手冊第791页的第二段。

事实上第一段也比較重要——

 
A metatype type refers to the type of any type, including class types, structure types, enumeration types, and protocol types.
 
一个元类型是对任一类型(包含类类型,结构类型。枚举类型以及协议类型)的类型的引用。

 
在动态编程语言中,其复合类型(比方Swift中的类类型,结构类型。枚举类型以及协议类型)一般会以某种编码方式记录在执行时内存中,然后对外接口提供每种类型的唯一签名句柄。

 
举一个非常easy的样例。大家玩过Objective-C的话。[NSString class]所返回的Class句柄就是NSString的元类型。然后,我们通过runtime提供的各种接口就能够做非常多事情。

比方:


  1. Class strClass = [NSString class]; 
  2.     NSLog(@"The class name is: %s.", class_getName(strClass)); 
上述代码将输出The class name is NSString.
 
对于像Objective-C以及Swift这样的比較动态的编程语言而言,元类型事实上就是桥接我当前静态代码与执行时类型进行交互的玩意儿。通过元类型,我除了能够查询所相应的类型的具体信息之外,甚至还能对已存在的类型进行改动。

比方改动成员属性、甚至替换成员方法的实现等。这样的功能特性在计算机编程语言中也被称作为反射--Reflection。

 

Swift中的反射的更多相关文章

  1. Swift Swift中的反射

    Swift的反射机制是基于一个叫 Mirror 的 struct 来实现的,其内部有如下属性和方法: let children: Children //对象的子节点. displayStyle: Mi ...

  2. 思考 Swift 中的 MirrorType 协议

    Swift中的反射非常有限,仅允许以只读方式访问元数据的类型子集.或许 Swift 因有严格的类型检验而不需要反射.编译时已知各种类型,便不再需要进行进一步检查或区分.然后大量的 Cocoa API ...

  3. 阿里巴巴最新开源项目 - [HandyJSON] 在Swift中优雅地处理JSON

    项目名称:HandyJSON 项目地址:https://github.com/alibaba/handyjson 背景 JSON是移动端开发常用的应用层数据交换协议.最常见的场景便是,客户端向服务端发 ...

  4. swift中的结构体和枚举

    Swift 里的结构体非常特殊. 类是面向对象编程语言中传统的结构单元.和结构体相比,Swift 的类支持实现继承,(受限的)反射,析构函数和多所有者. 既然类比结构体强大这么多,为什么还要使用结构体 ...

  5. Swift 中枚举

    Swift 中枚举高级用法及实践 字数11017 阅读479 评论0 喜欢20 title: "Swift 中枚举高级用法及实践"date: 2015-11-20tags: [AP ...

  6. swift 中关于open ,public ,fileprivate,private ,internal,修饰的说明

    关于 swift 中的open ,public ,fileprivate,private, internal的区别 以下按照修饰关键字的访问约束范围 从约束的限定范围大到小的排序进行说明 open,p ...

  7. 详解C#中的反射

    反射(Reflection) 2008年01月02日 星期三 11:21 两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的生理情况.这是如何做到的呢?B超是B ...

  8. Swift中的可选链与内存管理(干货系列)

    干货之前:补充一下可选链(optional chain) class A { var p: B? } class B { var p: C? } class C { func cm() -> S ...

  9. 在Swift中实现单例方法

    在写Swift的单例方法之前可以温习一下Objective-C中单例的写法: + (instancetype)sharedSingleton{ static id instance; static d ...

随机推荐

  1. C# 元数据描述

    元数据概述:元数据是一种二进制信息,用以对存储在公共语言运行库可移植可执行文件 (PE) 文件或存储在内存中的程序进行描述.将您的代码编译为 PE 文件时,便会将元数据插入到该文件的一部分中,而将代码 ...

  2. Maven deploy 部署 jar+pom 到 Nexus 私服

    经验之谈 工作中,我们常常需要将基础架构部门的 jar 包提供给业务部门的同事使用,那么,需要将 jar 包 deploy 到 nexus 私服上,网上资料不是很多,这里说一下具体细节. 首先,是打 ...

  3. windows下mysql-5.7.18安装

    在官网下载了mysql压缩包,按照官网:https://dev.mysql.com/doc/refman/5.7/en/windows-installation.html的教程安装,但是出错,后面根据 ...

  4. 【手记】sql报“聚合或其他set操作消除了null值”处理

    这个警告在常规场景中没什么影响,但如果是用excel跑SQL,它会因为该警告阻止你的后续操作~事实上excel执行sql限制多多,需要更多的奇技淫巧,之前我就写过一篇.言归正传,要解决这个警告,一种当 ...

  5. vb.bet 控件

    TextBox1.BackColor = Color.White'设置控件的背景色(白色) TextBox1.BackColor = Color.Yellow'设置控件的背景色(黃色) TextBox ...

  6. C#实现RSA加密与解密、签名与认证(转)

    一.RSA简介 RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响力 ...

  7. IDEA乱码解决

    设置中encoding都指定为utf-8,完全没用. 最终在java编译的地方找到了解决办法

  8. 【Java深入研究】4、fail-fast机制

    在JDK的Collection中我们时常会看到类似于这样的话: 例如,ArrayList: 注意,迭代器的快速失败行为无法得到保证,因为一般来说,不可能对是否出现不同步并发修改做出任何硬性保证.快速失 ...

  9. 【PostMan】1、Postman 发送json格式请求

    Postman 是一个用来测试Web API的Chrome 外挂软件,可由google store 免费取得并安装于Chrome里,对于有在开发Web API的开发者相当有用,省掉不少写测试页面呼叫的 ...

  10. 最长子串(FZU2128)

    最长子串 Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status  ...