简体   繁体   中英

Can't Add Parse Object to NSMutableArray

When I run the following code, I get the data from the Parse.com database, but when I try to print out the array that I try to store this data in, it gives off a null value. Help!

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]);
        }
    }];

I have properly declared (I think) the NSMutableArray resolution_names, but it returns null upon logging it.

Here is my header file:

#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

Once again, I am able to see the values of [object objectForKey:@"resolution_name"] in the console, but when I try to output the values of the NSMutableArray resolution_names, it just says "(null)" on the console.

A very common mistake with NSMutableArray s is to forget to initialise them. Since I do not see any initialisation of your array and it returns (null) I guess it's worth a try.

Try to add this to your viewDidLoad or init method.

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

Couple things. You do not need to declare the iVar, just the property will do. Next, you should specify your array as strong which tells the compiler to retain the object. So, @property (nonatomic) NSMutableArray *resolution_names; becomes @property (strong, nonatomic) NSMutableArray *resolution_names; .

Lastly, you should override the default getter to lazily instantiate the array when your app needs it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM