Swift打印变量内存地址和isa指向

1、打印内存地址

let str = "hello"
print(String.init(format: "%p", str))

0x9fc7e81ded839712

2、打印isa指向的类和父类

class Person: NSObject {
    
}
class Student: Person {
    
}
let stu = Student()
print("isa指向的类--",NSStringFromClass(object_getClass(stu) ?? NSObject.self))
print("isa指向的父类--",class_getSuperclass(object_getClass(stu)) ?? NSObject.self)

isa指向的类-- SwiftTest.Student
isa指向的父类-- Person

3、保留2位小数

let a = 12.123123
print(String(format: "%.2f", a))

12.12

你可能感兴趣的:(Swift打印变量内存地址和isa指向)