最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

Swift crifan 7083浏览 0评论

对于一个对象:

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 protected],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

转载请注明:在路上 » 【已解决】swift中给一个对象添加支持print打印出对象的描述信息

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.164 seconds, using 22.07MB memory