NSArray lastIndexOf / NSMutableArray lastIndexOf
by Tejas Shirodkar on Jun.09, 2013, under Development
I couldn’t find any pre existing way of getting the last index of an item in NSArray, so I ended up creating my own implementation.
Categories are a very cool feature of objective C and allow you to add functionality to pre-existing classes in a modular way.
After including the category as part of the build, a developer can simply import the category header and use lastIndexOf as it was part of NSArray or NSMutableArray.
[NSArray lastIndexOf:(id)anObject];
[NSMutableArray lastIndexOf:(id)anObject fromIndex:(NSUInteger)fromIndex];
Just add the following two files to your project and you are all set.
NSArray+LastIndexOf.h
// // NSArray+LastIndexOf.h // // Created by Tejas Shirodkar on 9/6/13. // Copyright (c) 2013 Tejas Shirodkar, tejas@tejasshirodkar.com. All rights reserved. // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. #ifndef NSArray_LastIndexOf_h #define NSArray_LastIndexOf_h @interface NSArray (LastIndexOf) - (NSUInteger)lastIndexOf:(id)anObject; - (NSUInteger)lastIndexOf:(id)anObject fromIndex:(NSUInteger)fromIndex; @end #endif
and NSArray+LastIndexOf.m
//
// NSArray+LastIndexOf.m
//
// Created by Tejas Shirodkar on 9/6/13.
// Copyright (c) 2013 Tejas Shirodkar, tejas@tejasshirodkar.com. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#include "NSArray+LastIndexOf.h"
@implementation NSArray (LastIndexOf)
- (NSUInteger)lastIndexOf:(id)anObject
{
__block NSUInteger lastIndex = NSNotFound;
[self enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
if([obj isEqual:anObject])
{
*stop = YES;
lastIndex = idx;
return;
}
}];
return lastIndex;
}
- (NSUInteger)lastIndexOf:(id)anObject fromIndex:(NSUInteger)fromIndex
{
__block NSUInteger lastIndex = NSNotFound;
[self enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
if(idx < fromIndex)
return;
if([obj isEqual:anObject])
{
*stop = YES;
lastIndex = idx;
return;
}
}];
return lastIndex;
}
@end
Happy Coding!