swift的类型系统及类型(内存)信息获取:接口、编译运行时、反射、内存布局
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的类型系统及类型(内存)信息获取:接口、编译运行时、反射、内存布局的更多相关文章
- Linux下tomcat运行时jvm内存分配
tomcat运行时jvm内存分配 ⑴开发环境下在myeclipse中配置-Xms256m -Xmx512m -XX:NewSize=64m -XX:MaxNewSize=128m -XX:PermSi ...
- C#实现进程内存信息获取
using System.Collections.Generic;using System.Runtime.InteropServices;using System;using System.Diag ...
- APP信息获取接口
https://itunes.apple.com/lookup?id=APPID&callback=2 http://myapp.com/cgi-bin/mapp/mapp_info?type ...
- 使用Visual VM 查看linux中tomcat运行时JVM内存
前言:在生产环境中经常发生服务器内存溢出,假死或者线程死锁等异常,导致服务不可用.我们经常使用的解决方法是通过分析错误日记,然后去寻找代码到底哪里出现了问题,这样的方式也许会奏效,但是排查起来耗费时间 ...
- linux下实现CPU使用率和内存使用率获取方法
想获取一下目标机运行时linux系统的硬件占用情况,写了这几个小程序,以后直接用了. 方法就是读取proc下的文件来获取了. cpu使用率: /proc/stat ,内存使用情况: /p ...
- Linux入门:常用命令:查看硬盘、分区、CPU、内存信息
查看硬盘信息 $df -lh #查看所有硬盘的使用状 $du -sh /etc #查看etc目录大小 #获得文件大小很方便,主要是目录 外部系统挂载 $mount ...
- .NET/C# 如何获取当前进程的 CPU 和内存占用?如何获取全局 CPU 和内存占用?
原文:.NET/C# 如何获取当前进程的 CPU 和内存占用?如何获取全局 CPU 和内存占用? 都知道可以在任务管理器中查看进程的 CPU 和内存占用,那么如何通过 .NET 编写代码的方式来获取到 ...
- JVM的内存管理、对象的生命周期、内存泄漏
1 JVM内存 分为“堆”.“栈”和“方法区”三个区域,分别用于存储不同的数据 1.1 堆 JVM在其内存空间开辟一个称为”堆”的存储空间,这部分空间用于存储使用new关键字所创建的对象. 1.2 栈 ...
- perl学习之:编译、执行与内存关系(转)
1.所谓在编译期间分配空间指的是静态分配空间(相对于用new动态申请空间),如全局变量或静态变量(包括一些复杂类型的 常量),它们所需要的空间大小可以明确计算出来,并且不会再改变,因此它们可以直接存放 ...
随机推荐
- ASP.Net ListBox DropdownList 不同条目设置背景色和字体颜色( 转· 载 )
ASP.Net ListBox DropdownList 不同条目设置背景色和字体颜色 2009-09-30 来自:真有意思 [ZU14.CN] 字体大小:[大 中 小] 摘要:在HTML展现页面 ...
- Oracle超过连接数(ORA-12520)
原因是超过了连接数,最有效的处理方法是关闭em服务,停止em服务,改成禁用. show parameter processes; --查看允许连接情况 select count(*) from v$ ...
- 使用C#利用cmd来调用java jar包获取其中的数据
其实也很简单,就是在C#中构建一个Process,启动jar包,并且给jar包传递参数 因为我并没有怎么学过JAVA,所以只写了个很小的Demo,就是根据传入的参数获取对应的数据 以下是JAVA De ...
- 零基础学C#算法(零基础学算法——C#版)
今天本人正在看算法方面的书.作为高中数学忘得差不多的渣渣,实在无力.无奈找了本书,c语言写的,哎.我就把其中代码翻译成C#版好了.此坑能否填平,看我耐性和网络支持条件吧.有生之年能看完的话我会把整个项 ...
- 【转】分布式环境下5种session处理策略(大型网站技术架构:核心原理与案例分析 里面的方案)
前言 在搭建完集群环境后,不得不考虑的一个问题就是用户访问产生的session如何处理.如果不做任何处理的话,用户将出现频繁登录的现象,比如集群中存在A.B两台服务器,用户在第一次访问网站时,Ngin ...
- 解决:maven 项目添加 pom 的 oracle 依赖
前言:maven 项目需要在 pom 文件中添加 oracle 的依赖. 如果报错:报找不到驱动:java.lang.ClassNotFoundException: oracle.jdbc.drive ...
- JAVA中的泛型(Generic)
Java泛型(Generic)简介 泛型是jdk1.5版本以后推出来的,表示类型参数化,让java能更具有动态性一些,让类型能变成参数传递. 要我自己感觉的话,泛型本身没啥用,跟反射在一起用,就体现出 ...
- 关于JS中闭包的问题
一直以来,我都以为我已经懂了JavaScript中闭包的概念,直到有一次小伙伴突然问我这个概念的时候,我才发现我根本不知道该怎来么跟他来讲述这个概念. 那时候我就知道我是自我欺骗,打肿脸充胖子了. 所 ...
- ES6入门——类的概念
1.Class的基本用法 概述 JavaScript语言的传统方式是通过构造函数,定义并生成新对象.这种写法和传统的面向对象语言差异很大,下面是一个例子: function Point(x, y) { ...
- 7 Recursive AutoEncoder结构递归自编码器(tensorflow)不能调用GPU进行计算的问题(非机器配置,而是网络结构的问题)
一.源代码下载 代码最初来源于Github:https://github.com/vijayvee/Recursive-neural-networks-TensorFlow,代码介绍如下:“This ...