0

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 *****

1
  • can you show your ToDoItem.m Commented Feb 24, 2016 at 6:30

3 Answers 3

3

You did not initialise the array before adding to it, add self.toDoItem.itemList = [NSMutableArray new];

Edit:

oh i see you added self.toDoItem.itemList = [[NSMutableArray alloc] init]; in the viewDidLoad, but this is not the right place to put it, it should be after self.toDoItem = [[ToDoItem alloc] init]; or inside the init method of ToDoItem

Sign up to request clarification or add additional context in comments.

7 Comments

fonix I have one doubt , if he initilize in under with prepare for segue how will u get the array value , the last value only get na bro
Thanks for the quick response. I tried adding it right after self.toDoItem = [[ToDoItem alloc] init];, but what that ends up doing is re-initializing the array and therefore clearing all the items in it. I figured if I initalized it in the onDidLoad, it would persist
well, you are going to have to change something quite significant to your logic then, since you are reassigning self.toDoItem, so at that point you have lost any previous ones anyway. maybe you should take that out? then you will have your previously inited array from the viewDidLoad
Shouldn't it be adding the item to the NSMutableArray that is directly in the class object though?
it should, but the fact that you have self.toDoItem = [[ToDoItem alloc] init]; right before it will make your array get reset to null, because you are assigning a completely new object to self.toDoItem.
|
0

First, you need create an init method at ToDoItem.m

- (id)init {
    self = [super init];
    if (self) {
        // Any custom setup work goes here
        self.itemList =  [[NSMutableArray alloc] init];
    }
    return self;
}

then run your project again.

2 Comments

For some reason doing this still returns a null array
Give me one case return null.
0

You are initialising the Array( itemList ) before ToDoItem is initialised, so initialised array remains nil. So, it cannot store any object.

Modify code as below,

self.toDoItem = [[ToDoItem alloc] init];
self.toDoItem.itemList = [[NSMutableArray alloc] init];

you can add above lines of code either at viewDidLoad or at Segue Method

hope it helps you.

1 Comment

Please Uncomment self.toDoItem.itemList = [[NSMutableArray alloc] init]; this line of code in viewDidLoad.

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.