Stuck on Page 142 NSMutableDictionary doesn't print its data


#1

I’m trying to understand NSMutableDictionary from page 142, but I get stuck while running the code in chapter 21?
When I run this line of code NSLog(@“Best Friends: %@”, friends); I don’t get the friends object name listed nor its data.
I see this instead, what am I doing wrong? Please help. Thanks.

[b][i]
2012-12-07 19:55:52.082 Chapter21notes[1880:303]
The years of friendship between you and Jonathan of age 19 is 12.
2012-12-07 19:55:52.083 Chapter21notes[1880:303]
The years of friendship between you and Christine of age 18 is 11.
2012-12-07 19:55:52.083 Chapter21notes[1880:303] Best Friends: {
bestFriend = “<bestFriend: 0x1001033a0>”;
"bestFriend " = “<bestFriend: 0x100102b90>”;
}

[b][i]

main.m
________

#import <Foundation/Foundation.h>
#import "bestFriend.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        bestFriend *Joe= [[bestFriend alloc] init];
        
        [Joe setName: @"Jonathan"];
        [Joe setAge: 19];
        [Joe setWhenmet:2000];
        
        NSLog(@"\nThe years of friendship between you and %@ of age %d is %ld.\n", [Joe name], [Joe age], [Joe yearsoffriendship]);
        
        bestFriend *Chris = [[bestFriend alloc] init];
        
        [Chris setName: @"Christine"];
        [Chris setAge: 18];
        [Chris setWhenmet:2001];
        
        NSLog(@"\nThe years of friendship between you and %@ of age %d is %ld.\n", [Chris name], [Chris age], [Chris yearsoffriendship]);
        
        //Create a dictionary
        NSMutableDictionary *friends = [NSMutableDictionary dictionary];
    
        
        //Put an object under key "bestFriend"
        [friends setObject:Joe forKey:@"bestFriend"];
        
        //Replace that object with a new best friend
        [friends setObject:Chris forKey:@"bestFriend "];
        
        NSLog(@"Best Friends: %@", friends);
        friends=nil;
  
    }
    return 0;
}
bestFriend.h
___________

#import <Foundation/Foundation.h>

@interface bestFriend : NSObject

{
    NSString *name;
    int age;
    NSInteger whenmet;
}

@property NSString *name;
@property int age;
@property NSInteger whenmet;

-(NSInteger)yearsoffriendship;

@end
bestFriend.m
____________
#import "bestFriend.h"

@implementation bestFriend

@synthesize name, age, whenmet;

-(NSInteger)yearsoffriendship;
{
    
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
    NSInteger now = [components year];
    return now - whenmet;

}

@end

#2

You haven’t really done anything wrong, but you forgot to override the description method in the bestFriend class.

Override the description method:

@implementation bestFriend
...
- (NSString *)description
{
    // provide a description and return
    NSString *it = [NSString stringWithFormat:...];
    return it;
@end

By the way, class names (by convention) should start with a capital letter: for example, Foo, FooBar, BestFriend, etc.

There is one other thing: the way you are using the NSMutableDictionary class. You should use keys that look distinct and that sound distinct. In particular do not create distinct keys by adding spaces to a base key: “bestFriend” and "bestFriend " as you have done here. You should read the NSDictionary Class Reference and related Programming Guides.


#3

Thanks ibex! This works and it helps save me headaches. I tried googling everything I can think of to read up on how to solve this problem and couldn’t figure how.