Reputation: 5117
I used NSLog(@"%@",super) in a method(any method) and it is crashing.... Why? How to print the super contents?
Updated :
currentclassname : superClassName
{
}
and also if i use NSLog(@"%@", [super description]); It is printing "<currentclassname: 0x3db7230>"
instead of superClassName... It is expected to print superClassName right.
Thanks in advance,
Upvotes: 2
Views: 684
Reputation: 86651
and also if i use NSLog(@"%@", [super description]); It is printing
<currentclassname: 0x3db7230>
instead of superClassName... It is expected to print superClassName right.
No. Using [super aMethod]
instead of [self aMethod]
just gives you the implementation of -aMethod that the superclass would use if it hadn't been overridden. [self className]
and [super className]
both resolve to NSObject's implementation which (I guess) interrogates the object's isa
instance variable to get the name.
Upvotes: 1
Reputation: 98984
When encountering the keyword super
and a method call on it, the compiler generates a different call - objc_msgSendSuper*()
instead of the usual objc_msgSend*()
.
objc_msgSendSuper*()
calls get an argument of type objc_super*
instead of objc_object*
:
struct objc_super {
id receiver;
Class class;
};
So, objc_super*
values aren't special instances, they have to be used with the special objc_msgSendSuper*()
functions.
Thus, as Alex says, just call -description
directly on super
- its value is meaningless outside of the context its in unless you specifically use it with a runtime function like objc_msgSendSuper()
.
Upvotes: 5
Reputation: 96323
super
is a way to send a message to yourself and invoke the superclass's implementation rather than your own. It's not a separate object.
NSLog
takes an object as the parameter to %@
, and the object you mean to pass here is yourself.
Frankly, I'm surprised the code in question even compiles.
If you want to log your superclass's description of yourself rather than your own, then, as Alex Reynolds says, you must use a [super description]
message for the parameter to NSLog
. This sends the description
message to yourself using your superclass's implementation, and passes the object that that message returns (the NSString object that is your superclass's description of yourself) as the parameter to NSLog
.
But that's probably not necessary. If you have overridden description
, that implementation can send [super description]
and integrate that string* into the description string that it creates and returns. If you haven't overridden description
, then a description
message to self
will hit the superclass's implementation anyway. Either way, pass self
, not super
, to your NSLog
statements.
*There are several ways you could integrate the one string into the other; see the NSString docs for more details.
Upvotes: 1
Reputation: 779
Try NSLog(@"%@",[super view]);
or anthything like [super ANY_METHOD];
What exactaly are you trying to achieve from super?
Upvotes: 0
Reputation: 96927
Use -description
, e.g.:
NSLog(@"%@", [super description]);
If you have access to the superclass, you can override its -description
method to return whatever information you want, or perhaps augment the class with a category.
Upvotes: 3