Re: Swift description
Re: Swift description
- Subject: Re: Swift description
- From: Roland King <email@hidden>
- Date: Sun, 12 Jul 2015 09:29:26 +0800
>
> 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"
_______________________________________________
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