Search code examples
objective-cmemorynslogdealloc

Printing Instance ID to NSLog?


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-


Solution

  • 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";
    }