【已解决】swift中给一个对象添加支持print打印出对象的描述信息

对于一个对象:

class PersonItem: ContactItem {
    var personType:PersonType
    var phoneNumber:String
    var email:String
    var position:String
}

之前打印都是:

print("parsed out: \(newPersonItem.type) \(newPersonItem.personType) \(newPersonItem.name) \(newPersonItem.id)")

gLog.debug("parse out \(personItem.phoneNumber) \(personItem.email) \(personItem.position) \(personItem.headerBkgColor) \(personItem.headerImage) \(personItem.headerImageLarge)")

 

很是麻烦。

 

想要去添加支持,使得对于print对象,可以自动打印出对应的基本信息

【解决过程】

1.搜:

swift print object description

What is the Swift equivalent of -[NSObject description]? – Stack Overflow

swift – Getting description like NSObject – Stack Overflow

How can I change the textual representation displayed for a type in Swift? – Stack Overflow

Textual Representation for Classes in Swift – Venkat Peri

2.搜:

swift DebugPrintable

Swifter – Swift 必备 tips

http://swifter.tips/print/

ios – Implementing debugDescription for DebugPrintable in Swift – Stack Overflow

cocoa – Difference between Printable and DebugPrintable in Swift – Stack Overflow

变成:

class ContactItem:NSObject, NSCoding {
    var type:ContactType
    // person/group/topic id string
    var id:String
    // person name/group title/topic title
    var name:String

    var headerBkgColor:UIColor
    //30x30, person/group/topic header image
    var headerImage:UIImage
    //41x41, for conversation/personal info/... use, larger size image
    var headerImageLarge:UIImage

    //used for SelectPersonViewController
    var selected:Bool
    //used for indicated whether some info is updated
    var isUpdated:Bool

    override var description: String {
        return "\(super.description),type=\(type),id=\(id),name=\(name),headerBkgColor=\(headerBkgColor),headerImage=\(headerImage),headerImageLarge=\(headerImageLarge),selected=\(selected),isUpdated=\(isUpdated)"
    }

class PersonItem: ContactItem{
    var personType:PersonType
    var phoneNumber:String
    var email:String
    var position:String

    override var description: String {
        return "\(super.description),personType=\(personType),phoneNumber=\(phoneNumber),email=\(email),position=\(position)"
    }

之后,代码:

gLog.debug("personItem=\(personItem)")

即可输出完整的信息了:

2016-05-20 21:46:39.280 [Debug] [NSOperationQueue 0x7b8c0ff0 :: NSOperation 0x7b8e5820 (QOS: LEGACY)] [ContactUtility.swift:613] parseCommonPersonJson(_:personItem:) > personItem=<JianDao.UserItem: 0x7b8b06c0>,type=Person,id=user-08da3990-9708-4338-ba5e-cff0586e0af8,name=crifan,headerBkgColor=UIDeviceRGBColorSpace 0.964706 0.701961 0.498039 1,headerImage=<UIImage: 0x7b646f60>, {30, 30},headerImageLarge=<UIImage: 0x7b646e20>, {41, 41},selected=false,isUpdated=false,personType=Myself,phoneNumber=13800000000,email=crifan@daryun.com,position=iOS技

 

【总结】

给对象加上继承符合CustomStringConvertible 协议,然后实现对应的var description: String即可

1.对于没有继承NSObject的类的,则需要加上:CustomStringConvertible

变成这种:

class CustomerItem: CustomStringConvertible {
    var name:String
    var model:String

    init() {
        self.name = ""
        self.model = ""
    }

    var description: String {
        return "name=\(name),model=\(model)"
    }
}

 

2.对于已经继承了NSObject的类的子类,由于NSObject默认已经继承CustomStringConvertible了,所以无需,也不能再额外添加:CustomStringConvertible

class ContactItem:NSObject, NSCoding, CustomStringConvertible {

否则会报错:

Redundant conformance of ‘CustomerItem’ to protocol ‘CustomStringConvertible’

应该写成这种:

class CustomerItem: NSObject{
 var name:String
 var model:String

 override init() {
   self.name = ""
   self.model = ""
 
   super.init()
 }
 
 override var description: String {
   return "\(super.description),name=\(name),model=\(model)"
 }
}

即可。

 

==>>>

这样以后再去用print(或者别的log函数)去打印该变量,就不会再输出:

xxx Object

而是输出对应的description的string值了。

CustomStringConvertible



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量