访问数组对象 [英] access to array object
问题描述
我创建一个数组,我和对象的初始化。我试图让访问数组对象,但我得到一个(空)。我该怎么办错了?
i create an array and i initialize it with objects. i tried to get access to the array object but i get a (null). what do i do wrong?
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];
我试图通过这条线code(即回报(NULL))来获得访问对象之一:
i try to get access to one of the objects by this line of code(that return (null)):
photoName.text = [NSString stringWithFormat:@"%@", [[photoArray objectAtIndex:2] nameOfPhotographer]]
更新:photoitem的code:
update: code of 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;
}
这是什么问题?
thnks !!
推荐答案
首先,确保你的PhotoItem接口文件与此类似:
First, make sure your PhotoItem Interface file is similar to this:
@interface PhotoItem : NSObject
{
UIImage *imageItem;
NSString *name;
NSString *photographer;
}
@property (nonatomic, retain) UIImage *imageItem;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *photographer;
@end
二,确保您的实现部分是这样的:
Second make sure your implementation is partly like this:
@implementation PhotoItem
@synthesize imageItem, name, photographer;
-(id)initWithPhoto:(UIImage*)image name:(NSString*)photoName photographer:(NSString*)photographerName
{
self = [super init];
if(self)
{
self.imageItem = image;
self.name = photoName;
self.photographer = photographerName;
}
return self;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"name:%@\nphotographer:%@", self.name, self.photographer];
}
@end
现在起介绍,当你登录一个对象上的默认调用,你真的可以诊断你的问题很容易了。
Now since description is the default call on an object when you log it, you can really diagnose your problem easily now.
我添加的类文件,向您展示了便捷的属性如何都可以。
I added the class files to show you how convenient properties can be.
现在,当您要访问说,摄影师的名字,只是做:
Now when you want to access say the name of the photographer, just do:
photo1.photographer
OK。现在在code,而不是抛出一切到一个数组马上这样做是为了确保一切正常:
Ok now in your code, instead of throwing everything into an array right away do this to make sure things are working:
PhotoItem *photo1 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"1.jpg"] name:@"roy rest" photographer:@"roy"];
NSLog(@"%@", [photo1 description]);
他们应该实际上,类code我已经给你应该能正常运行。现在,把他们都到一个数组根据需要,并使阵列中确认一切及格做这样的事情:
They should be actually, the class code I've given you should work fine. Now put them all into an array as desired, and to make sure everything in the array is up to snuff do something like this:
NSLog(@"%@", photoArray); //Description is the default when logging an object
//An array will call the description method on each of it's containing objects
希望这有助于!
这篇关于访问数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!