-2

I have an object like this:

@interface object : NSObject
{    
    NSString *_observation;    
    NSInteger _id;    
    NSString *_date;
    NSString *_device;  
    double _latitude;
    double _longitude; 
}

So as you see it has some attributes. Then I create an NSMutableArray that is full with objects like the one above, like

array[object, object ,object, ....]

What I want to ask is how can I process a specific attribute of a specific object into the array? For example how can I process the first's object id attribute into the array in obj-c?

Thank you very much!

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • What do you mean by "process an attribute into the array"? Do you want to insert an attribute into an array? Or retrieve the attribute of an object in array? By the way, you need to create some properties around those variables, otherwise you won't be able to access them from outside the `object` class. – Seva Alekseyev Jul 06 '12 at 15:08
  • retrieve the attribute of an object of the array. of course i have created properties this is just a pseudoCode example – user1507121 Jul 06 '12 at 15:10
  • 1
    It is very difficult to understand what it is you are asking about, can you provide an example of the type of thing it is you want to do? – Dan F Jul 06 '12 at 15:13
  • really??? is my english so bad? i want for example this: array.firstObject.id how can i refer to this thing? – user1507121 Jul 06 '12 at 15:14

4 Answers4

3

If I understand the question correctly, you want to access an object's instance variables once it is placed inside an NSMutableArray.

If the question is that easy, this is your solution:

NSInteger *currID = [array objectAtIndex:x].id;

Furthermore, you can use fast enumeration to go through the entire array accessing each element's variables.

NSInteger *currID;
for (NSObject *currentObject in array) {
    currID = currentObject.id;
    //proccess ID
}

This assumes you have set up @property for id like so in the .h file:

@property (nonatomic) BOOL latitude;
@property (nonatomic) BOOL longitude;
@property (nonatomic, copy) NSString *observation;    
@property (nonatomic) NSInteger id;    
@property (nonatomic, copy) NSString *date;
@property (nonatomic, copy) NSString *device;

And of course in the .m:

@synthesize latitude;
@synthesize longitude;
@synthesize observation;
@synthesize id;
@synthesize date;
@synthesize device;
Michael Boselowitz
  • 3,012
  • 1
  • 19
  • 22
0

In order to access members of an object within an array you need to get a pointer to that object. ie:

object *firstObject = [array objectAtIndex:0];
firstObject.id = 0;
object *secondObject = [array objectAtIndex:1;
...
Dan F
  • 17,654
  • 5
  • 72
  • 110
0

Either

[[array objectAtIndex:0] id]

or

[array objectAtIndex:0].id

Assuming the property around the _id data member is called id. This is a property getter call, not an direct ivar access.

NSArray does not and can not emulate C arrays with their a[i] indexing notation. This is not C++, where the [] operator can be redefined. In the lines above, the brackets represent ObjC method calls (AKA "message passing"), not array indexing.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
0

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.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190