What is the Swift equivalent of -[NSObject description]? -


in objective-c, 1 can add description method class aid in debugging:

@implementation myclass - (nsstring *)description {     return [nsstring stringwithformat:@"<%@: %p, foo = %@>", [self class], foo _foo]; } @end 

then in debugger, can do:

po fooclass <myclass: 0x12938004, foo = "bar"> 

what equivalent in swift? swift's repl output can helpful:

  1> class myclass { let foo = 42 }   2>    3> let x = myclass() x: myclass = {   foo = 42 } 

but i'd override behavior printing console:

  4> println("x = \(x)") x = c11lldb_expr_07myclass (has 1 child) 

is there way clean println output? i've seen printable protocol:

/// protocol should adopted types wish customize /// textual representation.  textual representation used when objects /// written `outputstream`. protocol printable {     var description: string { } } 

i figured automatically "seen" println not appear case:

  1> class myclass: printable {   2.     let foo = 42   3.     var description: string { { return "myclass, foo = \(foo)" } }   4. }      5>    6> let x = myclass() x: myclass = {   foo = 42 }   7> println("x = \(x)") x = c11lldb_expr_07myclass (has 1 child) 

and instead have explicitly call description:

 8> println("x = \(x.description)") x = myclass, foo = 42 

is there better way?

through experimentation have discovered printable , debugprintable protocols work when compiling actual app, not in repl or playground.

side note: code wrote correct, in case, looking debugprintable


swift has renamed these protocols customstringconvertible , customdebugstringconvertible - though compiler helpfully tells it's done :)


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -