NSArray
documentation is your friend.
Anyway, to access an object array you could use - (id)objectAtIndex:(NSUInteger)index
method of NSArray
.
What Seva Alekseyev is saying that your class has to be structured in a different way. For example:
//.h
@interface MyCustomObject : NSObject
{
// you dont need to declare variables, the compiler will do it for you
}
@property (nonatomic, copy) NSString* objObservation;
@property (nonatomic, assign) NSInteger objId;
@property (nonatomic, copy) NSString* objDate;
@property (nonatomic, copy) NSString* objDevice;
@property (nonatomic, assign) double objLatitude;
@property (nonatomic, assign) double objLongitude;
@end
//.m
@implementation MyCustomObject
@synthesize objId;
@synthesize objDate;
@synthesize objDevice;
@synthesize objLatitude;
@synthesize objLongitude;
@synthesize objObservation;
- (void)dealloc
{
/* uncomment if you don't use ARC
[objDate release];
[objDevice release];
[objObservation release];
[super dealloc];
*/
}
@end
Use your class like this (you need to #import "MyCustomObject.h"
)
// populate your object
MyCustomObject* myObj = [[MyCustomObject alloc] init];
myObj.objDate = @"yourDate";
myObj.objDevice = @"yourDevice";
myObj.objId = 1;
myObj.objLatitude = 22.0;
myObj.objLongitude = 23.87;
myObj.objObservation = @"yourObservation";
// insert it into your array (here only for test purposes but you could create an instance variable for store it)
NSArray* myArray = [NSArray arrayWithObjects:myObj, nil];
// if you don't use ARC
[myObj release];
// grab the object from the array
MyCustomObject * currentObj = [myArray objectAtIndex:0];
// see its values, for example
NSLog(@"%@", [currentObj objDevice]);
Now I would add some notes.
- When you use class, called them with an upper case letter, e.g.
MyClass
and not myclass
, variables instead start with a lower case, e.g. myVariable
- If your array can be populated in different times, you need a
NSMutableArray
. A NSArray
cannot be changed once created. NSMutableArray
instead is dynamic, you can add or remove objects to it
- When you deal with objects, you should wrap your variables with properties (in this case you don't need variables since the compiler will provide them with
@synthesize
directive). This allow you to not break objects encapsulation
- You could think to provide an initializer to your object if you want
When you write question try to put some context around it and try to explain what you have done and what do you want to achieve.