swift中的內存管理,涉及引用計數、弱引用、強引用、循環引用、Runtime是什么樣子的呢?
內存管理 - 強引用
在swift中也是使用ARC來追蹤和管理內存的,下面我們通過一個案例來進行分析
class CJLTeacher {
var age: Int = 18
var name: String = "CJL"
}
var t = CJLTeacher()
var t1 = t
var t2 = t
-
查看t的內存情況,為什么其中的refCounts是0x0000000600000003?
image
在分析類時(參考這篇文章Swift-進階 02:類、對象、屬性)有這么一個類HeapObject
,下面繼續通過這個類來分析t的引用計數
- 分析源碼
HeapObject -> InlineRefCounts
struct HeapObject {
HeapMetadata const *metadata;
SWIFT_HEAPOBJECT_NON_OBJC_MEMBERS;
...
}
??
#define SWIFT_HEAPOBJECT_NON_OBJC_MEMBERS \
InlineRefCounts refCounts
- 進入
InlineRefCounts
定義,是RefCounts
類型的別名,而RefCounts
是模板類,真正決定的是傳入的類型InlineRefCountBits
typedef RefCounts<InlineRefCountBits> InlineRefCounts;
??
template <typename RefCountBits>
class RefCounts {
std::atomic<RefCountBits> refCounts;
...
}
- 分析
InlineRefCountBits
,是RefCountBitsT
類的別名
typedef RefCountBitsT<RefCountIsInline> InlineRefCountBits;
- 分析
RefCountBitsT
,有bits
屬性
template <RefCountInlinedness refcountIsInline>
class RefCountBitsT {
...
typedef typename RefCountBitsInt<refcountIsInline, sizeof(void*)>::Type
BitsType;
...
BitsType bits;
...
}
??
template <>
struct RefCountBitsInt<RefCountNotInline, 4> {
//類型
typedef uint64_t Type;
typedef int64_t SignedType;
};
其中bits
其實質是將RefCountBitsInt
中的type屬性取了一個別名,所以bits的真正類型是uint64_t
即64
位整型數組
然后來繼續分析swift中對象創建的底層方法swift_allocObject
- 分析初始化源碼
swift_allocObject
static HeapObject *_swift_allocObject_(HeapMetadata const *metadata,
size_t requiredSize,
size_t requiredAlignmentMask) {
...
new (object) HeapObject(metadata);
...
}
??
<!--構造函數-->
constexpr HeapObject(HeapMetadata const *newMetadata)
: metadata(newMetadata)
, refCounts(InlineRefCounts::Initialized)
{ }
- 進入
Initialized
定義,是一個枚舉,其對應的refCounts
方法中,
enum Initialized_t { Initialized };
//對應的RefCounts方法
// Refcount of a new object is 1.
constexpr RefCounts(Initialized_t)
: refCounts(RefCountBits(0, 1)) {}
從這里看出真正干事的是RefCountBits
- 進入
RefCountBits
定義,也是一個模板定義
template <typename RefCountBits>
class RefCounts {
std::atomic<RefCountBits> refCounts;
...
}
所以真正的初始化地方是下面這個,實際上是做了一個位域
操作,根據的是Offsets
LLVM_ATTRIBUTE_ALWAYS_INLINE
constexpr
RefCountBitsT(uint32_t strongExtraCount, uint32_t unownedCount)
: bits((BitsType(strongExtraCount) << Offsets::StrongExtraRefCountShift) |
(BitsType(1) << Offsets::PureSwiftDeallocShift) |
(BitsType(unownedCount) << Offsets::UnownedRefCountShift))
{ }
分析RefCountsBit
的結構,如下所示,
isImmortal(0)
UnownedRefCount(1-31): unowned的引用計數
isDeinitingMask(32):是否進行釋放操作
StrongExtraRefCount(33-62): 強引用計數
UseSlowRC(63)
重點關注UnownedRefCount
和StrongExtraRefCount
-
將t的
refCounts
用二進制展示,其中強引用計數為3image
分析SIL代碼
-
當只有t實例變量時
image 當有t + t1時,查看是否有
strong_retain
操作
//SIL中的main
alloc_global @main.t1 : main.CJLTeacher // id: %8
%9 = global_addr @main.t1 : main.CJLTeacher : $*CJLTeacher // user: %11
%10 = begin_access [read] [dynamic] %3 : $*CJLTeacher // users: %12, %11
copy_addr %10 to [initialization] %9 : $*CJLTeacher // id: %11
//其中copy_addr等價于
- %new = load s*LGTeacher
- strong_retain %new
- store %new to %9
SIL官方文檔中關于copy_addr
的解釋如下
- 其中的
strong_retain
對應的就是swift_retain
,其內部是一個宏定義,內部是_swift_retain_
,其實現是對object
的引用計數作+1
操作
//內部是一個宏定義
HeapObject *swift::swift_retain(HeapObject *object) {
CALL_IMPL(swift_retain, (object));
}
??
//本質調用的就是 _swift_retain_
static HeapObject *_swift_retain_(HeapObject *object) {
SWIFT_RT_TRACK_INVOCATION(object, swift_retain);
if (isValidPointerForNativeRetain(object))
object->refCounts.increment(1);
return object;
}
??
void increment(uint32_t inc = 1) {
auto oldbits = refCounts.load(SWIFT_MEMORY_ORDER_CONSUME);
// constant propagation will remove this in swift_retain, it should only
// be present in swift_retain_n
if (inc != 1 && oldbits.isImmortal(true)) {
return;
}
//64位bits
RefCountBits newbits;
do {
newbits = oldbits;
bool fast = newbits.incrementStrongExtraRefCount(inc);
if (SWIFT_UNLIKELY(!fast)) {
if (oldbits.isImmortal(false))
return;
return incrementSlow(oldbits, inc);
}
} while (!refCounts.compare_exchange_weak(oldbits, newbits,
std::memory_order_relaxed));
}
- 回退到
HeapObject
,從InlineRefCounts
進入,其中是c++中的模板定義,是為了更好的抽象,在其中查找bits
(即decrementStrongExtraRefCount
方法)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE
bool incrementStrongExtraRefCount(uint32_t inc) {
// This deliberately overflows into the UseSlowRC field.
// 對inc做強制類型轉換為 BitsType
// 其中 BitsType(inc) << Offsets::StrongExtraRefCountShift 等價于 1<<33位,16進制為 0x200000000
//這里的 bits += 0x200000000,將對應的33-63轉換為10進制,為
bits += BitsType(inc) << Offsets::StrongExtraRefCountShift;
return (SignedBitsType(bits) >= 0);
}
例如以t
的refCounts
為例(其中62-33位是strongCount
,每次增加強引用計數
增加都是在33-62位上增加的,固定的增量為1左移33位
,即0x200000000
)
只有
t
時的refCounts
是 0x0000000200000003t + t1
時的refCounts
是 0x0000000400000003 = 0x0000000200000003 + 0x200000000t + t1 + t2
時的refCounts
是 0x0000000600000003 = 0x0000000400000003 + 0x200000000-
針對上面的例子,可以通過
CFGetRetainCOunt
獲取引用計數,發現依次是 2、3、4,默認多了一個1image -
如果將t、t1、t2放入函數中,還會再次retain一次
image
為什么是0x200000000
?
因為1左移33位,其中4位為一組,計算成16進制,剩余的33-32位0x10
,轉換為10進制為2
。其實際增加引用技術就是1
swift與OC強引用計數對比
OC
中創建實例對象時為0
swift
中創建實例對象時默認為1
內存管理 - 弱引用
以下面為例:
class CJLTeacher {
var age: Int = 18
var name: String = "CJL"
var stu: CJLStudent?
}
class CJLStudent {
var age = 20
var teacher: CJLTeacher?
}
func test(){
var t = CJLTeacher()
weak var t1 = t
}
-
查看
t
的引用計數變化image 弱引用聲明的變量是一個
可選值
,因為在程序運行過程中是允許將當前變量設置為nil
的-
在t1處加斷點,查看匯編
image 查看
swift_weakInit
函數,這個函數是由WeakReference
來調用的,相當于weak
字段在編譯器聲明過程中就自定義了一個WeakReference
的對象,其目的在于管理弱引用
WeakReference *swift::swift_weakInit(WeakReference *ref, HeapObject *value) {
ref->nativeInit(value);
return ref;
}
- 進入
nativeInit
void nativeInit(HeapObject *object) {
auto side = object ? object->refCounts.formWeakReference() : nullptr;
nativeValue.store(WeakReferenceBits(side), std::memory_order_relaxed);
}
- 進入
formWeakReference
,創建sideTable,
template <>
HeapObjectSideTableEntry* RefCounts<InlineRefCountBits>::formWeakReference()
{
//創建 sideTable
auto side = allocateSideTable(true);
if (side)
// 如果創建成功,則增加弱引用
return side->incrementWeak();
else
return nullptr;
}
- 進入
allocateSideTable
template <>
HeapObjectSideTableEntry* RefCounts<InlineRefCountBits>::allocateSideTable(bool failIfDeiniting)
{
// 1、先拿到原本的引用計數
auto oldbits = refCounts.load(SWIFT_MEMORY_ORDER_CONSUME);
// Preflight failures before allocating a new side table.
if (oldbits.hasSideTable()) {
// Already have a side table. Return it.
return oldbits.getSideTable();
}
else if (failIfDeiniting && oldbits.getIsDeiniting()) {
// Already past the start of deinit. Do nothing.
return nullptr;
}
// Preflight passed. Allocate a side table.
// FIXME: custom side table allocator
//2、創建sideTable
HeapObjectSideTableEntry *side = new HeapObjectSideTableEntry(getHeapObject());
// 3、將創建的地址給到InlineRefCountBits
auto newbits = InlineRefCountBits(side);
do {
if (oldbits.hasSideTable()) {
// Already have a side table. Return it and delete ours.
// Read before delete to streamline barriers.
auto result = oldbits.getSideTable();
delete side;
return result;
}
else if (failIfDeiniting && oldbits.getIsDeiniting()) {
// Already past the start of deinit. Do nothing.
return nullptr;
}
side->initRefCounts(oldbits);
} while (! refCounts.compare_exchange_weak(oldbits, newbits,
std::memory_order_release,
std::memory_order_relaxed));
return side;
}
1、先拿到原本的引用計數
2、創建
sideTable
-
3、將創建的sideTable地址給
InlineRefCountBits
,并查看其初始化方法,根據sideTable
地址作了偏移操作并存儲到內存,相當于將sideTable直接存儲到了64位的變量中image所以上面的
0xc000000020809a6c
是HeapObjectSideTableEntry
實例對象的內存地址,即散列表的地址
(除去63、62位)-
查看
HeapObjectSideTableEntry
定義,其中有object
對象、refCounts
image -
進入
SideTableRefCounts
,同InlineRefCounts
類似,實際做事的是SideTableRefCountBits
,繼承自RefCountBitsT
(存的是uint64_t類型的64位的信息),還有一個uint32_t
的weakBits
,即32位的位域信息- 64位 用于記錄 原有引用計數
- 32位 用于記錄 弱引用計數
image -
以0xc000000020809a6c
為例,將62、63位清零,變成0x20809A6C
,然后左移3
位(即InlineRefCountBits
初始化方法),變成0x10404D360
即HeapObjectSideTableEntry對象地址,即散列表地址
,然后通過x/8g
讀取
問題:如果此時再加一個強引用t2
查看其refCounts,t2是執行了strong_retain
的
-
源碼查看
_swift_retain_ -> increment -> incrementSlow -> incrementStrong
image
總結
對于HeapObject
來說,其refCounts
有兩種:
- 無弱引用:strongCount + unownedCount
- 有弱引用:object + xxx + (strongCount + unownedCount) + weakCount
HeapObject {
InlineRefCountBit {strong count + unowned count }
HeapObjectSideTableEntry{
HeapObject *object
xxx
strong Count + unowned Count(uint64_t)//64位
weak count(uint32_t)//32位
}
}
內存管理 - 循環引用
主要是研究閉包捕獲外部變量
,以下面代碼為例
var age = 10
let clourse = {
age += 1
}
clourse()
print(age)
<!--打印結果-->
11
從輸出結果中可以看出:閉包內部對變量的修改將會改變外部原始變量的值
,主要原因是閉包會捕獲外部變量,這個與OC中的block是一致的
- 定義一個類,在test函數作用域消失后,會執行init
class CJLTeacher {
var age = 18
//反初始化器(當前實例對象即將被回收)
deinit {
print("CJLTeacher deinit")
}
}
func test(){
var t = CJLTeacher()
}
test()
<!--打印結果-->
CJLTeacher deinit
- 修改例子,通過閉包修改其屬性值
class CJLTeacher {
var age = 18
//反初始化器(當前實例對象即將被回收)
deinit {
print("CJLTeacher deinit")
}
}
var t = CJLTeacher()
let clourse = {
t.age += 1
}
clourse()
<!--打印結果-->
11
- 【修改1】將上面例子修改為如下,其中閉包是否對t有強引用?
class CJLTeacher {
var age = 18
deinit {
print("CJLTeacher deinit")
}
}
func test(){
var t = CJLTeacher()
let clourse = {
t.age += 1
}
clourse()
}
test()
<!--運行結果-->
CJLTeacher deinit
運行結果發現,閉包對 t 并沒有強引用
- 【修改2】繼續修改例子為如下,是否有強引用?
class CJLTeacher {
var age = 18
var completionBlock: (() ->())?
deinit {
print("CJLTeacher deinit")
}
}
func test(){
var t = CJLTeacher()
t.completionBlock = {
t.age += 1
}
}
test()
從運行結果發現,沒有執行deinit方法,即沒有打印CJLTeacher deinit
,所以這里有循環引用
循環引用解決方法
有兩種方式可以解決swift中的循環引用
- 【方式一】使用
weak
修飾閉包傳入的參數,其中參數的類型是optional
func test(){
var t = CJLTeacher()
t.completionBlock = { [weak t] in
t?.age += 1
}
}
- 【方式二】使用
unowned
修飾閉包參數,與weak
的區別在于unowned
不允許被設置為nil,即總是假定有值
的
func test(){
var t = CJLTeacher()
t.completionBlock = { [unowned t] in
t.age += 1
}
}
捕獲列表
[weak t] / [unowned t]
在swift中被稱為捕獲列表
定義在參數列表之前
【書寫方式】捕獲列表被寫成用逗號括起來的表達式列表,并用方括號括起來
如果使用捕獲列表,則即使省略參數名稱、參數類型和返回類型,也必須使用
in
關鍵字[weak t]
就是取t的弱引用對象 類似weakself
請問下面代碼的clourse()
調用后,輸出的結果是什么?
func test(){
var age = 0
var height = 0.0
//將變量age用來初始化捕獲列表中的常量age,即將0給了閉包中的age(值拷貝)
let clourse = {[age] in
print(age)
print(height)
}
age = 10
height = 1.85
clourse()
}
<!--打印結果-->
0
1.85
所以從結果中可以得出:對于捕獲列表
中的每個常量
,閉包會利用周圍范圍內具有相同名稱的常量/變量,來初始化捕獲列表中定義的常量。有以下幾點說明:
捕獲列表中的常量是
值拷貝
,而不是引用捕獲列表中的常量的相當于復制了變量age的值
捕獲列表中的常量是只讀的,即不可修改
swift中Runtime探索
請問下面代碼,會打印方法和屬性嗎?
class CJLTeacher {
var age: Int = 18
func teach(){
print("teach")
}
}
let t = CJLTeacher()
func test(){
var methodCount: UInt32 = 0
let methodList = class_copyMethodList(CJLTeacher.self, &methodCount)
for i in 0..<numericCast(methodCount) {
if let method = methodList?[i]{
let methodName = method_getName(method)
print("方法列表:\(methodName)")
}else{
print("not found method")
}
}
var count: UInt32 = 0
let proList = class_copyPropertyList(CJLTeacher.self, &count)
for i in 0..<numericCast(count) {
if let property = proList?[i]{
let propertyName = property_getName(property)
print("屬性成員屬性:\(property)")
}else{
print("沒有找到你要的屬性")
}
}
print("test run")
}
test()
運行結果如下,發現并沒有打印方法和屬性
-
【嘗試1】如果給屬性 和 方法 都加上 @objc,可以打印嗎?
image從運行結果看,是可以打印,但是由于類并沒有暴露給OC,所以OC是無法使用的,這樣做是沒有意義的
-
【嘗試2】如果swift的類
繼承NSObject
,沒有@objc修飾屬性和方法,是否可以打印全部屬性+方法?image從結果發現獲取的只有
init
方法,主要是因為在swift.h
文件中暴露出來的只有init
方法 -
如果想讓OC能使用,必須類
繼承NSObject
+@objc修飾
屬性、方法image -
如果
去掉@objc
修飾屬性,將方法改成dynamic
修飾,是否可以打印方法?image從結果可以看出,依舊不能被OC獲取到,需要修改為
@objc dynamic
修飾image
結論
對于純swift類來說,沒有
動態特性dynamic
(因為swift
是靜態語言
),方法和屬性不加任何修飾符的情況下,已經不具備runtime
特性,此時的方法調度,依舊是函數表調度即V_Table調度
對于純swift類,方法和屬性添加
@objc
標識的情況下,可以通過runtime API獲取到,但是在OC中是無法進行調度的,原因是因為swift.h
文件中沒有swift類的聲明對于
繼承自NSObject
類來說,如果想要動態的獲取當前屬性+方法,必須在其聲明前
添加@objc
關鍵字,如果想要使用方法交換
,還必須在屬性+方法前
添加dynamic
關鍵字,否則當前屬性+方法只是暴露給OC使用,而不具備任何動態特性
objc源碼驗證
(由于xcode12.2暫時無法運行objc源碼,下列驗證圖片僅供參考)
-
進入
class_copyMethodList
源碼,斷住,查看此時的cls
,其中data()
存儲類的信息image -
進入
data
,打印bits、superclassimage從這里可以得出
swift
中有默認基類,即_SwiftObject
-
打印methods
image swift源碼中搜索
_SwiftObject
,繼承自NSObject
,在內存結構上與OC基本類似的
#if __has_attribute(objc_root_class)
__attribute__((__objc_root_class__))
#endif
SWIFT_RUNTIME_EXPORT @interface SwiftObject<NSObject> {
@private
Class isa;
//refCounts
SWIFT_HEAPOBJECT_NON_OBJC_MEMBERS;
}
-
在之前的文章中Swift-進階 02:類、對象、屬性,其中
TargetAnyClassMetadata
繼承自TargetHeapMetaData
,其中只有一個屬性kind,TargetAnyClassMetadata有四個屬性:isa、superclass、cacheData、data
即bitsimage所以swift為了保留和OC交互,其在底層存儲的數據結構上和OC是一致的
objc源碼中搜索
swift_class_t
,繼承自objc_class
,保留了OC模板類的4個屬性,其次才是自己的屬性
struct swift_class_t : objc_class {
uint32_t flags;
uint32_t instanceAddressOffset;
uint32_t instanceSize;
uint16_t instanceAlignMask;
uint16_t reserved;
uint32_t classSize;
uint32_t classAddressOffset;
void *description;
// ...
void *baseAddress() {
return (void *)((uint8_t *)this - classAddressOffset);
}
};
問題:為什么繼承NSObject?:必須通過NSObject聲明,來幫助編譯器判斷,當前類是一個和OC交互的類
元類型、AnyClass、Self
AnyObject
-
AnyObject
:代表任意類的instance、類的類型、僅類遵守的協議
class CJLTeacher: NSObject {
var age: Int = 18
}
var t = CJLTeacher()
//此時代表的就是當前CJLTeacher的實例對象
var t1: AnyObject = t
//此時代表的是CJLTeacher這個類的類型
var t2: AnyObject = CJLTeacher.self
//繼承自AnyObject,表示JSONMap協議只有類才可以遵守
protocol JSONMap: AnyObject { }
例如如果是結構體遵守協議,會報錯
需要將struct修改成class
//繼承自AnyObject,表示JSONMap協議只有類才可以遵守
protocol JSONMap: AnyObject {
}
class CJLJSONMap: JSONMap {
}
Any
-
Any
:代表任意類型
,包括 function類型 或者Optional類型,可以理解為AnyObject
是Any
的子集
//如果使用AnyObject會報錯,而Any不會
var array: [Any] = [1, "cjl", "", true]
AnyClass
-
AnyClass
:代表任意實例的類型
,類型是AnyObject.Type
- 查看定義,是
public typealias AnyClass = AnyObject.Type
- 查看定義,是
T.self & T.Type
-
T.self
:如果T是
實例
對象,返回的就是它本身
如果
T
是類,那么返回的是MetaData
T.Type
:一種類型
,T.self
是T.Type
類型
//此時的self類型是 CJLTeacher.Type
var t = CJLTeacher.self
打印結果如下
- 查看t1、t2存儲的是什么?
var t = CJLTeacher()
//實例對象地址:實例對象.self 返回實例對象本身
var t1 = t.self
//存儲metadata元類型
var t2 = CJLTeacher.self
type(of:)
-
type(of:)
:用來獲取一個值的動態類型
<!--demo1-->
var age = 10 as NSNumber
print(type(of: age))
<!--打印結果-->
__NSCFNumber
<!--demo2-->
//value - static type 靜態類型:編譯時期確定好的
//type(of:) - dynamic type:Int
var age = 10
//value的靜態類型就是Any
func test(_ value: Any){
print(type(of: value))
}
test(age)
<!--打印結果-->
Int
實踐
demo1
請問下面這段代碼的打印結果是什么?
class CJLTeacher{
var age = 18
var double = 1.85
func teach(){
print("LGTeacher teach")
}
}
class CJLPartTimeTeacher: CJLTeacher {
override func teach() {
print("CJLPartTimeTeacher teach")
}
}
func test(_ value: CJLTeacher){
let valueType = type(of: value)
value.teach()
print(value)
}
var t = CJLPartTimeTeacher()
test(t)
<!--打印結果-->
CJLPartTimeTeacher teach
CJLTest.CJLPartTimeTeacher
demo2
請問下面代碼的打印結果是什么?
protocol TestProtocol {
}
class CJLTeacher: TestProtocol{
var age = 18
var double = 1.85
func teach(){
print("LGTeacher teach")
}
}
func test(_ value: TestProtocol){
let valueType = type(of: value)
print(valueType)
}
var t = CJLTeacher()
let t1: TestProtocol = CJLTeacher()
test(t)
test(t1)
<!--打印結果-->
CJLTeacher
CJLTeacher
- 如果將test中參數的類型修改為泛型,此時的打印是什么?
func test<T>(_ value: T){
let valueType = type(of: value)
print(valueType)
}
<!--打印結果-->
CJLTeacher
TestProtocol
從結果中發現,打印并不一致,原因是因為當有協議、泛型
時,當前的編譯器并不能推斷出準確的類型,需要將value轉換為Any
,修改后的代碼如下:
func test<T>(_ value: T){
let valueType = type(of: value as Any)
print(valueType)
}
<!--打印結果-->
CJLTeacher
CJLTeacher
demo3
在上面的案例中,如果class_getClassMethod
中傳t.self
,可以獲取方法列表嗎?
func test(){
var methodCount: UInt32 = 0
let methodList = class_copyMethodList(t.self, &methodCount)
for i in 0..<numericCast(methodCount) {
if let method = methodList?[i]{
let methodName = method_getName(method)
print("方法列表:\(methodName)")
}else{
print("not found method")
}
}
var count: UInt32 = 0
let proList = class_copyPropertyList(CJLTeacher.self, &count)
for i in 0..<numericCast(count) {
if let property = proList?[i]{
let propertyName = property_getName(property)
print("屬性成員屬性:\(property)")
}else{
print("沒有找到你要的屬性")
}
}
print("test run")
}
test()
從結果運行看,并不能,因為t.self
是實例對象本身
,即CJLTeacher
,并不是CJLTeacher.Type
類型
總結
當無弱引用時,
HeapObject
中的refCounts
等于strongCount + unownedCount
當有弱引用時,
HeapObject
中的refCounts
等于object + xxx + (strongCount + unownedCount) + weakCount
循環應用可以通過
weak / unowned
修飾參數來解決swift中閉包的
捕獲列表
是值拷貝
,即深拷貝,是一個只讀的常量swift由于是
靜態語言
,所以屬性、方法在不加任何修飾符的情況下時是不具備動態性即Runtime特性
的,此時的方法調度是V-Table函數表
調度如果想要
OC使用swift
類中的方法、屬性,需要class繼承NSObject
,并使用@objc修飾
如果想要使用方法交換,除了
繼承NSObject+@objc修飾
,還必須使用dynamic
修飾Any
:任意類型,包括function類型、optional類型AnyObject
:任意類的instance、類的類型、僅類遵守的協議,可以看作是Any的子類AnyClass
:任意實例類型,類型是AnyObject.Type
T.self
:如果T是實例對象,則表示它本身,如果是類,則表示metadata
.T.self
的類型是T.Type