rihekopo
rihekopo

Reputation: 3350

Empty array error - can't pass data from an existing NSMutableArray to a NSString

UPDATE

The problem was inside the findObjectsInBackgroundWithBlock: with the following line:

NSLog(@"Successfully retrieved %@ .", objects[0]);

the new version:

NSLog(@"Successfully retrieved %@ .", firstObject);

Passing the data to the mutable array was correct, i only changed the objectAtIndex to firstObject. But this block still not works properly.

Old question:

I have an NSMutableArray called searchResult, i would like to use it's first object for a PFQuery query, but the app is crashing when i'm trying to convert it to an NSString.

This is the error from the log:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

I know it's because an empty array, but don't understand why is the array empty. It should contain one username, because I have a search form where users can search for other users, if the search was successful i store the result in the searchResult mutable array. The result is listed in a table view cell, that contains the "Add Friend" button. If the user taps it, the other user should be added to it's friends. So the point is that i really really don't understand why can't i pass the existing data to the NSString?

  - (void)viewDidLoad
    {
    [super viewDidLoad];

    self.currentUser = [PFUser currentUser];
    searchResult = [[NSMutableArray alloc] initWithObjects:@"User", nil];   

    }

- (void)didTapButton:(id)sender {

    UIButton *button = (UIButton *)sender;
    CGPoint pointInSuperview = [button.superview convertPoint:button.center  toView:tableView];
    NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:pointInSuperview];

    NSString * contactRecipient = [searchResult objectAtIndex:0];
    PFQuery *query = [PFQuery queryWithClassName:@"User"];
    [query whereKey:@"username" equalTo:contactRecipient];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            // The find succeeded.
            NSLog(@"Successfully retrieved %@ .", objects[0]);
            // Do something with the found objects
            for (PFObject *object in objects) {
                NSLog(@"%@", object.objectId);
            }
        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];

.h file

@interface TestViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{

    IBOutlet UITableView *tableView;
    NSMutableArray *searchResult;
    NSString *searchRes;

}
@property (weak, nonatomic) IBOutlet UITextField *searchField;
@property (nonatomic, strong) PFUser *currentUser;



@end

Upvotes: 0

Views: 108

Answers (1)

CrimsonChris
CrimsonChris

Reputation: 4641

Sounds like you're getting an empty response (which may be valid). Use objects.firstObject instead and make the rest of your code nil safe.

Upvotes: 1

Related Questions