0

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.

2 Answers 2

2

A very common mistake with NSMutableArrays 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];
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Properties are strong by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.