Friday, May 31, 2013

NSArray lastObject example in Objective C (iOS).


NSArray lastObject

Returns the object in the array with the highest index value.

- (id)lastObject

Return Value of [NSArray lastObject]
The object in the array with the highest index value. If the array is empty, returns nil.

NSArray lastObject example.
NSObject * obj = [[array lastObject] retain];
[array removeLastObject];
// do something with obj (in the same function)
[obj release];

Example of [NSArray lastObject].
NSArray* foo = [NSArray arrayWithObject: @"bar"];
id baz = foo.lastObject;

NSArray lastObject example.
[[NSSortDescriptor alloc] initWithKey: ((MyObject*)managedItems.lastObject).Order
                                                                   ascending: YES];
Or (better in my opinion)

NSSortDescriptor *rankSortDescriptor = [[NSSortDescriptor alloc] initWithKey: [[managedItems lastObject] Order]
                                                                   ascending: YES];

End of NSArray lastObject example article.