Re: NSObject Category Issue on iOS
Re: NSObject Category Issue on iOS
- Subject: Re: NSObject Category Issue on iOS
- From: Andy Lee <email@hidden>
- Date: Wed, 30 Mar 2011 14:53:23 +0000 (GMT)
On Mar 29, 2011, at 10:33 PM, Nik Heger <email@hidden> wrote:
@interface NSObject (Logger)
- (void) LOGx1: (id) first, ...;// support for simple logging of multiple
string
@end
That looks right.
So now, I expected to be able to call LOGx1 on any object
That is the correct expectation, assuming all your objects descend from NSObject.
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.
Bizarre. The second one should work. What if you comment out the first invocation? I wonder if there's a bug in your LOGx1: method where you aren't cleaning up your varargs. As Mike Abdullah suggested, can you post your code for LOGx1:?
As another experiment, instead of LOGx1: you could add a trivial category method that takes no args, to rule out the possibility of a varargs bug, and use that to prove to yourself that all descendants of NSObject can call that method.
The stack
trace seems to indicate that my FirstViewController class inherits from
NSObject(NSObject) instead of NSObject(Logger).
It doesn't make sense to say a class inherits from NSObject(NSObject). You were on the right track in the first place in assuming that you are simply extending a class. (If LOGx1: conflicted with an existing method, you'd have to address that.) Your class inherits from NSObject, period. There is one NSObject class and your category adds a method to it, meaning all descendants of NSObject can call that method.
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?
Correct.
Is there
anything obvious I am missing? Does it have to do with the NIB/XIB loading?
If you had put the category in a separate *bundle*, you might have to think about when that bundle gets loaded. But in the usual case (which I believe is what you have), the method should simply be available to all objects. In any case, the method works for your first invocation, so it is clearly available.
--Andy
_______________________________________________
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