2016-02-05 59 views
-1

的阵列从以下阵列我想取得与MESSAGE_ID字典= 1基本上我想获得其MESSAGE_ID == 1NSPredicate抓取字典从阵列

<__NSArrayM 0x17d918d0>(
<__NSArrayM 0x17faa070>(
{ 
    chatStatus = sent; 
    date = "Feb 05, 2016"; 
    from = ""; 
    height = "17.000000"; 
    isOutgoing = yes; 
    "message_id" = "1"; 
    time = "02:39 PM"; 
    to = ""; 
    width = "95.000000"; 
}, 
{ 
    chatStatus = sent; 
    date = "Feb 05, 2016"; 
    from = ""; 
    height = "17.000000"; 
    isOutgoing = yes; 
    "message_id" = "2"; 
    time = "02:39 PM"; 
    to = ""; 
    width = "95.000000"; 
}, 
) 

) 

回答

0

我认为该单元的indexpath你应该看看周围的NSArray的方法

-[NSArray indexesOfObjectsPassingTest:^BOOL(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)] 

做这样的事情:

 NSMutableArray *indexPaths = [NSMutableArray array]; 

    [myArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger firstIndex, BOOL * _Nonnull stop) { 

     NSArray *messages = (NSArray*)obj; 

     NSIndexSet *indexes = [messages indexesOfObjectsPassingTest:^BOOL(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { 

      NSDictionary *message = (NSDictionary*)obj; 

      if (message.message_id == 1) { 
       return YES; 
      } 
      return NO; 

     }]; 

     [indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) { 

      [indexPaths addObject:[NSIndexPath indexPathForRow:idx inSection:firstIndex]]; 

     }]; 

    })]; 

这似乎有点棘手,我很快就做到了,所以我认为你可以改进它。这个想法是枚举你的第一个数组,然后使用“indexesOfObjectsPassingTest”来获取匹配对象的所有索引。然后你和你的indexPaths通过遍历NSIndexSet对象的枚举:)

我想你甚至可以递归地做它,这样你就不会受到数组深度的限制,这里只有2:/

希望它有帮助\\ Bilkix。