My question is very similar to this one however I am not returning nil from my NSMutableArray and I have alloc'ed and init'ed my NSMutableArray. How would I retrieve the name value from the Timeline object at index, for example, 3.
Something similar to the following:
NSLog(@"tlresults: %@",(Timeline *)[tlresults objectAtIndex:3].name);
and return the Timeline.name value for index 3.
Timeline.h
@interface Timeline : NSObject
{
NSString *_name;
NSInteger _up;
NSInteger _down;
NSInteger _timeofdatapoint;
}
@property (nonatomic,retain) NSString *name;
@property (nonatomic) NSInteger up;
@property (nonatomic) NSInteger down;
@property (nonatomic) NSInteger timeofdatapoint;
@end
Timeline.m
#import "Timeline.h"
@implementation Timeline
@synthesize name = _name;
@synthesize up = _up;
@synthesize down = _down;
@synthesize timeofdatapoint = _timeofdatapoint;
@end
Function adding objects to and testing retrieval:
#import "Timeline.h"
...
NSMutableArray *tlresults = [[NSMutableArray alloc] init];
for (int i=0; i<10; i++) {
Timeline *tlobj = [Timeline new];
tlobj.name = username;
tlobj.up = 2*i;
tlobj.down = 5*i;
tlobj.timeofdatapoint = 2300*i;
[tlresults addObject:tlobj];
[tlobj release];
}
NSLog(@"tlresults count: %d",[tlresults count]);
NSLog(@"marray tlresults: %@",(Timeline *)[tlresults objectAtIndex:3]);
...
output:
tlresults count: 10
tlresults: Timeline: 0x7292eb0