main
1//
2// Licensed under the terms in License.txt
3//
4// Copyright 2010 Allen Ding. All rights reserved.
5//
6
7#import "KWFormatter.h"
8
9@implementation KWFormatter
10
11
12#pragma mark - Getting Descriptions
13
14+ (NSString *)formatObject:(id)anObject {
15 if ([anObject isKindOfClass:[NSString class]])
16 return [NSString stringWithFormat:@"\"%@\"", anObject];
17
18 else if ([anObject isKindOfClass:[NSDictionary class]])
19 return [anObject description]; // NSDictionary conforms to NSFastEnumeration
20
21 else if ([anObject conformsToProtocol:@protocol(NSFastEnumeration)])
22 return [self formattedCollection:anObject];
23
24 return [anObject description];
25}
26
27
28
29#pragma mark - Private
30
31+ (NSString *)formattedCollection:(id<NSFastEnumeration>)collection {
32
33 NSMutableString *description = [[[NSMutableString alloc] initWithString:@"("] autorelease];
34 NSUInteger index = 0;
35
36 for (id object in collection) {
37 if (index == 0)
38 [description appendFormat:@"%@", [self formatObject:object]];
39 else
40 [description appendFormat:@", %@", [self formatObject:object]];
41
42 ++index;
43 }
44
45 [description appendString:@")"];
46 return description;
47}
48
49
50
51@end