Search code examples
iosobjective-cnsarrayamazon-dynamodbobject-object-mapping

Objective-C class object not mapping to array


How can I pass the event to the toDoArray? I'm not sure what I'm missing. Any help appreciated.

SDEventModel.h

@interface SDEventModel : AWSDynamoDBObjectModel <AWSDynamoDBModeling>

ViewController.h

@property (nonatomic, strong) NSArray *toDoArray;

ViewController.m

    if (task.result) {
     AWSDynamoDBPaginatedOutput *paginatedOutput = task.result;
     for (SDEventModel *event in paginatedOutput.items) {
         //Do something with event.
         NSLog(@"Task results: %@", event);

         [self.toDoArray arrayByAddingObject:event];
         NSLog(@"To do array results: %@", self.toDoArray);

         [self.tableview reloadData];
     }
 }

Here is the output of the NSLog.

Task results: <SDEventModel: 0x7faa88d81430> {
city = "New York";
image = "photo-22.jpg";
title = "Hang with friends";
}
To do array results: (null)

Solution

  • The arrayByAddingObject method returns another array with the added object, and does not append the same.

    This is how the method is intended to be used:

    self.toDoArray = [self.toDoArray arrayByAddingObject:event];
    

    However, in your case, it seems that the array is not even initialized. So you need to do something like this as well:

    -(void) viewDidLoad {
        [super viewDidLoad];
        self.toDoArray = @[];
    }