luca
luca

Reputation: 37136

Reading NSobject property from within NSArray

I created this Class to store Rss feeds about Cd-rooms:

CdObject.h

@interface CdObject : NSObject{


NSString *title,*artist,*country,*company,*price,*year;

}

@property(nonatomic,retain)NSString *title,*artist,*country,*company,*price,*year;

CdObject.m

#import "CdObject.h"

@implementation CdObject
@synthesize title,artist,country,company,price,year;

@end

Then while parsing the xml i'm creating new instances of this class filling their properties (title,artist ect..)and pushing them into a NSMutableArray (cdListArray)

How could I read the property of one of these objects from inside cdListArray?

Ex:NSLog(@"%@",[cdListArray objectAtIndex:0].title) ??? 

Upvotes: 1

Views: 870

Answers (4)

Damien Locque
Damien Locque

Reputation: 1809

I think you need to cast:

NSLog(@"%@",[(CdObject *)[cdListArray objectAtIndex:0]] title);  

Upvotes: 0

Gobra
Gobra

Reputation: 4261

IMHO, the simples way in all such cases is valueForKey: and it's brother valueForKeyPath:

Upvotes: 3

Dan F
Dan F

Reputation: 17732

You simply cast it to the object you want. If you're paranoid about type casting that way, you can also check the object's class before the cast.

Ex:

NSObject *firstObject = [cdListArray objectAtIndex:0];
if ( [firstObject isKindOfClass:[CdObject class]] )
{
    CdObject* cdFirstObject = (CdObject*)firstObject;
    NSLog(@"%@", cdFirstObject.title;
}

Upvotes: 1

medvedNick
medvedNick

Reputation: 4510

Try NSLog(@"%@",((CdObject*)[cdListArray objectAtIndex:0]).title)

Upvotes: 1

Related Questions