Maulik
Maulik

Reputation: 19418

objective c interface access

this my first interface in a.h file.

@interface EventRow : NSObject 
{
NSString *title;
NSString *photo;
NSString *description;

NSMutableArray *photoArray;
}
@property (nonatomic , retain) NSString *title;
@property (nonatomic , retain) NSString *photo;
@property (nonatomic , retain) NSString *description;

@property (nonatomic , retain) NSMutableArray *photoArray;

@end

in the same file second

@interface PhotoRow : NSObject
{
//NSString *image;

NSMutableArray *imageArray;
}
@property (nonatomic , retain) NSMutableArray *imageArray;

@end

Now every object of "PhotoRow" with filled array(imageArray) stored into the "photoArray" array in EventRow's object.

Now I want to count the total elements of "imageArray" . But getting problem in access it through the EventRow's object.

any suggestions ?

Thanks..

Upvotes: 1

Views: 109

Answers (2)

JeremyP
JeremyP

Reputation: 86691

To access the individual objects in photoArray (eventRow is an instance of EventRow

[[eventRow photoArray] objectAtIndex: someIndex]; // I don't like dot notation!

or

[eventRow.photoArray objectAtIndex: someIndex];

To access the imageArray

[[[eventRow photoArray] objectAtIndex: someIndex] imageArray];

or

[eventRow.photoArray objectAtIndex: someIndex].imageArray;

To get the count of images for that imageArray

[[[[eventRow photoArray] objectAtIndex: someIndex] imageArray] count];

or

[eventRow.photoArray objectAtIndex: someIndex].imageArray.count;

If you want to count all of the images, you need loops but you can use fast enumeration to simplifiy things

size_t total = 0;
for (PhotoRow* photoRow in  [eventRow photoArray])
{
    total += [[photoRow imageArray] count];
}

However, I'd like you to rethink your design a bit. Your exposure of the NSMutableArray in each class breaks encapsulation. Once a caller has got hold of the array, it can modify the internal state of a PhotoRow or an EventRow without the object knowing about it. It would be better not to have the NSMutableArray properties but to add methods to add images and photoRows directly to PhotoRows and EventRows respectively. So, for instance your photoRow class might have the following methods:

-(size_t) imageCount; // returns the result of sending -count to the internal array
-(NSImage*) imageAtIndex: (size_t) index; // returns the result of sending -objectAtIndex: to the underlying array
-(void) addImage: (NSImage*) newImage;
// etc

Upvotes: 3

FreeAsInBeer
FreeAsInBeer

Reputation: 12979

If I understand you correctly, your photoArray variable in your EventRow class contains PhotoRow objects. You can count the objects in the PhotoRows imageArray variable like so:

int someIndex = 0;
[((PhotoRow*)[myEventRow.photoArray objectAtIndex:someIndex]).imageArray count];

Upvotes: 3

Related Questions