0

我在我的新应用程序中使用解析框架并尝试从其中一个表中获取数据。Parse 表包含 20 列,其中近 15 列是字符串类型。下面是获取代码

PFQuery *query=[PFQuery queryWithClassName:@"Product"];
[query setLimit:20];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {

    for (PFObject *obj in objects) {
        NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
        [dict setValue:obj[@"GTIN_CD"] forKey:@"GTIN_CD"];
        [dict setValue:obj[@"GTIN_NM"] forKey:@"GTIN_NM"];

        [self.arrProducts addObject:dict];  
    }

    [self.tblSearchResult setDelegate:self];
    [self.tblSearchResult setDataSource:self];
    [self.tblSearchResult reloadData];
}
else{
    NSLog(@"Error : %@",error);
}
}];

当我试图获取这两个对象 GTIN_CD、GTIN_NM 时,它不会返回任何值。对象也仅包含 5 列的值。

任何帮助将不胜感激。提前致谢

4

1 回答 1

0

You aren't doing anything with the object you fetched. You're both getting and setting value from the dict. Try this:

PFQuery *query=[PFQuery queryWithClassName:@"Product"];
[query setLimit:20];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        NSMutableDictionary *dict=[[NSMutableDictionary alloc]init]; // Initialize outside of loop
        for (PFObject *obj in objects) {
            [dict setValue:obj[@"GTIN_CD"] forKey:@"GTIN_CD"]; // obj[@"key"] instead of dict[@"key"]
            [dict setValue:obj[@"GTIN_NM"] forKey:@"GTIN_NM"];
        }

        [self.tblSearchResult setDelegate:self];
        [self.tblSearchResult setDataSource:self];
        [self.tblSearchResult reloadData];

   }
    else{
        NSLog(@"Error : %@",error);
    }
}];

UPDATE

Try this inside your if(!error):

for (PFObject *obj in objects) {
    for (NSString *key in [obj allKeys]) {
        if([obj[key] isKindOfClass:[NSString class]]) {
            NSLog(@"Key: %@ - Value: %@", key, obj[key]);
        }
    }
}

This should log all your columns and the values in them. Note that I haven't tested this code myself right now, so it may have typos etc.

于 2014-04-30T07:14:21.577 回答