Swift3.0中无父类的类用description打印地址

完整代码如下:

import Foundation

class LauCouponManager: CustomStringConvertible {

    static let shared = LauCouponManager()

    init() {

    }

    var description: String{

        return "address:\(Unmanaged.passUnretained(self).toOpaque())"
    }
}

在Swift中,类可以没有父类,如果继承自NSObject的类,可以直接使用description来打印地址,但是如果没有父类,那么这个类就要遵循一个协议:CustomStringConvertible
该协议的源文件介绍如下:

/// - SeeAlso: `String.init<T>(T)`, `CustomDebugStringConvertible`
public protocol CustomStringConvertible {

    /// A textual representation of this instance.
    ///
    /// Instead of accessing this property directly, convert an instance of any
    /// type to a string by using the `String(describing:)` initializer. For
    /// example:
    ///
    ///     struct Point: CustomStringConvertible {
    ///         let x: Int, y: Int
    ///
    ///         var description: String {
    ///             return "(\(x), \(y))"
    ///         }
    ///     }
    ///
    ///     let p = Point(x: 21, y: 30)
    ///     let s = String(describing: p)
    ///     print(s)
    ///     // Prints "(21, 30)"
    ///
    /// The conversion of `p` to a string in the assignment to `s` uses the
    /// `Point` type's `description` property.
    public var description: String { get }
}

有一个公用的只读属性description,在这个方法里面返回的也是字符串,可以用来打印调试.
如果打印地址:需要用

Unmanaged.passUnretained("your object").toOpaque()

your object就是你想要打印地址的对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值