如何通过键和值获取对象

时间:2015-03-14 15:48:50

标签: ios objective-c uitableview cocoa-touch

我有一个名为masterMessages的对象,其中填充了名为messages的对象。每个消息对象都有五个键:

  • 的ObjectID
  • senderId
  • SENDERNAME
  • 消息体
  • 时间戳

基本上我现在正在做的是查询在这个名为masterMessages的对象中发送给我的用户的所有消息。然后我使用:

        self.senderIds = [masterMessages valueForKeyPath:@"@distinctUnionOfObjects.senderId"];

在名为senderIds的数组中获取所有不同的发送者ID(senderId)。有了这个,我将填充我的对话表。但我想用发件人名称(senderName)而不是senderIds填充它。我只是这样做,以防两个用户具有相同的名称。

我想找到:

  1. 我怎么说"得到valueForKey:@" senderName"对于此senderId
    1. 有没有更好的方法来填充我的会话表?
    2. 这是我的代码:

      注意:即时通讯使用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 = objects;
      
              NSLog(@"self.messages = %@", masterMessages);
      
              self.senderIds = [masterMessages valueForKeyPath:@"@distinctUnionOfObjects.senderId"];
      
              NSLog(@"self.senderIds = %@", self.senderIds);
      
      
              [self.tableView reloadData];
      
          }
        }];
      
      
      }
      
      #pragma mark - Table view data source
      
      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
      // Return the number of sections.
      return 1;
      }
      
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      // Return the number of rows in the section.
      return [self.senderIds count];
      }
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
      
      NSLog(@"self.senderIds = %@", self.senderIds);
      
      NSString *senderDisplayName = [self.senderIds objectAtIndex:indexPath.row];
      
      NSLog(@"sender = %@", senderDisplayName);
      
      
      cell.textLabel.text = senderDisplayName;
      
      
      return cell;
      }
      
      
      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
      {
      selectedId = [self.senderIds objectAtIndex:indexPath.row];
      
      
      [self performSegueWithIdentifier:@"ShowMissionMessage" sender:self];
      
      
      
      }
      
      - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
      if ([segue.identifier isEqualToString:@"ShowMissionMessage"]) {
      
          [segue.destinationViewController setHidesBottomBarWhenPushed:YES];
          MissionChat *missionchatviewcontroller = (MissionChat *)segue.destinationViewController;
          missionchatviewcontroller.selectedId = selectedId;
          missionchatviewcontroller.masterMessages = masterMessages;
         }
      }
      

2 个答案:

答案 0 :(得分:0)

    function findObjectByKey(array, key, value) {
        for (var i = 0; i < array.length; i++) {
            if (array[i][key] === value) {
                return array[i];
            }
        }
        return null;
    }

var obj = findObjectByKey(objArray, 'id', 3);

//ES6

var obj = objArray.find(function (obj) { return obj.id === 3; });

答案 1 :(得分:-1)

function getObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjects(obj[i], key, val));
        } else if (i == key && obj[key] == val) {
            objects.push(obj);
        }
    }
    return objects;
}
var result = getObjects(obj, 'category_id', valu.category_id);
console.log(result);