-1

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
4
  • What was the output you expected? Commented Dec 26, 2012 at 22:54
  • 1
    I can see nothing surprising about the output of your code compared to your code. "Timeline: 0x7292eb0" means a Timeline object with memory address 0x7292eb0. Commented Dec 26, 2012 at 22:56
  • 2
    BTW: No need to declare instance variables or use @synthesize anymore. Commented Dec 26, 2012 at 23:50
  • @occulus While my syntax is clearly wrong what I'm aiming to achieve is described above "Ultimately ... [and the nslog statement]". Commented Dec 26, 2012 at 23:56

2 Answers 2

1

The correct way to write the cast so that you can access the declared properties of the class instance would be:

NSLog(@"tlresults: %@",((Timeline *)[tlresults objectAtIndex:3]).name);

or

NSLog(@"tlresults: %@",[(Timeline *)[tlresults objectAtIndex:3] name]);

or, if you need to access a lot of properties:

Timeline *timelineAtIndex3 = [tlresults objectAtIndex:3];
NSLog(@"tlresults: %@", timelineAtIndex3.name);
Sign up to request clarification or add additional context in comments.

1 Comment

Yesss. Thank you! It looks so obvious now. And thank you for such a fleshed out answer.
0

You need to override the description method to provide your own description for the object that will be printed with NSLog().
Since you haven't overridden this method, it's used the NSObject's one, which just prints the memory address of the object.

For example:

- (NSString*) description
{
    return [NSString stringWithFormat: @"Name: %@ up: %li down: %li time of data point: %li",_name,_up,_down,_timeofdatapoint);
}

Also, by convention, the property name shouldn't be this:

@property (nonatomic) NSInteger timeofdatapoint;

But this:

@property (nonatomic) NSInteger timeOfDataPoint;

1 Comment

I should have mentioned that I already overrode -(NSString*)description; yet I want to return a parsable Timeline object so I can iterate through an array of Timeline objects and return all names (for instance): NSLog(@"tlresults: %@",(Timeline *)[tlresults objectAtIndex:3].name); I'll edit the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.