无法将解析对象添加到NSMutableArray

时间:2013-11-22 02:20:48

标签: ios objective-c parse-platform

当我运行以下代码时,我从Parse.com数据库中获取数据,但是当我尝试打印出我尝试存储此数据的数组时,它会发出一个空值。救命啊!

PFQuery *query = [PFQuery queryWithClassName:@"Resolution"];
    [query whereKey:@"event" equalTo:event_name_label.text];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            for (PFObject *object in objects) {
                [resolution_names addObject:(NSString*)[object objectForKey:@"resolution_name"]];
                NSLog(@"%@", [object objectForKey:@"resolution_name"]);
            }
            NSLog(@"%@", resolution_names);
        } else {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];

我已正确声明(我认为)NSMutableArray resolution_names,但在记录它时会返回null。

这是我的头文件:

#import <UIKit/UIKit.h>

@interface MenuViewController : UIViewController {
IBOutlet UILabel *event_name_label;

NSString *event_id;
NSString *event_name;
NSMutableArray *resolution_names;
NSMutableArray *resolution_pro_speakers;
NSMutableArray *resolution_con_speakers;
NSMutableArray *resolution_results;

NSMutableArray *billtrackertablevalues;
}

@property (nonatomic, retain) IBOutlet UILabel *event_name_label;

@property (nonatomic) NSString *event_id;
@property (nonatomic) NSString *event_name;

@property (nonatomic) NSMutableArray *resolution_names;
@property (nonatomic)  NSMutableArray *resolution_pro_speakers;
@property (nonatomic)  NSMutableArray *resolution_con_speakers;
@property (nonatomic)  NSMutableArray *resolution_results;

@property (nonatomic) NSMutableArray *billtrackertablevalues;

@end

再次,我能够在控制台中看到[object objectForKey:@“resolution_name”]的值,但是当我尝试输出NSMutableArray resolution_names的值时,它只是说“(null)”控制台。

2 个答案:

答案 0 :(得分:2)

NSMutableArray的一个常见错误就是忘记初始化它们。因为我没有看到你的数组的任何初始化并返回(null)我想这值得一试。

尝试将其添加到您的viewDidLoadinit方法。

self.resolution_names = [[NSMutableArray alloc] init];

答案 1 :(得分:0)

夫妻俩。您不需要声明iVar,只需要属性即可。接下来,您应该将数组指定为strong,告诉编译器保留该对象。因此,@property (nonatomic) NSMutableArray *resolution_names;变为@property (strong, nonatomic) NSMutableArray *resolution_names;

最后,您应该覆盖默认的getter,以便在您的应用需要时懒惰地实例化数组。

- (NSMutableArray *)resolution_names
{
  if(!_resolution_names){
    _resolution_names = [NSMutableArray array];
  }
  return _resolution_names;
}