我创建一个数组并用对象初始化它。我试图访问数组对象,但我得到一个(null)。我做错了什么?
photoArray = [[NSMutableArray alloc] init];
PhotoItem *photo1 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"1.jpg"] name:@"roy rest" photographer:@"roy"];
PhotoItem *photo2 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"2.jpg"] name:@"roy's hand" photographer:@"roy"];
PhotoItem *photo3 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"3.jpg"] name:@"sapir first" photographer:@"sapir"];
PhotoItem *photo4 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"4.jpg"] name:@"sapir second" photographer:@"sapir"];
[photoArray addObject:photo1];
[photoArray addObject:photo2];
[photoArray addObject:photo3];
[photoArray addObject:photo4];
我尝试通过这行代码访问其中一个对象(返回(null)):
photoName.text = [NSString stringWithFormat:@"%@", [[photoArray objectAtIndex:2] nameOfPhotographer]]
更新:photoitem的代码:
-(id)initWithPhoto:(UIImage*)image name:(NSString*)photoName photographer:(NSString*)photographerName
{
self = [super init];
if(self)
{
imageItem = image;
name = photoName;
nameOfPhotographer = photographerName;
//[self setName:photoName];
//[self setNameOfPhotographer:photographerName];
//[self setImageItem:image];
}
return self;
}
问题是什么?
谢谢!!