Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics (Retired) Advanced Objective-C Dynamic Typing

Ivan Kazakov
PLUS
Ivan Kazakov
Courses Plus Student 43,317 Points

shape description returns some hexadecimal value instead of description

I had been working out the topic using empty Objective C program template. I've added Shape header and implementation files provided as part of the Project Files download; imported Shape.h into the main.m, to be able to use Shape instance as a member of the NSArray, but yet its 'description' returns hexadecimal value instead of description.

Running complete downloaded project works just fine. I've compared all the files line by line but still can't figure out what am I missing.

3 Answers

Stone Preston
Stone Preston
42,016 Points

I used your code and it worked fine. maybe try cleaning your build by pressing command + shift + k. not sure why its not using that overridden method.

Stone Preston
Stone Preston
42,016 Points

can you post the code you are using please

Ivan Kazakov
Ivan Kazakov
Courses Plus Student 43,317 Points

import <Cocoa/Cocoa.h>

import "Shape.h"

int main() { Shape *shape = [[Shape alloc] init];

NSArray *mixed = @[@23, @"tango", shape];

for(id thing in mixed)
{
    NSLog(@"%@", [thing description]);
}

return 0;

}

and here is the output:

2014-07-12 20:40:31.945 MyFirstObjectiveCProgram[2416:303] 23

2014-07-12 20:40:31.947 MyFirstObjectiveCProgram[2416:303] tango

2014-07-12 20:40:31.948 MyFirstObjectiveCProgram[2416:303] <Shape: 0x608000011030>

Stone Preston
Stone Preston
42,016 Points

did you override the description method in your shape implementation file? if not then thats the normal behavior of the description method inherited by NSObject. if you want it to return a different description then you will need to override that method in your Shape implementation file

Ivan Kazakov
Ivan Kazakov
Courses Plus Student 43,317 Points

The Shape.m does contain the custom implementation of description method:

-(NSString *)description { return [NSString stringWithFormat:@"I am %@. My parent is %@. area %f.", [self class], [self superclass], [self area]]; }

That's what confuses me.

Stone Preston
Stone Preston
42,016 Points

can you post all the code you have. it may have something to do with how you instantiated your shape object

Ivan Kazakov
Ivan Kazakov
Courses Plus Student 43,317 Points

Here is all the code.

Shape.h:

```#import <Foundation/Foundation.h> @interface Shape : NSObject @property(nonatomic, copy) NSString *color;

  • (double)area;
  • (BOOL)hitTestAtLocation:(CGPoint)location; @end```

======================

Shape.m:

```#import "Shape.h" @implementation Shape

@synthesize color = _color;

  • (double)area { return -1; }

-(BOOL)hitTestAtLocation:(CGPoint)location { return NO; }

-(NSString *)description { return [NSString stringWithFormat:@"I am %@. My parent is %@. area %f.", [self class], [self superclass], [self area]]; }

@end```

======================

main.m:

```#import <Cocoa/Cocoa.h>

import "Shape.h"

int main() { Shape *shape = [[Shape alloc] init];

NSArray *mixed = @[ @23, @"tango", shape];
for (id thing in mixed) {
    NSLog(@"%@", [thing description]);
}
return 0;

}```