Printing a variable memory address in swift

前端 未结 15 1026
天涯浪人
天涯浪人 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:29

    Note: This is for reference types.

    Swift 4/5:

    print(Unmanaged.passUnretained(someVar).toOpaque())
    

    Prints the memory address of someVar. (thanks to @Ying)


    Swift 3.1:

    print(Unmanaged.passUnretained(someVar as AnyObject).toOpaque())
    

    Prints the memory address of someVar.


提交回复
热议问题