• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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/


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
当AS3遇见Swift(一)发布时间:2022-07-13
下一篇:
Swift3.0语言教程使用路径字符串发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap