Gold Challenge: Chapter 2 My solution


#1

Here is my version of the gold challenge in chapter 2. I struggled with this a bit even after reading the “Objective -C Programming” by BNR but after I reviewed some of the material it came back to me :slight_smile: … Having experience in Java programming… Objective - C is a hard language to wrap your head around…

Let me know what you think and if I missed anything. Did I override my description method properly??

BNRContainer.h

[code]#import “BNRItem.h”

@interface BNRContainer : BNRItem
{
NSString *containerName;
NSMutableArray *containerArray;
}
@property NSString *containerName;

  • (void)addContainerObjects:(BNRItem *)b;

  • (int)valueOfContainer;

@end
[/code]

BNRContainer.m

[code]#import “BNRContainer.h”

@implementation BNRContainer

@synthesize containerName;

  • (void)addContainerObjects:(BNRItem *)b
    {
    if (!containerArray) {

      containerArray = [[NSMutableArray alloc] init];
    

    }
    [containerArray addObject:b];

}

  • (int)valueOfContainer
    {
    int value = 0;
    for (BNRContainer *c in containerArray) {

      value += [c valueInDollars];
    

    }

    return value;
    }

  • (id)initWithContainerName:(NSString *)cName
    {
    if (self) {
    self = [super init];
    [self setContainerName:cName];
    }

    return self;
    }

  • (NSString *)description
    {
    NSString *descriptionString = [[NSString alloc] initWithFormat:@"%@ %@ Total Value of Container: $%d ‘\r’", [self containerName], containerArray, [self valueOfContainer]];

    return descriptionString;
    }

@end
[/code]

Main.m

#import <Foundation/Foundation.h>
#import "BNRItem.h"
#import "BNRContainer.h"

int main(int argc, const char * argv[])
{
    
    @autoreleasepool {
    
    // Create an array of container objects
    NSMutableArray *containers = [[NSMutableArray alloc] init];
    
    // Create 3 different containers
        for (int i = 0; i < 3; i++) {
    // Create an instance of Container
    BNRContainer *newContainer = [[BNRContainer alloc] init];
    
    // Set name for the container
    [newContainer setContainerName:[NSString stringWithFormat:@"Container ID: %d", i]];
    
    // Add a few random BNRItem objects to Container Array
    for (int i = 0; i < 10; i++) {
        
        [newContainer addContainerObjects:[BNRItem randomItem]];
    }
    
    // Add container to containers array
    [containers addObject:newContainer];
    
    }
        
    // Print each container in the Containers array
    for (BNRContainer *c in containers) {
        NSLog(@"%@", c);
    }

}

    return 0;
}

Output

2013-01-08 23:14:42.928 RandomPossessions[592:403] Container ID: 0 ( "Rusty Rusty (8Q2U8): Worth $73, recorded on 2013-01-09 05:14:42 +0000", "Shiny Rusty (5Y2V3): Worth $40, recorded on 2013-01-09 05:14:42 +0000", "Rusty Rusty (2F9Z7): Worth $40, recorded on 2013-01-09 05:14:42 +0000", "Rusty Fluffy (8G5V6): Worth $99, recorded on 2013-01-09 05:14:42 +0000", "Shiny Rusty (3P9B1): Worth $10, recorded on 2013-01-09 05:14:42 +0000", "Rusty Shiny (6R5C1): Worth $93, recorded on 2013-01-09 05:14:42 +0000", "Fluffy Rusty (3E4O0): Worth $1, recorded on 2013-01-09 05:14:42 +0000", "Fluffy Shiny (3A6T4): Worth $30, recorded on 2013-01-09 05:14:42 +0000", "Shiny Rusty (8S3I1): Worth $77, recorded on 2013-01-09 05:14:42 +0000", "Rusty Rusty (4F6F9): Worth $65, recorded on 2013-01-09 05:14:42 +0000" ) Total Value of Container: $528 ' ' 2013-01-08 23:14:42.957 RandomPossessions[592:403] Container ID: 1 ( "Fluffy Rusty (1P5F4): Worth $29, recorded on 2013-01-09 05:14:42 +0000", "Fluffy Fluffy (3R2Q6): Worth $88, recorded on 2013-01-09 05:14:42 +0000", "Rusty Fluffy (4X7P8): Worth $38, recorded on 2013-01-09 05:14:42 +0000", "Shiny Shiny (7E4L1): Worth $77, recorded on 2013-01-09 05:14:42 +0000", "Shiny Fluffy (8K9Y2): Worth $35, recorded on 2013-01-09 05:14:42 +0000", "Fluffy Rusty (8J8T0): Worth $94, recorded on 2013-01-09 05:14:42 +0000", "Rusty Fluffy (5E2Z1): Worth $42, recorded on 2013-01-09 05:14:42 +0000", "Fluffy Rusty (1K1G7): Worth $71, recorded on 2013-01-09 05:14:42 +0000", "Rusty Shiny (9X6C8): Worth $40, recorded on 2013-01-09 05:14:42 +0000", "Fluffy Fluffy (3H0R6): Worth $54, recorded on 2013-01-09 05:14:42 +0000" ) Total Value of Container: $568 ' ' 2013-01-08 23:14:42.966 RandomPossessions[592:403] Container ID: 2 ( "Fluffy Shiny (0A4S4): Worth $13, recorded on 2013-01-09 05:14:42 +0000", "Rusty Rusty (4J8M0): Worth $26, recorded on 2013-01-09 05:14:42 +0000", "Shiny Fluffy (7F7U9): Worth $23, recorded on 2013-01-09 05:14:42 +0000", "Shiny Fluffy (5T0K8): Worth $96, recorded on 2013-01-09 05:14:42 +0000", "Rusty Fluffy (4Y1L2): Worth $25, recorded on 2013-01-09 05:14:42 +0000", "Fluffy Rusty (6C5F3): Worth $18, recorded on 2013-01-09 05:14:42 +0000", "Shiny Rusty (7B0S1): Worth $53, recorded on 2013-01-09 05:14:42 +0000", "Fluffy Rusty (5F8L6): Worth $61, recorded on 2013-01-09 05:14:42 +0000", "Fluffy Rusty (1C1O0): Worth $74, recorded on 2013-01-09 05:14:42 +0000", "Shiny Rusty (9F5J9): Worth $55, recorded on 2013-01-09 05:14:42 +0000" ) Total Value of Container: $444 ' '


#2

Unfortunately, Java programming language has been designed to give you the impression that you don’t have to know anything about what goes on under the hood of the machine, which is quite wrong. Platform independence is just an illusion, filled with sadness and confusion :slight_smile:

The Objective-C Programming Language is a super set of the C Programming Language. Thus it is worth investing some time in reading the following book.

The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie.

Once you get a good background in C, you will find the Objective-C a pleasant language to learn.

As for the description method, it is better to rewrite it like this:


- (NSString *)description
{
    return [NSString stringWithFormat:@"%@ %@ Total Value of Container: $%d '\r'", [self containerName], containerArray [self valueOfContainer]];
}

This will ensure that in a manual RC environment an autoreleased string object is returned.


#3

Ok great! thanks!