Skip to content
Browse files

Create a workable Ordinal number formatter.

  • Loading branch information...
1 parent 14ad494 commit 7f152e8d104868fb142754677537853b7ac97d27 @Abizern committed Nov 12, 2010
View
15 ADNOrdinalNumberFormatter.h
@@ -0,0 +1,15 @@
+//
+// ADNOrdinalNumberFormatter.h
+// ADNOrdinalNumberFormatter
+//
+// Created by Abizer Nasir
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface ADNOrdinalNumberFormatter : NSNumberFormatter {
+
+}
+
+@end
View
75 ADNOrdinalNumberFormatter.m
@@ -0,0 +1,75 @@
+//
+// ADNOrdinalNumberFormatter.m
+// ADNOrdinalNumberFormatter
+//
+// Created by Abizer Nasir
+//
+
+#import "ADNOrdinalNumberFormatter.h"
+
+
+@implementation ADNOrdinalNumberFormatter
+
+- (NSString *)stringForObjectValue:(id)anObject {
+ if (![anObject isKindOfClass:[NSNumber class]]) {
+ return nil; // Bail!
+ }
+
+ if ([self minimum] && [anObject integerValue] < [[self minimum] integerValue]) {
+ anObject = [self minimum];
+ }
+
+ if ([self maximum] && [anObject integerValue] > [[self maximum] integerValue]) {
+ anObject = [self maximum];
+ }
+
+ NSString *strRepresentation = [anObject stringValue];
+ NSString *lastDigit = [strRepresentation substringFromIndex:([strRepresentation length]-1)];
+
+ NSString *ordinal;
+
+ if ([strRepresentation isEqualToString:@"11"] || [strRepresentation isEqualToString:@"12"] || [strRepresentation isEqualToString:@"13"]) {
+ ordinal = @"th";
+ } else if ([lastDigit isEqualToString:@"1"]) {
+ ordinal = @"st";
+ } else if ([lastDigit isEqualToString:@"2"]) {
+ ordinal = @"nd";
+ } else if ([lastDigit isEqualToString:@"3"]) {
+ ordinal = @"rd";
+ } else {
+ ordinal = @"th";
+ }
+
+ return [NSString stringWithFormat:@"%@%@", strRepresentation, ordinal];
+}
+
+
+- (BOOL)getObjectValue:(id *)anObject forString:(NSString *)string errorDescription:(NSString **)error {
+ BOOL isSuccessful = NO;
+
+ NSScanner *scanner;
+ NSCharacterSet *letters = [NSCharacterSet letterCharacterSet];
+
+ scanner = [NSScanner scannerWithString:string];
+ [scanner setCaseSensitive:NO];
+ [scanner setCharactersToBeSkipped:letters];
+
+ NSInteger integerNumber;
+
+ if ([scanner scanInteger:&integerNumber]){
+ isSuccessful = YES;
+ if (anObject) {
+ *anObject = [NSNumber numberWithInteger:integerNumber];
+ }
+ } else {
+ if (error) {
+ *error = [NSString stringWithFormat:@"Unable to create number from %@", string];
+ }
+ }
+
+ return isSuccessful;
+}
+
+
+
+@end
View
6 ADNOrdinalNumberFormatter.xcodeproj/project.pbxproj
@@ -12,6 +12,7 @@
8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 089C165CFE840E0CC02AAC07 /* InfoPlist.strings */; };
8D11072D0486CEB800E47090 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; };
8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; };
+ EE7ECC4E128D11D000012945 /* ADNOrdinalNumberFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = EE7ECC4D128D11D000012945 /* ADNOrdinalNumberFormatter.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@@ -27,6 +28,8 @@
29B97325FDCFA39411CA2CEA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8D1107320486CEB800E47090 /* Ordinals.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Ordinals.app; sourceTree = BUILT_PRODUCTS_DIR; };
+ EE7ECC4C128D11D000012945 /* ADNOrdinalNumberFormatter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ADNOrdinalNumberFormatter.h; sourceTree = "<group>"; };
+ EE7ECC4D128D11D000012945 /* ADNOrdinalNumberFormatter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ADNOrdinalNumberFormatter.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -120,6 +123,8 @@
EE07D9A4128D08AE001D1AC7 /* Formatter Class */ = {
isa = PBXGroup;
children = (
+ EE7ECC4C128D11D000012945 /* ADNOrdinalNumberFormatter.h */,
+ EE7ECC4D128D11D000012945 /* ADNOrdinalNumberFormatter.m */,
);
name = "Formatter Class";
sourceTree = "<group>";
@@ -188,6 +193,7 @@
files = (
8D11072D0486CEB800E47090 /* main.m in Sources */,
256AC3DA0F4B6AC300CF3369 /* ApplicationDelegate.m in Sources */,
+ EE7ECC4E128D11D000012945 /* ADNOrdinalNumberFormatter.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
6 ADNOrdinalNumberFormatter.xcodeproj/project.xcworkspace/contents.xcworkspacedata
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Workspace>
+ <FileRef
+ location = "self:ADNOrdinalNumberFormatter.xcodeproj">
+ </FileRef>
+</Workspace>
View
6 ApplicationDelegate.h
@@ -8,13 +8,11 @@
#import <Cocoa/Cocoa.h>
@interface ApplicationDelegate : NSObject <NSApplicationDelegate> {
- @private
+@private
NSWindow *window;
NSTextField *inputField;
NSInteger number;
-
-
-
+
}
@property (assign) IBOutlet NSWindow *window;

0 comments on commit 7f152e8

Please sign in to comment.
Something went wrong with that request. Please try again.