Posts Tagged ‘code share’

【iPhone App Dev】Extend NSString with “IndexOf” method

Wednesday, September 7th, 2011

The NSString object in Objective-C is not powerful enough to handle every case, so I plan to extend more methods by myself. The first is a “indexOf” method, it’s just like the method in Java did. I wrote a category to extend original NSString object. Here the code is…

ExtendNSString.h:

#import <Foundation/Foundation.h>

@interface NSString (util)

- (int) indexOf:(NSString *)text;

@end

ExtendNSStriing.m:

#import "ExtendNSString.h"

@implementation NSString (util)

- (int) indexOf:(NSString *)text {
    NSRange range = [self rangeOfString:text];
    if ( range.length > 0 ) {
        return range.location;
    } else {
        return -1;
    }
}

@end