Best related discussions I’ve turned up: Making an array of Objects in Objective-C.. Scanned through links that came up when I prepped this post.
Self-study mode, creating mini-apps to reinforce and extend ideas I get working on book tutorials. …………….
Goal: To store some number of objects (example class ‘Trip’) in a mutable array, contained in another object (example class ‘TourCompany’).
(portions of the two different class interface files)
@interface Trip : NSObject
@property (strong, nonatomic) NSString *travelToLocation;
……
@interface TourCompany : NSObject
@property (strong, nonatomic) NSMutableArray *trips ;
…… CODE AND GOAL IN PIECES....
1) Demonstrate that I created 3 trip objects and can access the travelToLocation ivar ....
Trip *trip0 = [[Trip alloc] init];
Trip *trip1 = [[Trip alloc] init];
Trip *trip2 = [[Trip alloc] init];
[trip0 setTravelToLocation:@"Vienna"];
[trip1 setTravelToLocation:@"Mt. St. Helens"];
[trip2 setTravelToLocation:@"Tenochtitlan"];
NSString *somePlace1 = trip1.travelToLocation;
NSLog(@" %@ %@, \n \t Let's LOOK at one particular trip ivar %@.", twoBlanks, nuLn, somePlace1);
// CONSOLE RESULTS
// Let's LOOK at one particular trip ivar Mt. St. Helens.
....
2) Demonstrate that I
i) can store these 3 trip objects in a locally declared NSMutable Array
ii) can access the travelToLocation ivar
....
// put trip objects into a LOCALLY DECLARED mutable array
NSMutableArray *localTripsArray = [NSMutableArray array ];
//[myArray addObject: someOtherPerson]; - compare to a STackOverflow discussion
[localTripsArray addObject:trip0];
[localTripsArray addObject:trip1];
[localTripsArray addObject:trip2];
for (Trip *t in localTripsArray) {
NSLog(@"\n \t ^^^Trip location in LOCAL TRIPS Mutable ARRAY is %@", t.travelToLocation );
}
/*
CONSOLE RESULTS
objectInMutableArrayMiniAppChallenge[521:f803]
^^^Trip location in LOCAL TRIPS Mutable ARRAY is Vienna
2012-07-05 13:47:17.172 objectInMutableArrayMiniAppChallenge[521:f803]
^^^Trip location in LOCAL TRIPS Mutable ARRAY is Mt. St. Helens
2012-07-05 13:47:17.172 objectInMutableArrayMiniAppChallenge[521:f803]
^^^Trip location in LOCAL TRIPS Mutable ARRAY is Tenochtitlan
*/
.... 3) BUT I cannot figure out why I am not storing these 3 objects an NSMutable Array ('trips') in an instance of my TourCompany class ('friendlySkiesTourCo') ... // NOW instantiate an object in my TourCompany class
TourCompany *friendlySkiesTourCo = [[TourCompany alloc] init];
// put those same objects into the trips ivar in that object
//[myArray addObject: someOtherPerson];
// - compare to a STackOverflow discussion
//only difference I see is that instead of locally declared 'myArray'
// I'm using an ivar that's a member of an object
// I know there's are values in those objects, because I displayed them above
[friendlySkiesTourCo.trips addObject:trip0];
[friendlySkiesTourCo.trips addObject:trip1];
[friendlySkiesTourCo.trips addObject:trip2];
for (Trip *t in friendlySkiesTourCo.trips) {
NSLog(@"\n \t +++ Trip location in 'trips' mutable array in the object 'friendlySkiesTourCo', a TourCompany class object, is %@", t.travelToLocation );
}
/* Console NON-Results
I never get any output from this NSLog directive
I'll bet there is something pretty basic I don't understand
No doubt, it's in the Gol Durned Manual online , but I'm not seeing it
*/
....
4. Let's just double check that the problem is possibly a setting type problem
....
// I don't think I'm really putting the objects into this particular mutable array in the friendlySkies object correctly
// Let's just check
int howManyTripsInMutableArray = [friendlySkiesTourCo.trips count];
NSLog(@"\n \tThere are %d trips for the friendlySkiesTour Co ", howManyTripsInMutableArray);
/*
Sure enough...
2012-07-05 13:53:53.235 objectInMutableArrayMiniAppChallenge[555:f803]
There are 0 trips for the friendlySkiesTour Co
*/
Thanks for any clues. I hope that I did this writeup succinctly and that you don't think I just didn't RTGDM (that's 'gol durn' in case you wondered), because I sure tried to.
Laurel