0

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

APRULE
  • 105
  • 3
  • 11

2 Answers2

0

You should do something like this:

@property(strong, nonatomic) NSString* nameOfPhotographer;

And in initWith....

self.nameOfPhotographer = photographerName;

user1078065
  • 412
  • 1
  • 5
  • 19
0

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!

Benjamin
  • 1,832
  • 1
  • 17
  • 27
  • per the discussion here, http://stackoverflow.com/questions/192721/why-shouldnt-i-use-objective-c-2-0-accessors-in-init-dealloc, you should not use property accessors in your class' init and dealloc methods. – timthetoolman Jun 03 '12 at 16:11
  • @timthetoolman that is actually not true. using self.*something* = passed_variable causes this variable to be retained due to the definition of the accessor. Since these variables will be autoreleased, possibly immediately after this init method, the pointer reference will cause a BAD_ACCESS at some point if you just did something = passed_variable. Now if you did something = [passed_variable retain], that would be fine, as long as you released it later. APRULE never said anything about what environment he/she was using: this method is safe and sound and makes perfect sense. – Benjamin Jun 04 '12 at 01:30
  • 2
    I tend to listen to the folks like mmalc who work for Apple and really know their stuff. If they say not to use the property accessors in init and dealloc, I take it as gospel. if it needs to be retained, unless you are using ARC, then you do name =[photoName retain]; etc. If you are using ARC, then you don't need to worry about it. Mike Ash also comments here: http://www.mikeash.com/pyblog/friday-qa-2009-11-27-using-accessors-in-init-and-dealloc.html . These guys are true pros. Ignore them at your own risk. There be dragons. – timthetoolman Jun 04 '12 at 02:18
  • Thank you for the tip @timthetoolman. Thankfully this is a simple situation where it seemingly doesn't really matter. I shall be changing up my styles in inits and deallocs now after reading through everything =) – Benjamin Jun 04 '12 at 05:41