NSObject Category Issue on iOS
NSObject Category Issue on iOS
- Subject: NSObject Category Issue on iOS
- From: Nik Heger <email@hidden>
- Date: Wed, 30 Mar 2011 09:33:31 +0700
I am new to Objective-C so please forgive me if this is obvious.
I come from Ruby, where extending built in classes is straight forward - you
do it, and it works. I expected the same in Obj-C but got a confusing
result: It works sometimes, but not others.
Here is what I did:
1 - Created a Category on NSObject, this is the header, implementation was
working and tested too (and pretty simple)
@interface NSObject (Logger)
- (void) LOGx1: (id) first, ...;// support for simple logging of multiple
string
@end
So now, I expected to be able to call LOGx1 on any object
2 - Tried this out on a String object and on my UIViewController subclass
which gets loaded from a nib file
// FirstViewController.m - subclass of UIViewController, loaded from a nib
#import "NSObject-Logger.h"
...
- (void)sample
{
NSString *test = @"A TEST";
[test LOGx1: @"Testing NSString!",nil]; // working
[self LOGx1: @"foobar ",nil]; // throwing exception
}
The first invocation works as expected. Seems like NSString knows about the
Logger category. The second one crashes with an unknown selector. The stack
trace seems to indicate that my FirstViewController class inherits from
NSObject(NSObject) instead of NSObject(Logger). Output and stack trace in
[1].
That's as far as I got. Question is, why?
I tried the -ObjC and the -all_load linking flags after Googling for the
issue - didn't make a difference.
The thing I don't understand is: If I define a category on a built-in
object, I expect it to work under all circumstances. If it doesn't work for
some, it would be rather pointless to have this facility, no? Is there
anything obvious I am missing? Does it have to do with the NIB/XIB loading?
thanks, sorry for the length...
~Nik
PS: I later realized logging as a Category makes no sense and moved it into
its own Logger class where it's a class method; but I still want to
understand categories.
[1]
Here is what I get for the stack trace:
*2011-03-30 09:18:52.802 xxx[23245:207] Testing NSString!*
*2011-03-30 09:18:52.803 xxx[23245:207] -[FirstViewController LOGx1:]:
unrecognized selector sent to instance 0x4d37f30*
*2011-03-30 09:18:52.805 xxx[23245:207] *** Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason: '-[FirstViewController
LOGx1:]: unrecognized selector sent to instance 0x4d37f30'*
**** Call stack at first throw:*
*(*
* 0 CoreFoundation 0x00e935a9 __exceptionPreprocess +
185*
* 1 libobjc.A.dylib 0x00fe7313 objc_exception_throw +
44*
* 2 CoreFoundation 0x00e950bb -[NSObject(NSObject)
doesNotRecognizeSelector:] + 187*
*
*
*
*
_______________________________________________
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