No Description

NSDictionary+SafeAccess.m 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. //
  2. // NSDictionary+SafeAccess.m
  3. // IOS-Categories
  4. //
  5. // Created by Jakey on 15/1/25.
  6. // Copyright (c) 2015年 www.skyfox.org. All rights reserved.
  7. //
  8. #import "NSDictionary+SafeAccess.h"
  9. @implementation NSDictionary (SafeAccess)
  10. - (BOOL)hasKey:(NSString *)key
  11. {
  12. return [self objectForKey:key] != nil;
  13. }
  14. - (NSString*)stringForKey:(id)key
  15. {
  16. id value = [self objectForKey:key];
  17. if (value == nil || value == [NSNull null])
  18. {
  19. return nil;
  20. }
  21. if ([value isKindOfClass:[NSString class]]) {
  22. return (NSString*)value;
  23. }
  24. if ([value isKindOfClass:[NSNumber class]]) {
  25. return [value stringValue];
  26. }
  27. return nil;
  28. }
  29. - (NSNumber*)numberForKey:(id)key
  30. {
  31. id value = [self objectForKey:key];
  32. if ([value isKindOfClass:[NSNumber class]]) {
  33. return (NSNumber*)value;
  34. }
  35. if ([value isKindOfClass:[NSString class]]) {
  36. NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
  37. [f setNumberStyle:NSNumberFormatterDecimalStyle];
  38. return [f numberFromString:(NSString*)value];
  39. }
  40. return nil;
  41. }
  42. - (NSArray*)arrayForKey:(id)key
  43. {
  44. id value = [self objectForKey:key];
  45. if (value == nil || value == [NSNull null])
  46. {
  47. return nil;
  48. }
  49. if ([value isKindOfClass:[NSArray class]])
  50. {
  51. return value;
  52. }
  53. return nil;
  54. }
  55. - (NSDictionary*)dictionaryForKey:(id)key
  56. {
  57. id value = [self objectForKey:key];
  58. if (value == nil || value == [NSNull null])
  59. {
  60. return nil;
  61. }
  62. if ([value isKindOfClass:[NSDictionary class]])
  63. {
  64. return value;
  65. }
  66. return nil;
  67. }
  68. - (NSInteger)integerForKey:(id)key
  69. {
  70. id value = [self objectForKey:key];
  71. if (value == nil || value == [NSNull null])
  72. {
  73. return 0;
  74. }
  75. if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]])
  76. {
  77. return [value integerValue];
  78. }
  79. return 0;
  80. }
  81. - (NSUInteger)unsignedIntegerForKey:(id)key{
  82. id value = [self objectForKey:key];
  83. if (value == nil || value == [NSNull null])
  84. {
  85. return 0;
  86. }
  87. if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]])
  88. {
  89. return [value unsignedIntegerValue];
  90. }
  91. return 0;
  92. }
  93. - (BOOL)boolForKey:(id)key
  94. {
  95. id value = [self objectForKey:key];
  96. if (value == nil || value == [NSNull null])
  97. {
  98. return NO;
  99. }
  100. if ([value isKindOfClass:[NSNumber class]])
  101. {
  102. return [value boolValue];
  103. }
  104. if ([value isKindOfClass:[NSString class]])
  105. {
  106. return [value boolValue];
  107. }
  108. return NO;
  109. }
  110. - (int16_t)int16ForKey:(id)key
  111. {
  112. id value = [self objectForKey:key];
  113. if (value == nil || value == [NSNull null])
  114. {
  115. return 0;
  116. }
  117. if ([value isKindOfClass:[NSNumber class]])
  118. {
  119. return [value shortValue];
  120. }
  121. if ([value isKindOfClass:[NSString class]])
  122. {
  123. return [value intValue];
  124. }
  125. return 0;
  126. }
  127. - (int32_t)int32ForKey:(id)key
  128. {
  129. id value = [self objectForKey:key];
  130. if (value == nil || value == [NSNull null])
  131. {
  132. return 0;
  133. }
  134. if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]])
  135. {
  136. return [value intValue];
  137. }
  138. return 0;
  139. }
  140. - (int64_t)int64ForKey:(id)key
  141. {
  142. id value = [self objectForKey:key];
  143. if (value == nil || value == [NSNull null])
  144. {
  145. return 0;
  146. }
  147. if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]])
  148. {
  149. return [value longLongValue];
  150. }
  151. return 0;
  152. }
  153. - (char)charForKey:(id)key{
  154. id value = [self objectForKey:key];
  155. if (value == nil || value == [NSNull null])
  156. {
  157. return 0;
  158. }
  159. if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]])
  160. {
  161. return [value charValue];
  162. }
  163. return 0;
  164. }
  165. - (short)shortForKey:(id)key
  166. {
  167. id value = [self objectForKey:key];
  168. if (value == nil || value == [NSNull null])
  169. {
  170. return 0;
  171. }
  172. if ([value isKindOfClass:[NSNumber class]])
  173. {
  174. return [value shortValue];
  175. }
  176. if ([value isKindOfClass:[NSString class]])
  177. {
  178. return [value intValue];
  179. }
  180. return 0;
  181. }
  182. - (float)floatForKey:(id)key
  183. {
  184. id value = [self objectForKey:key];
  185. if (value == nil || value == [NSNull null])
  186. {
  187. return 0;
  188. }
  189. if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]])
  190. {
  191. return [value floatValue];
  192. }
  193. return 0;
  194. }
  195. - (double)doubleForKey:(id)key
  196. {
  197. id value = [self objectForKey:key];
  198. if (value == nil || value == [NSNull null])
  199. {
  200. return 0;
  201. }
  202. if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]])
  203. {
  204. return [value doubleValue];
  205. }
  206. return 0;
  207. }
  208. - (long long)longLongForKey:(id)key
  209. {
  210. id value = [self objectForKey:key];
  211. if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]]) {
  212. return [value longLongValue];
  213. }
  214. return 0;
  215. }
  216. - (unsigned long long)unsignedLongLongForKey:(id)key
  217. {
  218. id value = [self objectForKey:key];
  219. if ([value isKindOfClass:[NSString class]]) {
  220. NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
  221. value = [nf numberFromString:value];
  222. }
  223. if ([value isKindOfClass:[NSNumber class]]) {
  224. return [value unsignedLongLongValue];
  225. }
  226. return 0;
  227. }
  228. //CG
  229. - (CGFloat)CGFloatForKey:(id)key
  230. {
  231. CGFloat f = [self[key] doubleValue];
  232. return f;
  233. }
  234. - (CGPoint)pointForKey:(id)key
  235. {
  236. CGPoint point = CGPointFromString(self[key]);
  237. return point;
  238. }
  239. - (CGSize)sizeForKey:(id)key
  240. {
  241. CGSize size = CGSizeFromString(self[key]);
  242. return size;
  243. }
  244. - (CGRect)rectForKey:(id)key
  245. {
  246. CGRect rect = CGRectFromString(self[key]);
  247. return rect;
  248. }
  249. @end
  250. #pragma --mark NSMutableDictionary setter
  251. @implementation NSMutableDictionary (SafeAccess)
  252. -(void)setObj:(id)i forKey:(NSString*)key{
  253. if (i!=nil) {
  254. self[key] = i;
  255. }
  256. }
  257. -(void)setString:(NSString*)i forKey:(NSString*)key;
  258. {
  259. [self setValue:i forKey:key];
  260. }
  261. -(void)setBool:(BOOL)i forKey:(NSString *)key
  262. {
  263. self[key] = @(i);
  264. }
  265. -(void)setInt:(int)i forKey:(NSString *)key
  266. {
  267. self[key] = @(i);
  268. }
  269. -(void)setInteger:(NSInteger)i forKey:(NSString *)key
  270. {
  271. self[key] = @(i);
  272. }
  273. -(void)setUnsignedInteger:(NSUInteger)i forKey:(NSString *)key
  274. {
  275. self[key] = @(i);
  276. }
  277. -(void)setCGFloat:(CGFloat)f forKey:(NSString *)key
  278. {
  279. self[key] = @(f);
  280. }
  281. -(void)setChar:(char)c forKey:(NSString *)key
  282. {
  283. self[key] = @(c);
  284. }
  285. -(void)setFloat:(float)i forKey:(NSString *)key
  286. {
  287. self[key] = @(i);
  288. }
  289. -(void)setDouble:(double)i forKey:(NSString*)key{
  290. self[key] = @(i);
  291. }
  292. -(void)setLongLong:(long long)i forKey:(NSString*)key{
  293. self[key] = @(i);
  294. }
  295. -(void)setPoint:(CGPoint)o forKey:(NSString *)key
  296. {
  297. self[key] = NSStringFromCGPoint(o);
  298. }
  299. -(void)setSize:(CGSize)o forKey:(NSString *)key
  300. {
  301. self[key] = NSStringFromCGSize(o);
  302. }
  303. -(void)setRect:(CGRect)o forKey:(NSString *)key
  304. {
  305. self[key] = NSStringFromCGRect(o);
  306. }
  307. @end