【发布时间】:2012-06-02 18:32:46
【问题描述】:
我创建一个数组并用对象对其进行初始化。我试图访问数组对象,但我得到一个(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]]
更新:照片项目代码:
-(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;
}
有什么问题?
谢谢!!
【问题讨论】:
-
摄影师你叫什么名字?
-
可能你需要输入你的数组结果。
-
当 nameOfPhotographer 可能返回一个字符串时,为什么要使用 [NSString stringWithFormat ...]?
标签: objective-c xcode arrays object nsmutablearray