我的问题与这个问题非常相似,但是我没有nil
从我的问题中返回,NSMutableArray
我已经分配并初始化了我的NSMutableArray
. 我将如何name
从索引处的对象中检索值Timeline
,例如 3。类似于以下内容:
NSLog(@"tlresults: %@",(Timeline *)[tlresults objectAtIndex:3].name);
并返回索引 3 的 Timeline.name 值。
时间线.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
时间线.m
#import "Timeline.h"
@implementation Timeline
@synthesize name = _name;
@synthesize up = _up;
@synthesize down = _down;
@synthesize timeofdatapoint = _timeofdatapoint;
@end
添加对象和测试检索的功能:
#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]);
...
输出:
tlresults count: 10
tlresults: Timeline: 0x7292eb0