Tutorial :Objective-C: Accessing Member Objects that are in an array?!? I think



Question:

Totally new to Obj-C, so thanks for patience. :P

Because I'm beginner, I will use the car example. Easier for me to understand.

I have an object, Car. It has two member objects, tire and engine.

Tire and engine have their own member variables too, but they are just int with various names (like pressure, treadDepth).

In all these cases, I have synthesized accessor methods. I'm not sure about accessor methods for objects, so I just did @property id engine / @property id tire. Hope that is right!

Now, I can do dot.notation style to access like: [car.engine cylinders]. Fine! Sending tire and engine messages works fine. I write methods, this notation seems to work.

But when I declare an array of objects like 4 tires for the car:

@interface Car : NSObject {       tire *tires[4];  }  

I cannot send it message like this

[car.tire[0] setPressure: int];  

It says accessing unknown tires getter method.

Basically I am wondering if someone can help me understand how to correctly access member variables of an object that is in an array.

Thanks!


Solution:1

You are trying to call a getter on car that doesn't exist. You can't return a C-style array by value anyway, so instead of just returning a Tire* pointer i'd rather use a NSArray in this case:

// header:  @interface Car : NSObject {      NSArray *tires;  }  @property (nonatomic, copy) tires;  // ...    // source:  @implementation Car  @synthesize tires;    - (id)init {      if ((self = [super init])) {          tires = [[NSArray alloc] initWithObjects:                    [[[Tire alloc] init] autorelease],                    // ...                    nil];          // ...      }      return self;  }    - (void)dealloc {      [tires release]; // don't forget to clean up      // ...  }  

Now you could use the getter:

[[[car.tires] objectAtIndex:0] setPressure:0];  


Solution:2

Ah, ok. Ooops!

Need to write an indexed accessor. That is not part of the @synthesize deal? Oh man!

:P


Solution:3

Why not put all of your tire objects into an NSArray or NSSet? Or, since you know there are only four, you could simply define frontLeftTire, frontRightTire, etc. properties.


Solution:4

Well you could use Objective-C style arrays. Then you would have something like:

NSArray *tires = [NSArray arrayWithObjects: tire1, tire2, tire3, tire4];  

And then you would access them as:

[tires objectAtIndex:0];  

That's assuming you are using the synthesized methods as described. I'm not sure from your question, but it seems like you might want to define a class "tire" for these objects (rather than just a method, which is all I see above) that inherits from NSObject, or maybe from your own class CarPart, etc. Then you allocate 4 tires in a loop and call an init method that sets up some default state (hopefully better than the donut that came as the spare in my car) and then add them to your array in "Car" when you initialize a car.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »