Challenge 19 Example (No Loops, No Random Values)


#1

Parent.h

[code]#import <Foundation/Foundation.h>
@class Children;

@interface Parent : NSObject {
NSMutableArray *ChildrenArray;
NSString *NameOfParent;
}
@property NSMutableArray *ChildrenArray;
@property NSString *NameOfParent;
-(void)addYourSelfToArray:(NSMutableArray *)ParentsArray;
-(void)addChildrenArrayObject:(Children *)object;
@end[/code]

Parent.m

[code]#import “Parent.h”

@implementation Parent
@synthesize ChildrenArray;
@synthesize NameOfParent;
-(void)addYourSelfToArray:(NSMutableArray *)ParentsArray {
[ParentsArray addObject:self];
}
-(void)addChildrenArrayObject:(Children *)object {
if (!ChildrenArray) {
ChildrenArray = [[NSMutableArray alloc] init];
}
[ChildrenArray addObject:object];
}
-(NSString *)description {
return [NSString stringWithFormat:@"%@ have %lu children", [NameOfParent self], [ChildrenArray count]];
}
-(void)dealloc {
NSLog(@"%@ have %lu children who are:", [NameOfParent self], [ChildrenArray count]);
}
@end[/code]

Children.h

[code]#import <Foundation/Foundation.h>

@interface Children : NSObject {
NSString *NameOfChild;
}
@property NSString *NameOfChild;
@end[/code]

Children.m

[code]#import “Children.h”

@implementation Children
@synthesize NameOfChild;
-(NSString *)description {
return [NSString stringWithFormat:@"%@", [NameOfChild self]];
}
-(void)dealloc {
NSLog(@"%@", self);
}
@end[/code]

main.m

[code]#import <Foundation/Foundation.h>
#import “Parent.h”
#import “Children.h”

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

@autoreleasepool {
    NSMutableArray *ParentsArray = [[NSMutableArray alloc]init];
    Parent *parent = [[Parent alloc]init];
    [parent setNameOfParent:[NSString stringWithFormat:@"Pablo Mondragón"]];
    [parent addYourSelfToArray:ParentsArray];
    Parent *parent1 = [[Parent alloc]init];
    [parent1 setNameOfParent:[NSString stringWithFormat:@"Parent1"]];
    [parent1 addYourSelfToArray:ParentsArray];
    Children *childR = [[Children alloc]init];
    Children *childB = [[Children alloc]init];
    Children *child1 = [[Children alloc]init];
    Children *child2 = [[Children alloc]init];
    Children *child3 = [[Children alloc]init];
    Children *child4 = [[Children alloc]init];
    [childR setNameOfChild:[NSString stringWithFormat:@"Rodrigo Mondragón"]];
    [childB setNameOfChild:[NSString stringWithFormat:@"Bernardo Mondragón"]];
    [child1 setNameOfChild:[NSString stringWithFormat:@"child 1"]];
    [child2 setNameOfChild:[NSString stringWithFormat:@"child 2"]];
    [child3 setNameOfChild:[NSString stringWithFormat:@"child 3"]];
    [child4 setNameOfChild:[NSString stringWithFormat:@"child 4"]];
    [parent addChildrenArrayObject:childR];
    [parent addChildrenArrayObject:childB];
    [parent1 addChildrenArrayObject:child1];
    [parent1 addChildrenArrayObject:child2];
    [parent1 addChildrenArrayObject:child3];
    [parent1 addChildrenArrayObject:child4];
     NSLog(@"%@",ParentsArray);
}
return 0;

}[/code]

This is the output:

2013-07-31 14:05:39.238 Children[922:303] (
“Pablo Mondragon have 2 children”,
“Parent1 have 4 children”
)
2013-07-31 14:05:39.240 Children[922:303] Pablo Mondragon have 2 children who are:
2013-07-31 14:05:39.240 Children[922:303] Rodrigo Mondragón
2013-07-31 14:05:39.241 Children[922:303] Bernardo Mondragón
2013-07-31 14:05:39.241 Children[922:303] Parent1 have 4 children who are:
2013-07-31 14:05:39.242 Children[922:303] child 1
2013-07-31 14:05:39.242 Children[922:303] child 2
2013-07-31 14:05:39.243 Children[922:303] child 3
2013-07-31 14:05:39.243 Children[922:303] child 4