jdog
jdog

Reputation: 10759

Custom description method not working

Trying to write a custom description method for my NSManagedObject subclass and it instead of print that data in self.myvarname it prints out "Myvarname" or printing self.userCount it prints "UserCount"?

Followed this example: http://brandontreb.com/objective-c-tutorial-helpful-debugging-by-overriding-the-description-method/

Also, why in the xcode console when I type "po myroominstance.name" or "po myroominstance.usage" does it say no property found (given the class below)?

Here is my class:

.h
===================
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>


@interface Rooms : NSManagedObject
{
  NSString *name;
  NSNumber *capacity;
  NSString *descr;
  NSString *usage;
}

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * capacity;
@property (nonatomic, retain) NSString * descr;
@property (nonatomic, retain) NSString * usage;

@end


.M
==================
#import "Rooms.h"

@implementation Rooms

@dynamic name;
@dynamic capacity;
@dynamic descr;
@dynamic usage;

-(NSString *) description 
{
    return [NSString stringWithFormat:@"ID: %@ Name: %@ Website: %n",
        usage  ,descr,name];
}

@end

UPDATE 1:=========================================================

====== My break point is on this line ==========
Room *myRoom = [Room findFirst];
NSLog(@"The room name is %@", myRoom.name);
================================================

If I break at the NSlog line above, which by the way prints out "The room name is Name" and then type, in the console, "po myRoom.name" I get "Name". If I type "po myRoom.descr" I get "Descr".

Upvotes: 2

Views: 1156

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90571

Neither of the others get the formatting quite correct, but they're barking up the wrong tree, anyway. You seem to have copied the -description method from the tutorial you linked to and not adjusted it to match your class.

You could implement it like so:

- (NSString*) description
{
    return [NSString stringWithFormat:@"<%@ %p> name %@, capacity %@, descr %@, usage %@", [self class], self, name, capacity, descr, usage];
}

(There's no need to invoke -intValue on capacity and then use the "%d" formatting specifier.)

However, that doesn't explain why you're not getting the results you expect during a debugging session or when asking for the description of one of your instances. Please edit your question and add the specifics of how you're asking for the description and what exactly you're getting. For example, are you logging your instances in code? If so, show the logging statements and the output. Are you using po in the debugger? If so, show the actual command and the output.

Upvotes: 7

Related Questions