0

I am having trouble in accessing an array within a structure. How can I access an array which is stored in a structure ? I have a structure, say,

@interface FoodDriveRow : NSObject
{
NSMutableArray *teamData;
}
@property (nonatomic , retain) NSMutableArray *teamData;
@end

FoodDriveRow *row ;

now I want to access the array.

Edit

@interface TeamRow : NSObject
{
NSString *membername;
NSString *email;
NSString *phone;    
}
@property (nonatomic , retain) NSString *membername;
@property (nonatomic , retain) NSString *email;
@property (nonatomic , retain) NSString *phone;
@end

I am trying to store an object of TeamRow in to the array by

[row.teamData insertObject:tRow atIndex:tIndex1];

and want get the values from the array.

Thanks...

3
  • 1
    can you please give some more information Commented Apr 6, 2011 at 12:46
  • I think you need to learn more about Objective-C and Cocoa before asking such questions. Commented Apr 6, 2011 at 12:52
  • 1
    you only ask questions when you are learning... Commented Apr 6, 2011 at 12:59

6 Answers 6

4

You need to allocate the array, at the moment your array has a nil value.

Impliment -(id)init

Then within that you want to allocate your array and add some objects to it:

teamData = [[NSMutableArray alloc] init];
TeamRow *teamRow = [[TeamRow alloc] init]; 
[teamData addObject:teamRow];
[teamRow release];

Now you can access objects:

TeamRow *retrievedTeamRow = [teamData objectAtIndex:0]; 
Sign up to request clarification or add additional context in comments.

Comments

3
FoodDriveRow* myDriveRow;

Use it as below , lets suppose you store NSString object in array

For Adding

- (void)addObject:(id)anObject

[myDriveRow.teamData  addObject:myString];

For Removing

(void)removeObject:(id)anObject

For Accessing

- (id)objectAtIndex:(NSUInteger)index

2 Comments

ya thats true I can store values by [row.teamData insertObject:tRow atIndex:tIndex]; but I want to fetch the values from it
@maulik : Use --> myObjectStoredInArray = [row.teamData objectAtIndex:tIndex];
2

Did you try [row.teamData objectAtIndex:0] or from within the class itself, [self.teamData objectAtIndex:0]?

Comments

1

I am not sure I understand you but I think you can do this by row.teamData

2 Comments

by row.teamData I could not access the value of an array at specific index
[row.teamData objectAtIndex:indexValue]; will get you that
1
[[row teamData] objectAtIndex: i];

where i is the index in the array.

Comments

1

This isn't a structure (struct) that you are referring to, it's a class called FoodDriveRow that descends directly from NSObject. In order to access properties from an object, you can use the old style:

[[row teamData] objectAtIndex:i]

Or you can use the new dot syntax:

[row.teamData objectAtIndex:i]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.