How to restructure object of NSDictionaries

后端 未结 2 727
广开言路
广开言路 2020-12-22 13:34

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

self.messages = (
\"

        
相关标签:
2条回答
  • 2020-12-22 13:41

    They are PFObjects (from the parse.com framework), and PFObject implements description in this particular way. PFObjects implement a dictionary-like interface that is fully described here.

    With due respect to others, it is overly general to type them as id, and to use KVO to access their properties. Moreover, messageBody appears to be a column of type string. Treating as a dictionary will be unproductive.

    PFObjects should be treated this way:

    for (PFObject *pfObject in objects) {
        NSString *objectId = [pfObject objectId];
        NSDate *createdAt = [pfObject createdAt];
        NSString *messageBody = [pfObject objectForKey:@"messageBody"];
        NSString *senderId = [pfObject objectForKey:@"senderId"];
        // and so on
        NSLog(@"This object has id %@ created at %@, senderId %@", objectId, createdAt, senderId);
    }
    

    I'm aware of your other recent SO question regarding how to derive and array of senderIds from this array. Extracting senderId ought to be obvious from this code, but (a) you probably don't need a new array based on your UI need (a table of conversations), and (b) I think you're actually looking for senderName, and that might mean a substantial change to how you've implemented relationships between parse objects.

    I'll try to answer further over on that other question, but this one is clearly related.

    0 讨论(0)
  • 2020-12-22 13:48

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

    UPDATE:

    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

    0 讨论(0)
提交回复
热议问题