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

donggua080 9年前
   <h2>问题</h2>    <p>对于一个对象:</p>    <pre>  <code class="language-swift">class PersonItem: ContactItem {      var personType:PersonType      var phoneNumber:String      var email:String      var position:String  }</code></pre>    <p>之前打印都是:</p>    <pre>  <code class="language-swift">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)")</code></pre>    <p>很是麻烦。</p>    <p>想要去添加支持,使得对于print对象,可以自动打印出对应的基本信息</p>    <h2>解决过程</h2>    <h3>1.搜:</h3>    <p>swift print object description</p>    <p><a href="/misc/goto?guid=4959673502982774775" rel="nofollow,noindex">What is the Swift equivalent of -[NSObject description]? – Stack Overflow</a></p>    <p><a href="/misc/goto?guid=4959673503077896701" rel="nofollow,noindex">swift – Getting description like NSObject – Stack Overflow</a></p>    <p><a href="/misc/goto?guid=4959673503158464211" rel="nofollow,noindex">How can I change the textual representation displayed for a type in Swift? – Stack Overflow</a></p>    <p><a href="/misc/goto?guid=4959673503253253679" rel="nofollow,noindex">Textual Representation for Classes in Swift – Venkat Peri</a></p>    <h3>2.搜:</h3>    <p>swift DebugPrintable</p>    <p>Swifter – Swift 必备 tips</p>    <p><a href="/misc/goto?guid=4959673503332945140" rel="nofollow,noindex">http://swifter.tips/print/</a></p>    <p><a href="/misc/goto?guid=4959673503417613922" rel="nofollow,noindex">ios – Implementing debugDescription for DebugPrintable in Swift – Stack Overflow</a></p>    <p><a href="/misc/goto?guid=4959673503503472797" rel="nofollow,noindex">cocoa – Difference between Printable and DebugPrintable in Swift – Stack Overflow</a></p>    <h3>变成:</h3>    <pre>  <code class="language-swift">class ContactItem:NSObject, NSCoding, CustomStringConvertible {      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)"      }</code></pre>    <p>之后,代码:</p>    <pre>  <code class="language-swift">gLog.debug("personItem=\(personItem)")</code></pre>    <p>即可输出完整的信息了:</p>    <p>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技</p>    <h2>总结</h2>    <p>给对象加上继承符合CustomStringConvertible 协议,然后实现对应的var description: String即可</p>    <p>比如:</p>    <pre>  <code class="language-swift">class ContactItem:NSObject, NSCoding, CustomStringConvertible {      var id:String      // person name/group title/topic title      var name:String        override var description: String {          return "\(super.description),id=\(id),name=\(name)"      }  }</code></pre>    <p> </p>    <p>这样以后再去用print(或者别的log函数)去打印该变量,就不会再输出:</p>    <p>xxx Object</p>    <p>而是输出对应的description的string值了。</p>    <p> </p>    <p>来自: <a href="/misc/goto?guid=4959673503588229077" rel="nofollow">http://www.crifan.com/swift_add_print_object_class_decription_string_info/</a></p>    <p> </p>