Printing a variable memory address in swift

前端 未结 15 1023
天涯浪人
天涯浪人 2020-11-28 01:16

Is there anyway to simulate the [NSString stringWithFormat:@\"%p\", myVar], from Objective-C, in the new Swift language?

For example:

le         


        
15条回答
  •  自闭症患者
    2020-11-28 01:26

    My solution on Swift 3

    extension MyClass: CustomStringConvertible {
        var description: String {
            return "<\(type(of: self)): 0x\(String(unsafeBitCast(self, to: Int.self), radix: 16, uppercase: false))>"
        }
    }
    

    this code create description like default description

提交回复
热议问题