ExtendNSDictionary.m 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // ExtendNSDictionary.m
  3. // Futuan
  4. //
  5. // Created by 陈 福权 on 11-12-4.
  6. // Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "ExtendNSDictionary.h"
  9. @implementation NSDictionary (util)
  10. - (NSString *)toString:(NSString *)aKey
  11. {
  12. if ([self isKindOfClass:[NSDictionary class]])
  13. {
  14. id idval = [self objectForKey:aKey];
  15. if ([idval isKindOfClass:[NSNull class]] || idval == [NSNull null])
  16. {
  17. return @"";
  18. }
  19. if (idval && idval != [NSNull null])
  20. {
  21. if ([idval respondsToSelector:@selector(length)])
  22. {
  23. return [self objectForKey:aKey];
  24. }
  25. else if ([idval respondsToSelector:@selector(intValue)])
  26. {
  27. return [NSString stringWithFormat:@"%d", [[self objectForKey:aKey] intValue]];
  28. }
  29. }
  30. else
  31. {
  32. return @"";
  33. }
  34. return @"";
  35. }
  36. else
  37. {
  38. return @"";
  39. }
  40. }
  41. - (int)toInt:(NSString *)aKey
  42. {
  43. if ([self isKindOfClass:[NSDictionary class]])
  44. {
  45. id idval = [self objectForKey:aKey];
  46. if (idval && idval != [NSNull null] && [idval respondsToSelector:@selector(intValue)])
  47. {
  48. return [[self objectForKey:aKey] intValue];
  49. }
  50. else
  51. {
  52. return 0;
  53. }
  54. return 0;
  55. }
  56. else
  57. {
  58. return 0;
  59. }
  60. }
  61. - (float)toFloat:(NSString *)aKey
  62. {
  63. if ([self isKindOfClass:[NSDictionary class]])
  64. {
  65. id idval = [self objectForKey:aKey];
  66. if (idval && idval != [NSNull null] && [idval respondsToSelector:@selector(floatValue)])
  67. {
  68. return [[self objectForKey:aKey] floatValue];
  69. }
  70. else
  71. {
  72. return 0;
  73. }
  74. return 0;
  75. }
  76. else
  77. {
  78. return 0;
  79. }
  80. }
  81. //判断是否为整形:
  82. - (BOOL)isPureInt:(NSString*) string
  83. {
  84. NSScanner* scan = [NSScanner scannerWithString:string];
  85. int val;
  86. return [scan scanInt: &val] && [scan isAtEnd];
  87. }
  88. //判断是否为浮点形
  89. - (BOOL)isPureFloat:(NSString *) string
  90. {
  91. NSScanner* scan = [NSScanner scannerWithString:string];
  92. float val;
  93. return [scan scanFloat:&val] && [scan isAtEnd];
  94. }
  95. @end