access to array object

ぐ巨炮叔叔 提交于 2019-12-20 06:16:38

问题


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];

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]]

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;
}

what is the problem?

thnks!!


回答1:


You should do something like this:

@property(strong, nonatomic) NSString* nameOfPhotographer;

And in initWith....

self.nameOfPhotographer = photographerName;




回答2:


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 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]);

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

Hope this helps!



来源:https://stackoverflow.com/questions/10864893/access-to-array-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!