I am trying to print out a list of objects in my NSMutableArray via NSLog, but for some reason, it is appearing to be null. Basically, I have a to-do list that when the user enters a new string to add to the tableview, it will also add that item to the NSArray so I can save it to the device.
AddToDoItemViewController.m
#import "AddToDoItemViewController.h"
#import "ToDoItem.h"
@interface AddToDoItemViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton;
@end
@implementation AddToDoItemViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.toDoItem.itemList = [[NSMutableArray alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if (sender != self.saveButton) return;
if (self.textField.text.length > 0) {
self.toDoItem = [[ToDoItem alloc] init];
self.toDoItem.itemName = self.textField.text;
self.toDoItem.completed = NO;
NSLog(@"Trying to add to array: %@", self.toDoItem.itemName);
[self.toDoItem.itemList addObject:self.toDoItem.itemName];
NSLog(@"Array contents: %@", self.toDoItem.itemList);
}
}
@end
AddToDoItemViewController.h
#import <UIKit/UIKit.h>
#import "ToDoItem.h"
@interface AddToDoItemViewController : UIViewController
@property ToDoItem *toDoItem;
@end
ToDoItem.h
#import <Foundation/Foundation.h>
@interface ToDoItem : NSObject
@property NSString *itemName;
@property BOOL completed;
@property (readonly) NSDate *creationDate;
@property NSMutableArray *itemList;
@end
Now from my AddToDoItem.m file, when I use NSLog to try to output the Array I get this:
2016-02-24 01:04:49.668 ToDoList[4025:249117] Trying to add to array: ok
2016-02-24 01:04:49.669 ToDoList[4025:249117] Array contents: (null)
**** The 'ok' was the text I entered *****