Printing Instance ID to NSLog?
Asked Answered
R

2

15

In the dealloc method for a class how would I print out the ID (or some other unique identifier) for the instance being deallocated?

- (void)dealloc {
    NSLog(@"_deallocing: ??");
    [super dealloc];
}

Is this possible? I am just trying to get a little more feedback in the console as an aid to learning.

many thanks -gary-

Rack answered 9/9, 2009 at 11:49 Comment(0)
G
7

Try this:

- (void)dealloc {
    NSLog(@"_deallocing: %@", self);
    [super dealloc];
}

This will output a bit more info about the object to the console. Depending on the class, you'll either get a memory address and the class name or something more detailed. If you want to give something more detailed in your own classes, override this method and return whatever you'd like:

-(NSString *)description {
    return @"Something useful about this object";
}
Grassi answered 9/9, 2009 at 12:3 Comment(4)
Excellent, so can I assume that "self" is simply accessing the default description of the class?Rack
That's right - the NSLog() function replaces %@ with the given object's description. NSObject's -description method provides the default <classname : address> value. See here for more info on useful stuff to do with NSLog: cocoadev.com/index.pl?NSLogGrassi
What if you want the ID for a different class? E.g. I have a member variable NSMutableArray and I'd like to print its instance ID.Augie
Nothing wrong with doing this--it's usually what you want--but I think dmkash's answer is better suited to the question at hand. In the case that description is overridden, this will not necessarily output a "unique identifier" as the question asks.Imprinting
V
54

If you specifically want the memory address of the object (which I suppose could be considered an "identifier" if you don't have one implemented in your class), you can use this:

NSLog(@"deallocing %p", self);

This can be rather helpful if you have more than one instance of a particular class and are trying to determine which is getting dealloc'd when.

Villain answered 27/5, 2010 at 2:8 Comment(1)
Documentation about the string format specifiers: developer.apple.com/library/mac/documentation/Cocoa/Conceptual/…Lanceted
G
7

Try this:

- (void)dealloc {
    NSLog(@"_deallocing: %@", self);
    [super dealloc];
}

This will output a bit more info about the object to the console. Depending on the class, you'll either get a memory address and the class name or something more detailed. If you want to give something more detailed in your own classes, override this method and return whatever you'd like:

-(NSString *)description {
    return @"Something useful about this object";
}
Grassi answered 9/9, 2009 at 12:3 Comment(4)
Excellent, so can I assume that "self" is simply accessing the default description of the class?Rack
That's right - the NSLog() function replaces %@ with the given object's description. NSObject's -description method provides the default <classname : address> value. See here for more info on useful stuff to do with NSLog: cocoadev.com/index.pl?NSLogGrassi
What if you want the ID for a different class? E.g. I have a member variable NSMutableArray and I'd like to print its instance ID.Augie
Nothing wrong with doing this--it's usually what you want--but I think dmkash's answer is better suited to the question at hand. In the case that description is overridden, this will not necessarily output a "unique identifier" as the question asks.Imprinting

© 2022 - 2024 — McMap. All rights reserved.