swift是静态语言,没有在运行时保存类型的结构信息(isa、class)。

一、self、Self、Type、typeof

extension Collection where Self.Element == UInt8, Self.Index == Int

public static func isValueTypeOrSubtype(_ value: Any) -> Bool {

return value is Self

}

static func _transform(dict: [String: Any]) -> _ExtendCustomModelType? {

var instance: Self

if let _nsType = Self.self as? NSObject.Type {

instance = _nsType.createInstance() as! Self

} else {

instance = Self.init()

}

_transform(dict: dict, to: &instance)

instance.didFinishMapping()

return instance

}

let someInstance: SomeBaseClass = SomeSubClass()

/*                      |                |

compileTime       Runtime

|                |

To extract, use:       .self          type(of)

  • metatype-type –>type.Type|type.Protocol
  • type-as-value –>type.self

1. In a protocol, it refers to the type that conforms to the protocol in any particular use. In Equatable, for example, it's used to require that the two values being compared are of the same type. It's something like a generic type parameter that you don't have to put between the <…> because it's deduced from the context of its use.

2. In a class/static method, it can be used as the return type, to indicate that the return type is the type of the class to which the method was sent, rather than the class in which the method is declared. It's similar to 'instancetype' in Obj-C.

Self相当于oc中的instance

是什么

相信大家都知道self这个关键字的具体作用,它跟OC里的self基本一样。但是对于Self来说...(WTF,这是什么东西)

当你用错Self的时候编译器会这样提示

'Self' is only available in a protocol or as the result of a method in a class

分割开来的话就是两个意思

1.Self可以用于协议(protocol)中限制相关的类型

2.Self可以用于类(Class)中来充当方法的返回值类型

二、内存布局

unsafeMutableRawPointer

三、反射

let mirror = Mirror(reflecting: self)

for child in mirror.children {

print("Property name:", child.label)

print("Property value:", child.value)

}

https://www.swiftbysundell.com/posts/reflection-in-swift

四、内存信息获取

Looking up the elements in structs, classes, and enums is currently quite complex. Much of this complexity is due to the lack of a direct reference between these types and the field descriptors which contain the information about a type’s fields. A helper function called swift_getFieldAt searches for the appropriate field descriptor for a given type. This whole function should go away once we add that direct reference, but in the meantime it provides an interesting look at how the runtime code is able to use the language’s metadata to look up type information.

https://mjtsai.com/blog/2018/09/28/how-swifts-mirror-works/

swift的类型系统及类型(内存)信息获取:接口、编译运行时、反射、内存布局的更多相关文章

  1. Linux下tomcat运行时jvm内存分配

    tomcat运行时jvm内存分配 ⑴开发环境下在myeclipse中配置-Xms256m -Xmx512m -XX:NewSize=64m -XX:MaxNewSize=128m -XX:PermSi ...

  2. C#实现进程内存信息获取

    using System.Collections.Generic;using System.Runtime.InteropServices;using System;using System.Diag ...

  3. APP信息获取接口

    https://itunes.apple.com/lookup?id=APPID&callback=2 http://myapp.com/cgi-bin/mapp/mapp_info?type ...

  4. 使用Visual VM 查看linux中tomcat运行时JVM内存

    前言:在生产环境中经常发生服务器内存溢出,假死或者线程死锁等异常,导致服务不可用.我们经常使用的解决方法是通过分析错误日记,然后去寻找代码到底哪里出现了问题,这样的方式也许会奏效,但是排查起来耗费时间 ...

  5. linux下实现CPU使用率和内存使用率获取方法

    想获取一下目标机运行时linux系统的硬件占用情况,写了这几个小程序,以后直接用了. 方法就是读取proc下的文件来获取了. cpu使用率:    /proc/stat ,内存使用情况:     /p ...

  6. Linux入门:常用命令:查看硬盘、分区、CPU、内存信息

    查看硬盘信息 $df -lh    #查看所有硬盘的使用状 $du -sh /etc   #查看etc目录大小 #获得文件大小很方便,主要是目录 外部系统挂载 $mount               ...

  7. .NET/C# 如何获取当前进程的 CPU 和内存占用?如何获取全局 CPU 和内存占用?

    原文:.NET/C# 如何获取当前进程的 CPU 和内存占用?如何获取全局 CPU 和内存占用? 都知道可以在任务管理器中查看进程的 CPU 和内存占用,那么如何通过 .NET 编写代码的方式来获取到 ...

  8. JVM的内存管理、对象的生命周期、内存泄漏

    1 JVM内存 分为“堆”.“栈”和“方法区”三个区域,分别用于存储不同的数据 1.1 堆 JVM在其内存空间开辟一个称为”堆”的存储空间,这部分空间用于存储使用new关键字所创建的对象. 1.2 栈 ...

  9. perl学习之:编译、执行与内存关系(转)

    1.所谓在编译期间分配空间指的是静态分配空间(相对于用new动态申请空间),如全局变量或静态变量(包括一些复杂类型的 常量),它们所需要的空间大小可以明确计算出来,并且不会再改变,因此它们可以直接存放 ...

随机推荐

  1. SVN trunk(主线) branch(分支) tag(标记) 用法详解和详细操作步骤

    使用场景: 假如你的项目(这里指的是手机客户端项目)的某个版本(例如1.0版本)已经完成开发.测试并已经上线了,接下来接到新的需求,新需求的开发需要修改多个文件中的代码,当需求已经开始开发一段时间的时 ...

  2. fzu 2139 久违的月赛之二

    Problem 2139 久违的月赛之二 Accept: 42    Submit: 106Time Limit: 1000 mSec    Memory Limit : 32768 KB Probl ...

  3. HDU 2669 Romantic 扩展欧几里德---->解不定方程

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. Maven配置私服仓库

    首先就是,最基本的打开maven的配置文件,上面是我自己的习惯,多留一个以备不坏 打开setting配置文件,来修改路径(本人不习惯将所有软件放在C盘,一般都是单独存放盘) 接下来就是公司给你的账户和 ...

  5. vue 如何将输入框的输入自动小写转大写(使用 v-model 时采取另一种方式)

    前言:小写转大写,可以用过滤器实现,但当使用 v-model 时就不行了,这里有解决方案.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9289205.html ...

  6. Code Signal_10分钟挑战题_constructArray

    完成时间3分06秒 Given an integer size, return an array containing each integer from 1 to size in the follo ...

  7. python学习之老男孩python全栈第九期_数据库day002 -- 作业 (数据库为day001创建的数据库)

    1.自行创建测试数据 对score表进行优化: 添加数据: 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 为了方便做题,额外增加几条数据 查询: 3.查询平均成绩大于60分的同学的学号和 ...

  8. 文档类型、DOCTYPE切换和浏览器模式

    DTD(文档类型定义)是一种机器可读的规则,它们定义XML或HTML的特定版本中允许有什么.不允许有什么.在解析网页时,浏览器将使用这些规则检查页面的有效性并且采取相应的措施.浏览器通过分析页面的DO ...

  9. python之切片

    1.为什么要学习切片[切片是解决何种问题的?] (2)对于含有元素很少的list,我们遍历其前三个元素,通常可以这样来实现: #遍历list,取其中索引为[0].[1].[2]的元素: L = ['X ...

  10. 破解 jeb 2.3.7 demo

    前言 使用的技术和上文的一样. mips 版本的修改版 修改版: https://gitee.com/hac425/jeb-mips 正文 安卓版 jeb-2.3.7.201710262129-JEB ...