• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Accessing a property of a returned object?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accessing a property of a returned object?


  • Subject: Re: Accessing a property of a returned object?
  • From: Wayne Packard <email@hidden>
  • Date: Sun, 27 Jul 2008 00:35:31 -0700

Xcode is complaining because objectAtIndex: returns an id. NSMutableArray doesn't know/care what type(s) of objects it's holding. If you want to use the dot syntax, you'll need to cast the id returned from objectAtIndex: to the type of object it really is (a Unit* in this case). Alternately, if your Unit object has a property called nextUnit, you could do something like this without a cast:

[[army objectAtIndex: i-1] setNextUnit: [army lastObject]]; // i.e., retrieve the object at i-1 and send it a message to set the next unit, passing as a parameter the last object in the army array.

This will fail at runtime if the object stored at that position in the array does not know how to handle the setNextUnit: message.

On a different note, it seems superfluous to store a linked list of objects in an array. The array provides them with an order so you could just iterate over it to operate on each object in the list.

Conversely, you could skip the array and just create the objects in your loop, setting the next object pointer as you make them. Store a pointer to the first object and then iterate the list by following the links.

If the order sometimes changes, you could either create a new array with the correct order, or if you're not using an array, just fix up the link pointers.

wp


On Jul 26, 2008, at 11:33 PM, Mark Teagarden wrote:

Hi,

I'm working on a strategy game in which a NSMutableArray called 'army' contains a series of Unit objects. Each Unit contains a property called next_unit, which is a pointer to the next unit in the array - I'm implementing a linked-list so that units can be moved sequentially.

Army starts off with six units, which are assigned one at a time to the array:

for(i=0;i<6;i++){
	u = [[Unit alloc] initWithPositionX:24+i Y:14-i];
	[army addObject:u];
	...
	[u release];
}

Obviously when the first unit is added, there's nothing for next_unit to point to, so I have to go back and do it on the next loop iteration, which is where my problem arises:

	if(i > 0) [army objectAtIndex:i-1].next_unit = [army lastObject];

Clearly, what I want to do is get the next-to-last unit in army and point its next_unit pointer to the most recently added unit. However, XCode she don't-a like this:

error: request for member 'next_unit' in something not a structure or union

Is there a recommended way for accessing an ivar with synthesized gettors/settors, that belongs to a returned object?

I hope I've explained my problem clearly enough. Thanks for an advice you may offer.

Mark


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Accessing a property of a returned object? (From: Mark Teagarden <email@hidden>)

  • Prev by Date: Re: Accessing a property of a returned object?
  • Next by Date: Re: Accessing a property of a returned object?
  • Previous by thread: Re: Accessing a property of a returned object?
  • Next by thread: Re: Accessing a property of a returned object?
  • Index(es):
    • Date
    • Thread