Segev
Segev

Reputation: 19303

Accessing properties of an object inside array of objects

I have a list of items showing up on a table view. Every item has its properties such as name, pic, rank etc'. My goal is, every time the user selects a row the item with its properties will be added to a new list.

I've created a new list called listOfBugs and because i want it to be global i've allocated and initialized it inside viewDidLoad. (Is that a proper thing to do?)

Here is my code:

MasterViewController.h

@interface MasterViewController : UITableViewController
{
    NSMutableArray *listOfBugs;
}
@property (strong) NSMutableArray *bugs;

MasterViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    listOfBugs = [[NSMutableArray alloc]init];
    self.title = @"Scary Bugs";
}
...
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ScaryBugDoc *bug = [self.bugs objectAtIndex:indexPath.row];

    UIAlertView *messageAlert = [[UIAlertView alloc]
                                 initWithTitle:@"Row Selected" message:bug.data.title delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [messageAlert show];
    [listOfBugs addObject:bug];
    NSLog(@"this is %@",listOfBugs);

}

Using NSLog I can see that the objects are added:

ScaryBugs[1195:11303] this is <ScaryBugDoc: 0x75546e0>
2012-12-05 17:45:13.100 
ScaryBugs[1195:11303] this is <ScaryBugDoc: 0x75546e0>

I have a few questions.

1.How can I access the properties of the objects inside of the array listOfBugs ?

Update: This worked for me:

NSLog(@"this is %@",((ScaryBugDoc *)[listOfBugs objectAtIndex:0]).data.title);

But I can't access the listOfBugs from another class.

I turned it into a property as suggested to make my life easier but still can't access it from another class. For example in listOfBugsViewController.m return [_listOfBugs count]; will give me the error Use of undeclared identifier '_listOfBugs'

2.I want to be abale to populate a table view with the customized list, how can i do that?

After accomplishing that I would like to save the list as a plist and also add and remove objects from it at ease so I need to take that under consideration.

This is the code that I'm based on, I only made a few adjustments to create the new list

Upvotes: 0

Views: 622

Answers (2)

lnafziger
lnafziger

Reputation: 25740

This is really two questions:

1) How do I make my property a public property which can be accessed by other classes?

You do this just like you did with your bugs property. Add this to your .h file:

@property (strong) NSMutableArray *newList;

Note that if you aren't using different threads, you can make it a little more efficient by using the nonatomic property as well (@property (nonatomic, strong)).

Once you do that, you don't need your iVar declaration because it will automatically be generated for you. (i.e. you can remove NSMutableArray *newList;.)

2) How do I access an object in an array?

Objects in an array are stored as an id object, meaning that it is a "generic" object. If you know what type of object is stored, then you need to tell the compiler what it is so that it knows what properties and methods are appropriate for that class. You do this by casting the variable to the proper type:

ScaryBugDoc *bug = (ScaryBugDoc *)[self.newList objectAtIndex:0];

Then, you can access the properties of the object, assuming that they are public (as covered in point 1 above) like this:

NSLog(@"this is %s", bug.data.tile);

Upvotes: 2

CBredlow
CBredlow

Reputation: 2840

Okay, so based from the comments, this should work:

Album* tempAlbum = [albumList objectAtIndex:i];
//now you can access album's properties
Song* tempSong = [album.songs objectAtIndex:j];
//now you can access song's properties

This can be simplified down to:

Song* someSong = [((Album)[albumList objectAtIndex:i]).songs objectAtIndex:j];

When returning an object from an NSArray, or a collection object like that it will return a generic id object. This will need to be typecasted to the expected object so you can access the right properties.

Upvotes: 0

Related Questions