Gold Challenge: a solution from someone really unexperienced


#1

Hi.
This is the first time I write something on this forum, and also the first time I read seriously a programming guide. I’ve no experience whatsoever with programming, any programming, but I was curious and I want to learn. I browsed different books, but people told me that yours is one of the best guide around, so I decided to stick with this. It was a good choice because I’m really enjoying it. : )
Anyway in this chapter two I faced my first issue, I wasn’t able to complete the Gold Challenge. The guide tells it needs some hours, to me, it took a week. Well, the spare time I have in a week, obviously. But indeed I’m talking about 12/15 hours, maybe 20, that is really a lot of time. Yeah yeah, I know. : D

When I completed it, I came here to see all of you dudes solutions. I must say, they kinda blew me away. I have no idea of what all those instructions you use are. Things like “@property” or “@synthesize” or “#pragma” or “keyValue” and a lot of others are a mystery to me, but seeing how you use them, I bet they save a lot of time.

My solution, I tried to emulate the guide as best I could, doing some research on internet too, for something like “how to sum things” or “how to sum things in a description string”. This is it, and I would really like to know if it is good (well it works so I guess it isn’t bad at least), but mainly what do you think of it, if I could have done something better, if I wasted a lot of time doing things not necessary.

Thank you. : )

BNRContainer.h

@interface BNRContainer : BNRItem
{
    NSMutableArray *subitems;
    NSString *containerName;
    int containerValue;
}

- (NSMutableArray *)subitems;
- (NSString *)containerName;
- (int) containerValue;

- (id)initWithContainerName: (NSString *)name ContainerValue:(int)cValue 
                   subitems:(NSMutableArray *)itemset;

- (void)setContainerName: (NSString *)str;
- (void)setContainerValue: (int)i;
- (void)setSubitems: (NSMutableArray *)itemset;

+ (id) randomContainer;

@end

BNRContainer.m

@implementation BNRContainer

- (id)initWithContainerName:(NSString *)name ContainerValue:(int)cValue subitems:(NSMutableArray *)itemset
{
    self = [super init];
    
    if (self) {
        [self setContainerName:name];
        [self setContainerValue:cValue];
        [self setSubitems:itemset];
    }
    
    return self;    
}


+ (id)randomContainer
{
    int boxNumber = rand() % 10;
    
    NSString *name = [NSString stringWithFormat:@"Box%i",
                      boxNumber];
                      
    
    NSMutableArray *itemset = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < 10; i++) { 
        BNRItem *item = [BNRItem randomItem]; [itemset addObject:item];
    }
   
    int boxValue = 5;
      
    BNRContainer *newRandomContainer = [[BNRContainer alloc] initWithContainerName:name ContainerValue:boxValue subitems:itemset];
    
    return newRandomContainer;
}


- (int) valueInDollars {
    int sum = 0;
    for (int i = 0; i < [subitems count]; i++) {
        sum += [[subitems objectAtIndex:i] valueInDollars];
    }
    return sum;
}


- (void) setContainerName:(NSString *)srt
{
    containerName = srt;
}
- (NSString *) containerName
{
    return containerName;
}

- (void) setContainerValue:(int)i
{
    containerValue = i;
}
- (int) containerValue
{
    return containerValue;
}

- (void) setSubitems:(NSMutableArray *)itemset
{
    subitems = itemset;
}

- (NSMutableArray *) subitems
{
    return subitems;
}


- (NSString *) description
{
    NSString *descriptionString = [[NSString alloc] initWithFormat:@" Name: %@ of value $%i, composed by items:%@.",
                                   containerName,
                                   containerValue,
                                   subitems];

    return descriptionString;
    
}

@end

main.m


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

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

    @autoreleasepool {


        NSMutableArray *boxArray = [[NSMutableArray alloc] init];
        
        for (int i = 0; i < 2; i++) {
            BNRContainer *c = [BNRContainer randomContainer]; [boxArray addObject:c];
        }
        

        BNRContainer *jumbo = [[BNRContainer alloc] initWithContainerName:@"Jumbo" ContainerValue:10 subitems];
        NSLog(@"%@  which complessive value is $%i. Total value of the box is %i", 
              jumbo,
              [jumbo valueInDollars],
              [jumbo containerValue]+[jumbo valueInDollars]);

 boxArray = nil;

    }
    return 0;
}

Output is this:

2012-10-01 13:06:08.006 RandomPossession[15215:903]  Name: Jumbo of value $10, composed by items:(
    " Name: Box5 of value $5, composed by items:(\n    \"Rusty Bear (7B5U1): Worth $1, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Fluffy Bear (5W2A6): Worth $23, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Fluffy Mac (1R1I7): Worth $44, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Shiny Spork (4C9H5): Worth $97, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Fluffy Bear (2B8E0): Worth $98, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Rusty Bear (5W3W6): Worth $8, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Fluffy Spork (2E7T5): Worth $65, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Rusty Bear (0N8B6): Worth $1, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Shiny Mac (5Q2Q5): Worth $79, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Fluffy Bear (9Q5Q3): Worth $23, recorded on 2012-10-01 13:06:07 +0200\"\n).",
    " Name: Box6 of value $5, composed by items:(\n    \"Shiny Mac (4S4Y4): Worth $38, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Shiny Bear (8M0U9): Worth $63, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Rusty Bear (7U9Z5): Worth $85, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Rusty Spork (0K8Z5): Worth $23, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Fluffy Mac (1L2E5): Worth $42, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Shiny Mac (5F3P7): Worth $92, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Fluffy Spork (0S1S7): Worth $25, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Rusty Bear (8L6B9): Worth $35, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Rusty Bear (1O0Y4): Worth $56, recorded on 2012-10-01 13:06:07 +0200\",\n    \"Shiny Bear (5J9K5): Worth $7, recorded on 2012-10-01 13:06:07 +0200\"\n)."
).  which complessive value is $905. Total value of the box is 915

I really don’t understand why the items output is so messy, considering that if I print a single BNRContainer, the output is really neat.
Hope to hear some comments here, thank you again.
Bye.


#2

Missing files; for example, files for BNRItem.

Please post them all so that I can compile and run.

As for the time you are spending, that’s quite normal when learning new things especially in the world of computing. Hang in there.


#3

Oh, sorry, I didn’t think you would need them too.
Here they are.

BNRItem.h


@interface BNRItem : NSObject
{
    NSString *itemName;
    NSString *serialNumber;
    int valueInDollars;
    NSDate *dateCreated;
}
                                  
+ (id)randomItem;

- (void)setItemName: (NSString *)str;
- (NSString *)itemName;

- (void)setSerialNumber: (NSString *)str;
- (NSString *)serialNumber;

- (void)setValueInDollars: (int)i; 
- (int)valueInDollars;

- (NSDate *)dateCreated;

- (id)initWithItemName: (NSString *)name
         valueInDollars: (int)value
          serialNumber: (NSString *)sNumber;

@end

BNRItem.m


@implementation BNRItem

+ (id)randomItem;
{
    //Create an array of three adjectives
    NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@"Fluffy", @"Rusty", @"Shiny", nil];

    //Create an array of three nouns
    NSArray *randomNounList = [NSArray arrayWithObjects:@"Bear", @"Spork", @"Mac", nil];

    //Get the index of a random adjective/noun from the list
    //Note: The % operator, called modulo operator, gives you the remainder. So adjectiveIndex is a random number from 0 to 2 inclusive.
    NSInteger adjectiveIndex = rand() % [randomAdjectiveList count];
    NSInteger nounIndex = rand() % [randomNounList count];
    
    //Note that NSInteger is not an object, but a type definition for "unsigned long"
    NSString *randomName = [NSString stringWithFormat:@"%@ %@",
                            [randomAdjectiveList objectAtIndex:adjectiveIndex],
                            [randomNounList objectAtIndex:nounIndex]];
                            
    int randomValue = rand() % 100;
    
    NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
                                    '0' + rand() % 10,
                                    'A' + rand() % 26,
                                    '0' + rand() % 10,
                                    'A' + rand() % 26,
                                    '0' + rand() % 10];
    
    BNRItem *newItem = [[self alloc] initWithItemName:randomName valueInDollars:randomValue serialNumber:randomSerialNumber];
    
    return newItem;
}


/*                           
- (id)init  
{
    return [self initWithItemName:@"Item" valueInDollars:0 serialNumber:@""];
}
*/

- (id)initWithItemName:(NSString *)name valueInDollars:(int)value serialNumber:(NSString *)sNumber


{
    //Call the superclass's designated initializer
    self = [super init];
    
    //Give the istance variables initial values
    //Did the superclass's designated initiazlizer succeed?
    if (self) {
        [self setItemName:name];
        [self setValueInDollars:value];
        [self setSerialNumber:sNumber];
        dateCreated = [[NSDate alloc] init];
    }
    
    //Return the address of the newly initializated object
    return self;
}

- (void) setItemName:(NSString *)str
{
    itemName = str;
}
- (NSString *)itemName
{
    return itemName;
}

- (void) setSerialNumber:(NSString *)str
{
    serialNumber = str;
}
- (NSString *)serialNumber
{
    return serialNumber;
}

- (void) setValueInDollars:(int)i
{
    valueInDollars = i;
}
- (int)valueInDollars
{
    return valueInDollars;
}

- (NSDate *)dateCreated
{
    return dateCreated;
}

- (NSString *)description
{
    NSString *descriptionString = [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d, recorded on %@",
                                   itemName,
                                   serialNumber,
                                   valueInDollars,
                                   dateCreated];
    return descriptionString;
}

@end

I leaved the description lines from the guide, hope you don’t mind.
Thank you for the reply, anyway.


#4

Part of the reason the output appears messy is that your code relies on the default ‘description’ method on NSArray (specifically, for your subitems array)

One possible fix would be to iterate through the subitems array and display each item ‘by hand’.

Try something like this for the description method of BNRContainer:

     NSMutableString *descriptionString = [NSMutableString stringWithFormat:@"Name: %@ of value $%i, ",
                                                                  containerName, containerValue];
    
     [descriptionString appendString:@"composed by items:\n"];

    for (BNRItem *subItem in subitems) {
        [descriptionString appendString:[subItem description]];
        [descriptionString appendString:@"\n"];
    }

#5

The reason for the messy looking output is that as soon as you start nesting arrays (an array containing another array), after a certain depth, the way arrays print themselves seems to be changing (I am very surprised.)

To demonstrate this, I have modified your main program slightly:

//  main.m

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

@interface NSArray (MyExtensions)
- (void)prettyPrint;
@end

@interface Foo : NSObject

+ (Foo *)randomFoo;

@end

int main (int argc, const char * argv[])
{
    @autoreleasepool {
        NSMutableArray *boxArray = [[NSMutableArray alloc] init];
        for (int i = 0; i < 2; i++) {
            BNRContainer *c = [BNRContainer randomContainer];
            [boxArray addObject:c];
            Foo *f = [Foo randomFoo];
            [boxArray addObject:f];

            NSLog (@"---(1)-> %s: %@", __PRETTY_FUNCTION__, c);
            NSLog (@"---(1)-> %s: %@", __PRETTY_FUNCTION__, f);
        }
        NSLog (@"---(2)-> %s: %@", __PRETTY_FUNCTION__, boxArray);
        
        NSMutableArray *A1 = [NSMutableArray arrayWithObjects:[Foo randomFoo], [Foo randomFoo], nil];
        NSMutableArray *B1 = [NSMutableArray arrayWithObjects:[Foo randomFoo], [Foo randomFoo], nil];
        NSMutableArray *AB = [NSMutableArray arrayWithObjects:A1, B1, [BNRContainer randomContainer], nil];
        NSLog (@"---(3)-> %s: %@", __PRETTY_FUNCTION__, AB);

        [AB prettyPrint];
    }
    return 0;
}

@interface Foo ()
{
    NSArray *randomStringsArray;
}
@end

@implementation Foo

+ (Foo *)randomFoo
{
    return [[[self class] alloc] init];
}

- (id)init
{
    self = [super init];
    randomStringsArray = [[self class] randomStringsArray];
    return self;
}

+ (NSArray *)randomStringsArray
{
    NSMutableArray *array = [NSMutableArray array];
    const u_int32_t N = 1 + arc4random_uniform (9);
    for (int x = 0; x < N; ++x)
    {
        [array addObject:[[self class] randomDescription]];
    }
    return array;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"%@ %@", [[self class] randomDescription], randomStringsArray];
}

+ (NSString *)randomString
{
    char buf [16 + 1];
    const u_int32_t N = arc4random_uniform (sizeof (buf) /sizeof (buf [0]));

    for (int x = 0; x < N; ++x)
    {
        buf [x] = 'A' + arc4random_uniform (26);
    }
    buf [N] = 0; 
    return [NSString stringWithFormat:@"%d %s %d", arc4random_uniform (1024), buf, arc4random_uniform (2048)];
}

+ (NSString *)randomDescription
{
    return [NSString stringWithFormat:@"F1: %@ G1: %@ H1: %@", [self randomString], [self randomString], [self randomString]];
}

@end

@implementation NSArray (MyExtensions)

- (void)prettyPrint
{    
    for (NSObject *x in self)
    {
        if ([x isKindOfClass:[NSArray class]])
             [(NSArray *)x prettyPrint];
        else
             NSLog (@"%s: %@", __PRETTY_FUNCTION__, [x description]);
    }
}
@end

The output:

2012-10-03 17:44:41.705 ArmaXYZ[17664:403] ---(1)-> int main(int, const char **): Name: Box7 of value $5, composed by items: (
    "Rusty Mac (0Y4Q3): Worth $58, recorded on 2012-10-03 08:14:41 +0000",
    "Rusty Bear (2Q7N7): Worth $65, recorded on 2012-10-03 08:14:41 +0000",
    "Rusty Spork (3R9R0): Worth $12, recorded on 2012-10-03 08:14:41 +0000",
    "Fluffy Bear (6F7E2): Worth $78, recorded on 2012-10-03 08:14:41 +0000",
    "Rusty Mac (9D9H7): Worth $33, recorded on 2012-10-03 08:14:41 +0000",
    "Shiny Spork (5R8R4): Worth $36, recorded on 2012-10-03 08:14:41 +0000",
    "Rusty Mac (8U8Q4): Worth $53, recorded on 2012-10-03 08:14:41 +0000",
    "Shiny Bear (2I9G1): Worth $3, recorded on 2012-10-03 08:14:41 +0000",
    "Rusty Bear (8P8J5): Worth $8, recorded on 2012-10-03 08:14:41 +0000",
    "Rusty Bear (3Y5V5): Worth $14, recorded on 2012-10-03 08:14:41 +0000"
)
2012-10-03 17:44:41.705 ArmaXYZ[17664:403] ---(1)-> int main(int, const char **): F1: 470 BTNLRXDM 457 G1: 995 MHA 966 H1: 804 TIJBKXQTBQODE 1421 (
    "F1: 547 AUR 1018 G1: 481  1071 H1: 1016 ZCMZUUA 729",
    "F1: 74 MGAOUGTQ 350 G1: 52  786 H1: 835 ZRZXRMSWLABVJEOL 834",
    "F1: 346 P 1268 G1: 698 HHEYJ 266 H1: 854 DGKQ 1324",
    "F1: 876 O 1459 G1: 960 MEAQTNUNFHIX 232 H1: 451 OYNSKULVRLF 1640",
    "F1: 581 IUXV 622 G1: 289 SXNTRBCWH 258 H1: 889 ULMSCCKLYHOVSDC 150",
    "F1: 980 PICWCKLUOA 370 G1: 671 KBYNZRQACUSDGDGA 838 H1: 622 WEIYVXGBAARYHNYE 1411",
    "F1: 697 NKDQHXOTNX 1432 G1: 35 GIZR 1287 H1: 43 OQRBRZUYBAWYLJV 1397",
    "F1: 143 SFF 406 G1: 638 JDWNDY 1273 H1: 673 AXVZRYXZIKYU 596",
    "F1: 837 IOHKNQ 609 G1: 598 EPFEQ 1249 H1: 619 GIUS 364"
)
2012-10-03 17:44:41.707 ArmaXYZ[17664:403] ---(1)-> int main(int, const char **): Name: Box4 of value $5, composed by items: (
    "Fluffy Spork (5F4D8): Worth $17, recorded on 2012-10-03 08:14:41 +0000",
    "Fluffy Mac (2Q6O7): Worth $5, recorded on 2012-10-03 08:14:41 +0000",
    "Shiny Bear (7P8F8): Worth $1, recorded on 2012-10-03 08:14:41 +0000",
    "Rusty Bear (4L1B5): Worth $94, recorded on 2012-10-03 08:14:41 +0000",
    "Fluffy Mac (9Y2Q7): Worth $42, recorded on 2012-10-03 08:14:41 +0000",
    "Fluffy Spork (8T0O2): Worth $95, recorded on 2012-10-03 08:14:41 +0000",
    "Rusty Spork (2Z1X2): Worth $82, recorded on 2012-10-03 08:14:41 +0000",
    "Fluffy Mac (1G7Q0): Worth $90, recorded on 2012-10-03 08:14:41 +0000",
    "Shiny Spork (6C8P9): Worth $35, recorded on 2012-10-03 08:14:41 +0000",
    "Fluffy Mac (0R6D6): Worth $89, recorded on 2012-10-03 08:14:41 +0000"
)
2012-10-03 17:44:41.708 ArmaXYZ[17664:403] ---(1)-> int main(int, const char **): F1: 732 HUSXFWWA 1913 G1: 525 Q 1918 H1: 965 TDBRRRLKKYJU 79 (
    "F1: 693 ZSZVDCLMRUPMDP 1768 G1: 37 ZHEEKYGPZQYYVB 1741 H1: 201 FKVYZRXUWEB 1950"
)
2012-10-03 17:44:41.711 ArmaXYZ[17664:403] ---(2)-> int main(int, const char **): (
    "Name: Box7 of value $5, composed by items: (\n    \"Rusty Mac (0Y4Q3): Worth $58, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Rusty Bear (2Q7N7): Worth $65, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Rusty Spork (3R9R0): Worth $12, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Fluffy Bear (6F7E2): Worth $78, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Rusty Mac (9D9H7): Worth $33, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Shiny Spork (5R8R4): Worth $36, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Rusty Mac (8U8Q4): Worth $53, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Shiny Bear (2I9G1): Worth $3, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Rusty Bear (8P8J5): Worth $8, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Rusty Bear (3Y5V5): Worth $14, recorded on 2012-10-03 08:14:41 +0000\"\n)",
    "F1: 435  174 G1: 804 E 412 H1: 572 RWAEXSL 832 (\n    \"F1: 547 AUR 1018 G1: 481  1071 H1: 1016 ZCMZUUA 729\",\n    \"F1: 74 MGAOUGTQ 350 G1: 52  786 H1: 835 ZRZXRMSWLABVJEOL 834\",\n    \"F1: 346 P 1268 G1: 698 HHEYJ 266 H1: 854 DGKQ 1324\",\n    \"F1: 876 O 1459 G1: 960 MEAQTNUNFHIX 232 H1: 451 OYNSKULVRLF 1640\",\n    \"F1: 581 IUXV 622 G1: 289 SXNTRBCWH 258 H1: 889 ULMSCCKLYHOVSDC 150\",\n    \"F1: 980 PICWCKLUOA 370 G1: 671 KBYNZRQACUSDGDGA 838 H1: 622 WEIYVXGBAARYHNYE 1411\",\n    \"F1: 697 NKDQHXOTNX 1432 G1: 35 GIZR 1287 H1: 43 OQRBRZUYBAWYLJV 1397\",\n    \"F1: 143 SFF 406 G1: 638 JDWNDY 1273 H1: 673 AXVZRYXZIKYU 596\",\n    \"F1: 837 IOHKNQ 609 G1: 598 EPFEQ 1249 H1: 619 GIUS 364\"\n)",
    "Name: Box4 of value $5, composed by items: (\n    \"Fluffy Spork (5F4D8): Worth $17, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Fluffy Mac (2Q6O7): Worth $5, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Shiny Bear (7P8F8): Worth $1, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Rusty Bear (4L1B5): Worth $94, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Fluffy Mac (9Y2Q7): Worth $42, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Fluffy Spork (8T0O2): Worth $95, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Rusty Spork (2Z1X2): Worth $82, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Fluffy Mac (1G7Q0): Worth $90, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Shiny Spork (6C8P9): Worth $35, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Fluffy Mac (0R6D6): Worth $89, recorded on 2012-10-03 08:14:41 +0000\"\n)",
    "F1: 455 GGLOPMCYEIHUHN 2041 G1: 296 ZGUVTFPBZKFUTP 1980 H1: 246 O 384 (\n    \"F1: 693 ZSZVDCLMRUPMDP 1768 G1: 37 ZHEEKYGPZQYYVB 1741 H1: 201 FKVYZRXUWEB 1950\"\n)"
)
2012-10-03 17:44:41.714 ArmaXYZ[17664:403] ---(3)-> int main(int, const char **): (
        (
        "F1: 71 YNWK 1843 G1: 439 FTEURGKQIBOFVPS 1079 H1: 156 LWZVFHK 468 (\n    \"F1: 520  843 G1: 1008 RG 1433 H1: 401 WPUYAQIRIKVMUL 1939\",\n    \"F1: 382 DAEYUXDVKBPV 698 G1: 28 AJJXHDXMKGUU 1844 H1: 647 EGUUI 148\",\n    \"F1: 12 EISO 2018 G1: 87 SWT 2 H1: 114 LACDZERMCCSHIQJH 663\"\n)",
        "F1: 433 YJHEGGKWUO 1984 G1: 239 QDPJOKA 1603 H1: 80 YXLEWWFRQEVLD 1556 (\n    \"F1: 464 EBNODWWLXOBXRVFN 1982 G1: 202 CEDSKFO 1251 H1: 845 UIISEKOHIPG 319\",\n    \"F1: 257  560 G1: 150 SX 261 H1: 590 BKZC 1702\",\n    \"F1: 607 DSNOZS 1407 G1: 497 BODXMOEMNFV 1008 H1: 319 SHIEXDYOUPSNTRW 1852\",\n    \"F1: 198 UOLIHBHGADTP 111 G1: 200 A 497 H1: 64 NJQDEORAMRPOMRWA 1193\",\n    \"F1: 484 PVZXHXCWMQOO 1984 G1: 310 ES 140 H1: 106 ULIDOFKIG 1624\"\n)"
    ),
        (
        "F1: 22 KQJIZOBO 1114 G1: 383 NSNYPE 1946 H1: 934 RKCWGBVRIEXIEO 1230 (\n    \"F1: 200 WUPGHOENXM 1062 G1: 804 HMKRCS 1192 H1: 409 MIZKVYJK 1409\",\n    \"F1: 394 ULFSDSHDQPKG 1510 G1: 174 SLHYM 1267 H1: 700 MLTNNXVEIP 1426\",\n    \"F1: 626 OP 1696 G1: 131 HWXTTQUSRAOTRW 1114 H1: 993 TKSIMWTSIUCZ 1748\",\n    \"F1: 994 UTQLTMDTEGE 1651 G1: 686 OERGSLUOSM 1198 H1: 316  1353\",\n    \"F1: 440 CT 905 G1: 984 XKHTTWBQRERVNS 1369 H1: 58 YCKJFKBYCKIXDY 2011\",\n    \"F1: 986 U 1814 G1: 1013 XMJTRVT 177 H1: 905 ELPUKVGRIL 1797\"\n)",
        "F1: 873 G 281 G1: 340 VPNVX 268 H1: 565 SMYUNW 399 (\n    \"F1: 711 VYEA 1421 G1: 105 ZNQPOPNDUBKFZEK 2040 H1: 1001 FIZD 553\",\n    \"F1: 885 QJSVIAGAOOKPFRVJ 122 G1: 287 AR 626 H1: 661 VVGQKXBMN 703\",\n    \"F1: 55 SVLLT 1777 G1: 227 GLDWXKOLJUTRB 1285 H1: 555 KPAWP 651\",\n    \"F1: 70  115 G1: 442 LNKYNANF 946 H1: 727 GM 672\",\n    \"F1: 315 DKFDQL 915 G1: 62 AT 1607 H1: 996 GQBNWNZQZD 1222\",\n    \"F1: 571 JSRWID 105 G1: 899 RUAQBHVSOYDM 1799 H1: 41 SNFSMKWFEOUTGTJ 1555\",\n    \"F1: 905 VIJPM 1410 G1: 392 OPJAIF 1760 H1: 398 WOGFRFDHWW 1333\"\n)"
    ),
    "Name: Box3 of value $5, composed by items: (\n    \"Shiny Bear (0S6O6): Worth $94, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Fluffy Spork (4W0F3): Worth $38, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Fluffy Spork (2D7H6): Worth $17, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Rusty Spork (4C5V5): Worth $20, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Shiny Spork (9O4T8): Worth $11, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Shiny Bear (1H9X3): Worth $15, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Rusty Mac (2H8J1): Worth $10, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Fluffy Spork (5E7X4): Worth $68, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Fluffy Bear (2A4A5): Worth $1, recorded on 2012-10-03 08:14:41 +0000\",\n    \"Fluffy Spork (3B0R5): Worth $15, recorded on 2012-10-03 08:14:41 +0000\"\n)"
)
2012-10-03 17:44:41.714 ArmaXYZ[17664:403] -[NSArray(MyExtensions) prettyPrint]: F1: 484 GTRQXUH 285 G1: 756 QNH 999 H1: 602 WAARKNX 735 (
    "F1: 520  843 G1: 1008 RG 1433 H1: 401 WPUYAQIRIKVMUL 1939",
    "F1: 382 DAEYUXDVKBPV 698 G1: 28 AJJXHDXMKGUU 1844 H1: 647 EGUUI 148",
    "F1: 12 EISO 2018 G1: 87 SWT 2 H1: 114 LACDZERMCCSHIQJH 663"
)
2012-10-03 17:44:41.715 ArmaXYZ[17664:403] -[NSArray(MyExtensions) prettyPrint]: F1: 195 ONAWETBVAJJ 981 G1: 381 IMYRPRLNZD 1916 H1: 872 VP 405 (
    "F1: 464 EBNODWWLXOBXRVFN 1982 G1: 202 CEDSKFO 1251 H1: 845 UIISEKOHIPG 319",
    "F1: 257  560 G1: 150 SX 261 H1: 590 BKZC 1702",
    "F1: 607 DSNOZS 1407 G1: 497 BODXMOEMNFV 1008 H1: 319 SHIEXDYOUPSNTRW 1852",
    "F1: 198 UOLIHBHGADTP 111 G1: 200 A 497 H1: 64 NJQDEORAMRPOMRWA 1193",
    "F1: 484 PVZXHXCWMQOO 1984 G1: 310 ES 140 H1: 106 ULIDOFKIG 1624"
)
2012-10-03 17:44:41.715 ArmaXYZ[17664:403] -[NSArray(MyExtensions) prettyPrint]: F1: 615 IA 237 G1: 929 KYAHRICVUBL 1111 H1: 404 KUNN 1887 (
    "F1: 200 WUPGHOENXM 1062 G1: 804 HMKRCS 1192 H1: 409 MIZKVYJK 1409",
    "F1: 394 ULFSDSHDQPKG 1510 G1: 174 SLHYM 1267 H1: 700 MLTNNXVEIP 1426",
    "F1: 626 OP 1696 G1: 131 HWXTTQUSRAOTRW 1114 H1: 993 TKSIMWTSIUCZ 1748",
    "F1: 994 UTQLTMDTEGE 1651 G1: 686 OERGSLUOSM 1198 H1: 316  1353",
    "F1: 440 CT 905 G1: 984 XKHTTWBQRERVNS 1369 H1: 58 YCKJFKBYCKIXDY 2011",
    "F1: 986 U 1814 G1: 1013 XMJTRVT 177 H1: 905 ELPUKVGRIL 1797"
)
2012-10-03 17:44:41.716 ArmaXYZ[17664:403] -[NSArray(MyExtensions) prettyPrint]: F1: 851 DPUTEAYLLQIC 233 G1: 179  487 H1: 990 RXNLMOPQ 2041 (
    "F1: 711 VYEA 1421 G1: 105 ZNQPOPNDUBKFZEK 2040 H1: 1001 FIZD 553",
    "F1: 885 QJSVIAGAOOKPFRVJ 122 G1: 287 AR 626 H1: 661 VVGQKXBMN 703",
    "F1: 55 SVLLT 1777 G1: 227 GLDWXKOLJUTRB 1285 H1: 555 KPAWP 651",
    "F1: 70  115 G1: 442 LNKYNANF 946 H1: 727 GM 672",
    "F1: 315 DKFDQL 915 G1: 62 AT 1607 H1: 996 GQBNWNZQZD 1222",
    "F1: 571 JSRWID 105 G1: 899 RUAQBHVSOYDM 1799 H1: 41 SNFSMKWFEOUTGTJ 1555",
    "F1: 905 VIJPM 1410 G1: 392 OPJAIF 1760 H1: 398 WOGFRFDHWW 1333"
)
2012-10-03 17:44:41.718 ArmaXYZ[17664:403] -[NSArray(MyExtensions) prettyPrint]: Name: Box3 of value $5, composed by items: (
    "Shiny Bear (0S6O6): Worth $94, recorded on 2012-10-03 08:14:41 +0000",
    "Fluffy Spork (4W0F3): Worth $38, recorded on 2012-10-03 08:14:41 +0000",
    "Fluffy Spork (2D7H6): Worth $17, recorded on 2012-10-03 08:14:41 +0000",
    "Rusty Spork (4C5V5): Worth $20, recorded on 2012-10-03 08:14:41 +0000",
    "Shiny Spork (9O4T8): Worth $11, recorded on 2012-10-03 08:14:41 +0000",
    "Shiny Bear (1H9X3): Worth $15, recorded on 2012-10-03 08:14:41 +0000",
    "Rusty Mac (2H8J1): Worth $10, recorded on 2012-10-03 08:14:41 +0000",
    "Fluffy Spork (5E7X4): Worth $68, recorded on 2012-10-03 08:14:41 +0000",
    "Fluffy Bear (2A4A5): Worth $1, recorded on 2012-10-03 08:14:41 +0000",
    "Fluffy Spork (3B0R5): Worth $15, recorded on 2012-10-03 08:14:41 +0000"
)

Compare the outputs prefixed by —(1)->, —(2)->, —(3)->, and prettyPrint.

To beautify the output, I have added a category to NSArray, NSArray (MyExtensions), that contains a single method prettyPrint, and invoked that method to print the output nicely.

See Also: description methods (of which there are three) in NSArray Class Reference.


#6

It took me awhile to understand all you guys did here. I think I understood, even if there are instructions I don’t completely understand.

Anyway, this means that if we want a pretty print for every nesting we have in the code, we need to implement a method that override the default “description” of NSString description class for every and each of those nesting, right? The objects are never gonna take their original implemented description methods as long as we nest them, right?

Note: the “Foo” class made me laugh. : D