NSArray+Category.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. //
  2. // NSArray+Category.m
  3. // 乐销
  4. //
  5. // Created by 隋林栋 on 2017/1/21.
  6. // Copyright © 2017年 ping. All rights reserved.
  7. //
  8. #import "NSArray+Category.h"
  9. @implementation NSArray (Category)
  10. - (CGFloat)height{
  11. if (self.count > 0) {
  12. CGFloat numReturn = 0;
  13. for (UIView * view in self) {
  14. if ([view isKindOfClass:[UIView class]]) {
  15. numReturn += view.topToUpView + view.height;
  16. }
  17. }
  18. return numReturn;
  19. }
  20. return 0;
  21. }
  22. //转换已选择model
  23. //- (void)fetchSelectModels:(NSArray *)arySelect compareKey:(NSString *)compareKey exchangeKey:(NSString *)exchangeKey{
  24. // if (!arySelect) {
  25. // return;
  26. // }
  27. // NSMutableDictionary * dic = [NSMutableDictionary dictionary];
  28. // for (id model in arySelect) {
  29. // if ([model respondsToSelector:NSSelectorFromString(compareKey)] && [model valueForKeyPath:compareKey]) {
  30. // [dic setValue:[NSNumber numberWithBool:true] forKey:[model valueForKeyPath:compareKey]];
  31. // }
  32. // }
  33. // for (id model in self) {
  34. // if ([model isKindOfClass:[ModelAryIndex class]]) {
  35. // ModelAryIndex * modelSection = (ModelAryIndex *)model;
  36. // for (id model in modelSection.aryMu) {
  37. // if ([model respondsToSelector:NSSelectorFromString(compareKey)]&&[model valueForKeyPath:compareKey]) {
  38. // id isEqual =[dic objectForKey:[model valueForKeyPath:compareKey]];
  39. // if ([model respondsToSelector:NSSelectorFromString(exchangeKey)]) {
  40. // [model setValue:[NSNumber numberWithBool:isEqual?true:false] forKeyPath:exchangeKey];
  41. // }
  42. // }
  43. // }
  44. // }else{
  45. // if ([model respondsToSelector:NSSelectorFromString(compareKey)]&&[model valueForKeyPath:compareKey]) {
  46. // NSNumber * num = [model valueForKeyPath:compareKey];
  47. // id isEqual =[dic objectForKey:[model valueForKeyPath:compareKey]];
  48. // if ([model respondsToSelector:NSSelectorFromString(exchangeKey)]) {
  49. // [model setValue:[NSNumber numberWithBool:isEqual?true:false] forKeyPath:exchangeKey];
  50. // }
  51. // }
  52. // }
  53. // }
  54. //}
  55. //获取选中的数据
  56. - (NSMutableArray *)fetchSelectModelsCompareModel:(id)model keyPath:(NSString *)keyPath{
  57. NSMutableArray * aryReturn = [NSMutableArray array];
  58. NSMutableDictionary * dic = [NSMutableDictionary dictionary];
  59. if ([model respondsToSelector:NSSelectorFromString(keyPath)]&&[model valueForKeyPath:keyPath]) {
  60. [dic setValue:[NSNumber numberWithBool:true] forKey:[model valueForKeyPath:keyPath]];
  61. }
  62. for (id model in self) {
  63. if ([model respondsToSelector:NSSelectorFromString(keyPath)]&&[model valueForKeyPath:keyPath]) {
  64. id isEqual =[dic objectForKey:[model valueForKeyPath:keyPath]];
  65. if (isEqual) {
  66. [aryReturn addObject:model];
  67. }
  68. }
  69. }
  70. return aryReturn;
  71. }
  72. //获取选中的数据
  73. - (NSMutableArray *)fetchSelectModelsKeyPath:(NSString *)keyPath value:(id)value{
  74. NSMutableArray * aryReturn = [NSMutableArray array];
  75. NSMutableDictionary * dic = [NSMutableDictionary dictionary];
  76. if (!value) {
  77. return aryReturn;
  78. }
  79. [dic setValue:[NSNumber numberWithBool:true] forKey:value];
  80. for (id model in self) {
  81. if ([model respondsToSelector:NSSelectorFromString(keyPath)]&&[model valueForKeyPath:keyPath]) {
  82. id isEqual =[dic objectForKey:[model valueForKeyPath:keyPath]];
  83. if (isEqual) {
  84. [aryReturn addObject:model];
  85. }
  86. }
  87. }
  88. return aryReturn;
  89. }
  90. //组成字符串
  91. - (NSString *)componentsJoinedByString:(NSString *)separator keyPath:(NSString *)keyPath{
  92. NSMutableArray * aryReturn = [NSMutableArray array];
  93. for (id model in self) {
  94. if ([model respondsToSelector:NSSelectorFromString(keyPath)]&&[model valueForKeyPath:keyPath]) {
  95. NSString * str = [NSString stringWithFormat:@"%@",UnPackStr([model valueForKeyPath:keyPath])];
  96. if (isStr(str)) {
  97. [aryReturn addObject:str];
  98. }
  99. }
  100. }
  101. return [aryReturn componentsJoinedByString:separator];
  102. }
  103. //组成固定key Json字符串
  104. - (NSString *)fetchJsonkeyPath:(NSString *)keyPath{
  105. NSMutableArray * aryReturn = [NSMutableArray array];
  106. for (id model in self) {
  107. if ([model respondsToSelector:NSSelectorFromString(keyPath)]&&[model valueForKeyPath:keyPath]) {
  108. [aryReturn addObject:@{keyPath:[model valueForKeyPath:keyPath]}];
  109. }
  110. }
  111. return [GlobalMethod exchangeDicToJson:aryReturn];
  112. }
  113. //获取keypath 组成的数组
  114. - (NSMutableArray *)fetchValues:(NSString *)keyPath{
  115. NSMutableArray * aryReturn = [NSMutableArray array];
  116. for (id model in self) {
  117. if ([model respondsToSelector:NSSelectorFromString(keyPath)]&&[model valueForKeyPath:keyPath]) {
  118. [aryReturn addObject:[model valueForKeyPath:keyPath]];
  119. }
  120. }
  121. return aryReturn;
  122. }
  123. //获取元素ary
  124. - (NSMutableArray *)fetchValuesComponentAry:(NSString *)keyPath{
  125. NSMutableArray * aryMu = [NSMutableArray array];
  126. for (id modelItem in self) {
  127. {
  128. if ([modelItem respondsToSelector:NSSelectorFromString(keyPath)]) {
  129. NSArray * value = [modelItem valueForKeyPath:keyPath];
  130. if (isAry(value)) {
  131. [aryMu addObjectsFromArray:value];
  132. }
  133. }
  134. }
  135. }
  136. return aryMu;
  137. }
  138. //组成dic
  139. - (NSMutableDictionary *)exchangeDicWithKeyPath:(NSString *)keyPath{
  140. if (!isStr(keyPath)) {
  141. return [NSMutableDictionary dictionary];
  142. }
  143. NSMutableDictionary * dic = [NSMutableDictionary dictionary];
  144. for (id model in self) {
  145. if([model respondsToSelector:NSSelectorFromString(keyPath)]&&[model valueForKeyPath:keyPath]) {
  146. [dic setObject:model forKey:[model valueForKeyPath:keyPath]];
  147. }
  148. }
  149. return dic;
  150. }
  151. //组成dic
  152. - (NSMutableDictionary *)exchangeStrAryToDic{
  153. NSMutableDictionary * dic = [NSMutableDictionary dictionary];
  154. for (NSString * model in self) {
  155. if (isStr(model)) {
  156. [dic setObject:model forKey:model];
  157. }
  158. }
  159. return dic;
  160. }
  161. //获取相同的model
  162. - (id)fetchSameModelKeyPath:(NSString *)keyPath model:(id)model{
  163. // if ([model respondsToSelector:NSSelectorFromString(keyPath)] && [model valueForKeyPath:keyPath]) {
  164. // //集成元素
  165. // NSMutableDictionary * dicMu = [NSMutableDictionary dictionary];
  166. // for (id modelItem in self) {
  167. // if ([modelItem isKindOfClass:[ModelAryIndex class]]) {
  168. // ModelAryIndex * modelSection = (ModelAryIndex *)modelItem;
  169. // for (id modelItem in modelSection.aryMu) {
  170. // if ([modelItem respondsToSelector:NSSelectorFromString(keyPath)]&&[modelItem valueForKeyPath:keyPath]) {
  171. // [dicMu setObject:modelItem forKey:[modelItem valueForKeyPath:keyPath]];
  172. // }
  173. // }
  174. // }else{
  175. // if ([modelItem respondsToSelector:NSSelectorFromString(keyPath)]&&[modelItem valueForKeyPath:keyPath]) {
  176. // [dicMu setObject:modelItem forKey:[modelItem valueForKeyPath:keyPath]];
  177. // }
  178. // }
  179. // }
  180. // //获取指定元素
  181. // return [dicMu objectForKey:[model valueForKeyPath:keyPath]];
  182. // }
  183. return nil;
  184. }
  185. //获取相同的model
  186. - (id)fetchSameModelKeyPath:(NSString *)keyPath value:(id)value{
  187. if (!value) {
  188. return nil;
  189. }
  190. //集成元素
  191. NSMutableDictionary * dicMu = [NSMutableDictionary dictionary];
  192. for (id modelItem in self) {
  193. if ([modelItem respondsToSelector:NSSelectorFromString(keyPath)]&&[modelItem valueForKeyPath:keyPath]) {
  194. [dicMu setObject:modelItem forKey:[modelItem valueForKeyPath:keyPath]];
  195. }
  196. }
  197. //获取指定元素
  198. return [dicMu objectForKey:value];
  199. }
  200. //获取不同的model
  201. - (NSMutableArray *)fetchDifferentElementKeyPath:(NSString *)keyPath{
  202. if (!isStr(keyPath)) {
  203. return nil;
  204. }
  205. //集成元素
  206. NSMutableDictionary * dicMu = [NSMutableDictionary dictionary];
  207. NSMutableArray *aryReturn = [NSMutableArray array];
  208. for (id modelItem in self) {
  209. if ([modelItem respondsToSelector:NSSelectorFromString(keyPath)]&&[modelItem valueForKeyPath:keyPath]) {
  210. if (![dicMu objectForKey:[modelItem valueForKeyPath:keyPath]]) {
  211. [dicMu setObject:modelItem forKey:[modelItem valueForKeyPath:keyPath]];
  212. [aryReturn addObject:modelItem];
  213. }
  214. }
  215. }
  216. //获取指定元素
  217. return aryReturn;
  218. }
  219. //获取相同的model
  220. - (NSMutableArray *)fetchSameElementKeyPath:(NSString *)keyPath aryCompare:(NSArray *)aryCompare{
  221. if (!isStr(keyPath)) {
  222. return nil;
  223. }
  224. if (!isAry(aryCompare)) {
  225. return nil;
  226. }
  227. //集成元素
  228. NSMutableDictionary * dicMu = [NSMutableDictionary dictionary];
  229. for (id modelItem in aryCompare) {
  230. if ([modelItem respondsToSelector:NSSelectorFromString(keyPath)]&&[modelItem valueForKeyPath:keyPath]) {
  231. [dicMu setObject:modelItem forKey:[modelItem valueForKeyPath:keyPath]];
  232. }
  233. }
  234. NSMutableArray *aryReturn = [NSMutableArray array];
  235. for (id modelItem in self) {
  236. if ([modelItem respondsToSelector:NSSelectorFromString(keyPath)]&&[modelItem valueForKeyPath:keyPath]) {
  237. if ([dicMu objectForKey:[modelItem valueForKeyPath:keyPath]]) {
  238. [aryReturn addObject:modelItem];
  239. }
  240. }
  241. }
  242. //获取指定元素
  243. return aryReturn;
  244. }
  245. //转换ary to section
  246. //- (NSMutableArray *)exchangeToSectionWithKeyPath:(NSString *)keyPath {
  247. // //init ary all
  248. // NSMutableDictionary * dicAllKey = [NSMutableDictionary dictionary];
  249. // NSMutableArray * aryReturn = [NSMutableArray array];
  250. // for (id model in self) {
  251. // if ([model respondsToSelector:NSSelectorFromString(keyPath)]) {
  252. // NSString * strKey = [model valueForKeyPath:keyPath];
  253. // if (![dicAllKey objectForKey:UnPackStr(strKey)]) {
  254. // ModelAryIndex * modelSection = [ModelAryIndex initWithStrFirst:UnPackStr(strKey)];
  255. // [dicAllKey setObject:modelSection forKey:UnPackStr(strKey)];
  256. // [aryReturn addObject:modelSection];
  257. // [modelSection.aryMu addObject:model];
  258. // }else{
  259. // ModelAryIndex * modelSection = [dicAllKey objectForKey:UnPackStr(strKey)];
  260. // [modelSection.aryMu addObject:model];
  261. // }
  262. // }
  263. // }
  264. // return aryReturn;
  265. //}
  266. - (NSInteger)fetchSameModelIndexKeyPath:(NSString *)keyPath model:(id)model{
  267. id modelIndex = [self fetchSameModelKeyPath:keyPath model:model];
  268. if (modelIndex) {
  269. return [self indexOfObject:modelIndex];
  270. }
  271. return -1;
  272. }
  273. - (NSInteger)fetchSameModelIndexKeyPath:(NSString *)keyPath value:(id)value{
  274. id modelIndex = [self fetchSameModelKeyPath:keyPath value:value];
  275. if (modelIndex) {
  276. return [self indexOfObject:modelIndex];
  277. }
  278. return -1;
  279. }
  280. //获取临时固定ary
  281. - (NSArray *)tmpAry{
  282. return [NSArray arrayWithArray:self];
  283. }
  284. - (NSMutableArray *)tmpMuAry{
  285. return [NSMutableArray arrayWithArray:self];
  286. }
  287. - (NSMutableArray *)copyModelMuAry{
  288. NSMutableArray * aryMu = [NSMutableArray array];
  289. for (id modelItem in self) {
  290. if ([modelItem respondsToSelector:NSSelectorFromString(@"dictionaryRepresentation")]) {
  291. NSDictionary * dic = [GlobalMethod performSelector:@"dictionaryRepresentation" delegate:modelItem object:nil isHasReturn:true];
  292. if (dic) {
  293. id modelCopy = [GlobalMethod performSelector:@"modelObjectWithDictionary:" delegate:[modelItem class] object:dic isHasReturn:true];
  294. [aryMu addObject:modelCopy];
  295. }
  296. }else {
  297. [aryMu addObject:modelItem];
  298. }
  299. }
  300. return aryMu;
  301. }
  302. //将model 转换成字典
  303. - (NSMutableArray *)exchangeModelToDicAry{
  304. NSMutableArray * aryMu = [NSMutableArray array];
  305. for (id modelItem in self) {
  306. if ([modelItem respondsToSelector:NSSelectorFromString(@"dictionaryRepresentation")]) {
  307. NSDictionary * dic = [GlobalMethod performSelector:@"dictionaryRepresentation" delegate:modelItem object:nil isHasReturn:true];
  308. if (dic) {
  309. [aryMu addObject:dic];
  310. }
  311. }else if ([modelItem isKindOfClass:NSDictionary.class]){
  312. [aryMu addObject:modelItem];
  313. }
  314. }
  315. return aryMu;
  316. }
  317. //- (void)writeToLocal:(NSString *)localKey{
  318. // NSArray * aryTmp = self.tmpAry;
  319. // [GlobalMethod asynthicBlock:^{
  320. // [GlobalMethod writeAry:aryTmp key:localKey];
  321. // }];
  322. //}
  323. //将string 转换成model
  324. //- (NSMutableArray *)exhcnageStrAryToModelAry{
  325. // NSMutableArray * aryReturn = [NSMutableArray new];
  326. // for (NSString * str in self) {
  327. // if (isStr(str)) {
  328. // ModelBaseData * model = [ModelBaseData new];
  329. // model.string = str;
  330. // [aryReturn addObject:model];
  331. // }else if(str && [str isKindOfClass:[ModelBaseData class]]){
  332. // [aryReturn addObject:str];
  333. // }
  334. // }
  335. // return aryReturn;
  336. //}
  337. //获取model 同一类
  338. - (NSMutableArray * )fetchModelsClassName:(NSString *)className{
  339. if (!isStr(className))return [NSMutableArray array];
  340. NSMutableArray * aryReturn = [NSMutableArray array];
  341. for (id model in self.tmpAry) {
  342. if (model && [model isKindOfClass:NSClassFromString(className)]) {
  343. [aryReturn addObject:model];
  344. }
  345. }
  346. return aryReturn;
  347. }
  348. #pragma mark logical method
  349. //fetch image request string
  350. - (NSString *)imageRequestStr{
  351. return [GlobalMethod exchangeDicToJson:self.imageAryOfDic];
  352. }
  353. //- (NSMutableArray *)imageAryOfDic{
  354. // NSMutableArray * aryReturn = [NSMutableArray array];
  355. // for (ModelImage * modelItem in self) {
  356. // if ([modelItem isKindOfClass:[ModelImage class]]){
  357. // if (modelItem.image) {
  358. // CGSize size = modelItem.image.size;
  359. // [aryReturn addObject:@{@"url":UnPackStr(modelItem.image.imageURL),@"desc":UnPackStr(modelItem.desc),@"type":@"0",@"width":strDotF(size.width),@"height":strDotF(size.height)}];
  360. // }else{
  361. // [aryReturn addObject:@{@"url":UnPackStr(modelItem.url),@"desc":UnPackStr(modelItem.desc),@"type":@"0",@"width":strDotF(modelItem.width),@"height":strDotF(modelItem.width)}];
  362. // }
  363. // }
  364. // }
  365. // return aryReturn;
  366. //}
  367. #pragma mark exchange to model
  368. - (NSMutableArray *)exchangeToModel:(NSString *)modelName{
  369. NSMutableArray * aryReturn = [NSMutableArray new];
  370. for (id modelItem in self) {
  371. if (NSClassFromString(modelName)) {
  372. if ([modelItem respondsToSelector:NSSelectorFromString(@"exchangeToModel:")]) { id modelNew = [GlobalMethod performSelector:@"exchangeToModel:" delegate:modelItem object:modelName isHasReturn:true];
  373. if (modelNew) {
  374. [aryReturn addObject:modelNew];
  375. }
  376. }
  377. }
  378. }
  379. return aryReturn;
  380. }
  381. /**
  382. recofig views in the direction vertical
  383. */
  384. - (void)reconfigVerticalViews{
  385. if (self.count > 0) {
  386. CGFloat numTop = 0;
  387. for (UIView * view in self) {
  388. if ([view isKindOfClass:[UIView class]]) {
  389. view.top = numTop + view.topToUpView;
  390. numTop += view.topToUpView + view.height;
  391. }
  392. }
  393. }
  394. }
  395. //- (NSString *)jsonStr{
  396. // NSMutableArray *array = [NSMutableArray array];
  397. // for (ModelCell_AddressBook * model in self) {
  398. // if (model && [model isKindOfClass:[ModelCell_AddressBook class]]) {
  399. // switch (model.type) {
  400. // case ENUM_SELECT_STAFF:
  401. // {
  402. // if(model.model && [model.model isKindOfClass:[ModelStaffEmNum class]]){
  403. // ModelStaffEmNum * modelStaff = model.model;
  404. // [array addObject:@{@"coname":UnPackStr(modelStaff.empName),@"conphone":UnPackStr(modelStaff.phone),@"conposition":UnPackStr(modelStaff.positionName)}];
  405. // }
  406. // }
  407. // break;
  408. // default:
  409. // break;
  410. // }
  411. //
  412. // } else if (model && [model isKindOfClass:[ModelProduct class]]) {
  413. // [array addObject:[(ModelProduct *)model jsonObjFromProduct]];
  414. // }
  415. // }
  416. // return [GlobalMethod exchangeDicToJson:array];
  417. //}
  418. @end