KWFormatter.m 1.5 KB

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