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动态申请空间),如全局变量或静态变量(包括一些复杂类型的 常量),它们所需要的空间大小可以明确计算出来,并且不会再改变,因此它们可以直接存放 ...
随机推荐
- mssql名词解释
因为自己在sql这部分不强,有时候碰到一些名词我都不知道什么意思,所以在这里记录下,方便以后查看 1.NOT FOR REPLICATION 控制约束.标志和触发器 在所有IDENTITY属性字段 ...
- Java学习--Cookie 和session
- leetcode实践:找出两个有序数组的中位数
题目 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 代码实现 package com. ...
- 使用postman测试接口时需要先登录怎么办
原文 1.在浏览器上先登录,登录成功后获取cookie: 2.接着打开postman:
- redis 批量删除key
redis-cli -h -p keys -p del s
- 【学习笔记】--- 老男孩学Python,day7 python中is 和 == 的区别 encode decode
is比较的是id(内存地址)是不是一样,==比较的是值是不是一样 Python中,万物皆对象!万物皆对象!万物皆对象!(很重要,重复3遍) 每个对象包含3个属性,id,type,value id就是对 ...
- Spring是什么、spring容器、Spring三大核心思想DI(依赖注入)、IOC(控制反转)、AOP(面向切面编程)
1.Spring (1)Spring是什么? 是一个轻量级的.用来简化企业级应用开发的开发框架. 注: a.简化开发: Spring对常用的api做了简化,比如,使用Spring jdbc来访问数据库 ...
- elasticsearch 多列 聚合(sql group by)
文档数据格式 {"zone_id":"1","user_id":"100008","try_deliver_t ...
- [转]XSS的原理分析与解剖:第四章(编码与绕过)
0×01前言 很抱歉,这第四章被我推了几个月,今天是元旦难得有空,就把第四章写下.我先把主要使用的编码说下,介绍完会说下绕过. 本文建议与<杂谈如何绕过WAF>一同阅读. 0×02 URL ...
- 【node】用koa搭建一个增删改服务(一)
前文,vue分类里有一个日志demo的练习,这篇文章就是介绍针对日志demo的服务是怎么写的 一.koa搭建项目 1. npm init 2. npm install koa 二.建数据库 下面是项目 ...