如何重构NSDictionaries的对象 [英] How to restructure object of NSDictionaries

查看:79
本文介绍了如何重构NSDictionaries的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的调试器中,这是我的日志语句打印我的消息对象的方式:

In my debugger this is how my log statement is printing my messages object:

self.messages = (
"<lean: 0x7fcf98665140, objectId: vglE1UJ5KI, localId: (null)> {\n    messageBody = Jj;\n    recipientId = XvvxETqjph;\n    senderId = XvvxETqjph;\n    timestamp = \"1424991590106.210938\";\n}",
"<lean: 0x7fcf98667940, objectId: rgBFYBMKlU, localId: (null)> {\n    messageBody = \"test 3 from ian\";\n    recipientId = XvvxETqjph;\n    senderId = Hoy7UjLzOh;\n    timestamp = \"1424631667110.638184\";\n}",
"<lean: 0x7fcf98667f30, objectId: hB5uhwsYsu, localId: (null)> {\n    messageBody = \"test 2 from user1\";\n    recipientId = XvvxETqjph;\n    senderId = VQzxWbDnal;\n    timestamp = \"1424630904935.162109\";\n}",
"<lean: 0x7fcf986685b0, objectId: dOe2B9oq5b, localId: (null)> {\n    messageBody = \"test 1\";\n    recipientId = XvvxETqjph;\n    senderId = XvvxETqjph;\n    timestamp = \"1424630808309.478027\";\n}"
)




  1. 究竟是什么?它看起来像是字典的对象,但是objectId和localId变量在花括号之外,所以我不确定。

  1. what exactly is this? It looks like an object of dictionaries but the objectId and localId variables are outside of the curly braces so im not sure.

如何重构这个以便我可以自己拥有每个单独的字典吗?

How do I restructure this so that I can have each individual dictionary by itself?

我从parse.com上的后端获取这些数据:

Im getting this data from my backend on parse.com:

-(void)viewWillAppear:(BOOL)animated{

NSString *userId = [[PFUser currentUser] objectId];

PFQuery *query = [PFQuery queryWithClassName:@"lean"];
[query whereKey:@"recipientId" equalTo:userId];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error) {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
    else {
        // We found messages!
        masterMessages = (NSDictionary *)objects;

        NSLog(@"self.messages = %@", masterMessages);



        [self.tableView reloadData];

    }
}];


}

推荐答案

这是一个简单的 NSArray ,其中包含名为 lean 类型的对象。你可以通过索引获得所需的对象

this is a simple NSArray which contains objects of type named lean. you can get the needed object by its index

更新:

我当然不能确定。我不知道精简的结构。但很可能它包含名为 messageBody 的字段(我认为它必须是属性)。如果确实如此,那么您可以执行以下操作:

I can't be sure of course. I have no idea about lean's structure. But most likely it contains field (and I assume it must be a property) named messageBody. If it is really so then you can do the following:

NSMutableArray* messagesBySenderName = [NSMutableArray new];

for (id lean in objects)
{
  NSDictionary *messageBody = [lean valueForKeyPath:@"messageBody"];

  if ([lean[@"senderId"] isEqualToString:@"Hoy7UjLzOh"])
  {
    [messagesBySenderName addObject:messageBody];
  } 
}

再次 - 这只是一个假设。在任何情况下,我都不能保证它会起作用

Again - it is only an assumption. In no case I can guarantee it will work

这篇关于如何重构NSDictionaries的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆