Re: Swift description
Re: Swift description
- Subject: Re: Swift description
- From: Quincey Morris <email@hidden>
- Date: Sat, 11 Jul 2015 19:32:56 +0000
On Jul 11, 2015, at 11:51 , Jens Alfke <email@hidden> wrote:
>
> I think William asked how to implement a custom description, not how to print it.
Roland was pointing out that Swift uses the custom description for string interpolation, which is the equivalent of using a “%@“ format string in Obj-C. That doesn’t prevent William from using NSLog with a format string, but I think it was worth noting that Swift has a native equivalent.
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)”)
> The method is the same (for compatibility): description(). But since Swift is stricter about typing, you have to implement the Printing (sp?) protocol, which contains just that one method, to signal that your class has a custom description.
As Roland said, the protocol name changed in Swift 2, and it’s now CustomStringConvertible or CustomDebugStringConvertible**. However, it’s not about strict typing, but strictly indicating the behavior you’re intending to provide (aka “conforming”).
** There are 3 other “Custom…” family protocols, but it doesn’t look they get used for the print functions.
_______________________________________________
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