NSFormatter クラスは抽象クラスなので、サブクラス化する必要があります。その上で、次のメソッドを実装する必要があります。
- (BOOL)isPartialStringValid:(NSString *)partialString newEditingString:(NSString **)newString errorDescription:(NSString **)error;
- (NSString *)stringForObjectValue:(id)obj;
- (BOOL)getObjectValue:(out id *)obj forString:(NSString *)string errorDescription:(out NSString **)error;
次のようなサブクラスを作成します。
.h
@interface MyFormatter : NSFormatter
@end
.m
@implementation MyFormatter
- (BOOL)isPartialStringValid:(NSString *)partialString newEditingString:(NSString **)newString errorDescription:(NSString **)error
{
// In this method you need to write the validation
// Here I'm checking whether the first character entered in the textfield is 'a' if yes, It's invalid in my case.
if ([partialString isEqualToString:@"a"])
{
NSLog(@"not valid");
return false;
}
return YES;
}
- (NSString *)stringForObjectValue:(id)obj
{
// Here you return the initial value for the object
return @"Midhun";
}
- (BOOL)getObjectValue:(out id *)obj forString:(NSString *)string errorDescription:(out NSString **)error
{
// In this method we can parse the string and pass it's value (Currently all built in formatters won't support so they just return NO, so we are doing the same here. If you are interested to do any parsing on the string you can do that here and pass YES after a successful parsing
// You can read More on that here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFormatter_Class/index.html#//apple_ref/occ/instm/NSFormatter/getObjectValue:forString:errorDescription:
return NO;
}
@end