• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Swift description
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Swift description


  • Subject: Re: Swift description
  • From: Greg Parker <email@hidden>
  • Date: Sun, 12 Jul 2015 13:37:24 -0700

> On Jul 11, 2015, at 6:29 PM, Roland King <email@hidden> wrote:
>
>>
>> However, Roland got it slightly wrong, according to the documentation. This:
>>
>> 	print (“\(myObj)”)
>>
>> never uses debugDescription, only description. To use debugDescription (if it exists, or description instead):
>>
>> 	debugPrint (“\(myObj)”)
>
> Dunno about the documentation - I’m remembering a WWDC video which said one falls back to the other. So I tested it in a playground. \(variable) appears to do as suggested, uses description if that exists, falls back to debugDescription if it doesn’t and prints the class name if neither do.
>
> Surprisingly all debugPrint() does is not, as you might expect, use the debugDescription in preference, but just puts quotes around the whole thing and still uses description if it exists. That feels wrong.
>
> class IHaveNothing{}
>
> class OnlyDebugPrintable : IHaveNothing, CustomDebugStringConvertible
> {
> 	var debugDescription : String { return "I am CustomDebugStringConvertible" }
> }
>
> class HasTheLot : OnlyDebugPrintable, CustomStringConvertible
> {
> 	var description : String { return "I have the lot" }
> }
>
>
> let a = IHaveNothing()
> let b = OnlyDebugPrintable()
> let c = HasTheLot()
>
> print( "\(a)” )				// IHaveNothing
> print( "\(b)” )				// I am CustomDebugStringConvertible
> print( "\(c)” )				// I have the lot
> debugPrint( "\(a)” )			// “IHaveNothing"
> debugPrint( "\(b)” )			// “I am CustomDebugStringConvertible"
> debugPrint( "\(c)” )			// “I have the lot"

Note that there is a distinction between "print an object" and "perform string interpolation on an object and print that string".

    print(c)            // I have the lot
    print("\(c)")       // I have the lot
    debugPrint(c)       // I am CustomDebugStringConvertible
    debugPrint("\(c)")  // "I have the lot"

Note that (1) string interpolation prefers the non-debug description when available, and (2) debugPrint() of a String prints the quotes but print() of a String does not.


--
Greg Parker     email@hidden     Runtime Wrangler



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Swift description
      • From: Roland King <email@hidden>
References: 
 >Swift description (From: William Squires <email@hidden>)
 >Re: Swift description (From: Roland King <email@hidden>)
 >Re: Swift description (From: Jens Alfke <email@hidden>)
 >Re: Swift description (From: Quincey Morris <email@hidden>)
 >Re: Swift description (From: Roland King <email@hidden>)

  • Prev by Date: Dependency tracking with asset catalog and interface builder compilers
  • Next by Date: Re: Dependency tracking with asset catalog and interface builder compilers
  • Previous by thread: Re: Swift description
  • Next by thread: Re: Swift description
  • Index(es):
    • Date
    • Thread