Logging with NSLog Requires Inheritance from NSObject

While attempting to coax Swift to pretty print our objects with println(), we also looked into logging with NSLog. In both cases (details in the link) we found that our class must inherit from NSObject. We also found that calling NSLog on a class that does not inherit from NSObject is a big no no.

class ABC   {
   func description() -> String {
    return "This is class ABC"
  }
}

NSLog("%@", [ABC()])

Logging an instance of the above where ABC does not inherit from NSObject throws this error:

2014-06-04 16:01:03.658 PlaygroundStub_OSX[25724:303] *** NSForwarding: warning: object 0x104c90010 of class '_TtC11lldb_expr_03ABC' does not implement methodSignatureForSelector: -- trouble ahead
Playground execution failed: error: Execution was interrupted, reason: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.
* thread #1: tid = 0xfb5b0, 0x0000000101c25503 libswift_stdlib_core.dylib`-[SwiftObject doesNotRecognizeSelector:] + 19, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
  * frame #0: 0x0000000101c25503 libswift_stdlib_core.dylib`-[SwiftObject doesNotRecognizeSelector:] + 19
    frame #1: 0x00007fff8c0db322 CoreFoundation`___forwarding___ + 1010
    frame #2: 0x00007fff8c0daea8 CoreFoundation`__forwarding_prep_0___ + 120
    frame #3: 0x00007fff8c16f080 CoreFoundation`-[NSArray descriptionWithLocale:indent:] + 384
    frame #4: 0x00007fff90b7e19b Foundation`_NSDescriptionWithLocaleFunc + 64
    frame #5: 0x00007fff8c06bd94 CoreFoundation`__CFStringAppendFormatCore + 7332
    frame #6: 0x00007fff8c09a313 CoreFoundation`_CFStringCreateWithFormatAndArgumentsAux + 115
    frame #7: 0x00007fff8c0d1b9b CoreFoundation`_CFLogvEx + 123
    frame #8: 0x00007fff90ba7d0c Foundation`NSLogv + 79
    frame #9: 0x0000000101e861be libswiftFoundation.dylib`Foundation.(NSLog (Swift.String, Swift.Array<Swift.CVarArg>...) -> ()).(closure #1) + 398
    frame #10: 0x0000000101e7f078 libswiftFoundation.dylib`partial apply forwarder for reabstraction thunk helper from @callee_owned (@unowned Swift.CVaListPointer) -> (@unowned ()) to @callee_owned (@unowned Swift.CVaListPointer) -> (@out ()) + 56
    frame #11: 0x0000000101e99348 libswiftFoundation.dylib`specialization <()> of Swift.withVaList <A>(Swift.Array<Swift.CVarArg>, (Swift.CVaListPointer) -> A) -> A + 1848
    frame #12: 0x0000000101e5a9d6 libswiftFoundation.dylib`Foundation.NSLog (Swift.String, Swift.Array<Swift.CVarArg>...) -> () + 118
    frame #13: 0x0000000101e3d41c

One thought on “Logging with NSLog Requires Inheritance from NSObject

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s