FROM : j o a r
DATE : Wed Jul 26 21:14:22 2006
On 26 jul 2006, at 20.37, Brad Bumgarner wrote:
> Basically, what I am trying to accomplish is a "shift and roll" of
> the objects in "anArray." I want to shift every object to the next
> higher position and "roll" the last object to the first position in
> the array.
How about this alternative implementation:
if ([array count] > 1)
{
[array insertObject: [array lastObject] atIndex: 0];
[array removeLastObject];
}
If you choose to retain your own implementation, keep in mind that
you would at least need to retain your "roller" object while it's not
in the array - The array could be the only one retaining it, and in
that case your implementation would crash!
There also seems to be something wrong with your for-statement
(should be "x==1", and not "x=1"), but I guess that's a simple typo?
Finally, as a matter style, I think that this method should always
return the original array, and never *nil* (unless of course, the
argument to the method is *nil* - in which case you might want to
consider raising an exception).
j o a r
PS: An alternative approach, and perhaps something that could serve
as a tiny exercise for new students of ObjC+Cocoa: This
functionality seems like something that could be added as a category
to NSMutableArray, rather than as a method in some other class.
DATE : Wed Jul 26 21:14:22 2006
On 26 jul 2006, at 20.37, Brad Bumgarner wrote:
> Basically, what I am trying to accomplish is a "shift and roll" of
> the objects in "anArray." I want to shift every object to the next
> higher position and "roll" the last object to the first position in
> the array.
How about this alternative implementation:
if ([array count] > 1)
{
[array insertObject: [array lastObject] atIndex: 0];
[array removeLastObject];
}
If you choose to retain your own implementation, keep in mind that
you would at least need to retain your "roller" object while it's not
in the array - The array could be the only one retaining it, and in
that case your implementation would crash!
There also seems to be something wrong with your for-statement
(should be "x==1", and not "x=1"), but I guess that's a simple typo?
Finally, as a matter style, I think that this method should always
return the original array, and never *nil* (unless of course, the
argument to the method is *nil* - in which case you might want to
consider raising an exception).
j o a r
PS: An alternative approach, and perhaps something that could serve
as a tiny exercise for new students of ObjC+Cocoa: This
functionality seems like something that could be added as a category
to NSMutableArray, rather than as a method in some other class.
| Related mails | Author | Date |
|---|---|---|
| Brad Bumgarner | Jul 26, 20:37 | |
| Henry McGilton | Jul 26, 20:43 | |
| David Emme | Jul 26, 20:46 | |
| Brad Bumgarner | Jul 26, 20:48 | |
| j o a r | Jul 26, 21:14 |






Cocoa mail archive

