Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1059 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NSLog an object's memory address in overridden description method

#1
I am overriding an object's description method. I need to know how to print the object's memory address to replace *{???}* in the code below:

-(NSString *) description {
return [NSString stringWithFormat:@"<SomeClass: %@>\nparmeterOne: %@\nparameterTwo: %@",
{???}, self.parameterOne, self.paramterTwo];
}

I want it to print in the console like this:

<SomeClass: 0x4c05600> parameterOne: 12 parameterTwo: sausages



Reply

#2
To print address use `%p` format specifier and self pointer:

-(NSString *) description {
return [NSString stringWithFormat:@"<SomeClass: %p>\nparmeterOne: %@\nparameterTwo: %@",
self, self.parameterOne, self.paramterTwo];
}
Reply

#3
### The easiest method is to use the super description ###

- (NSString *)description
{
return [NSString stringWithFormat:@"%@ Area: %@, %@", [super description], self.identifier, self.name];
}

So in the case of this model object that is a subclass of NSObject, you can dodge extra work and remembering `%p`.

### Manually using NSStringWithClass() and %p ###

- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p> Area: %@, %@", NSStringFromClass([self class]), self, self.identifier, self.name];
}

So in the case of an object model in which you have a concrete implementer that is derived from this class you will show the correct class name.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through