-1

Hey so I can't seem to figure out why my array isn't getting any data from my other classes, it's almost as if it's not calling the others at all.

 #import "AG_ViewController.h"
 #import "AG_Storage.h"
 #import "AG_AddItemViewController.h"

 @interface AG_ViewController ()

 @property NSMutableArray *mainArray;

 @end

 @implementation AG_ViewController

 - (void)viewDidLoad
 {
[super viewDidLoad];
self.mainArray = [[NSMutableArray alloc] init];
[self loadInitialData];
 }

 - (void)loadInitialData
 {
// Do any additional setup after loading the view, typically from a nib.

AG_Storage *item1 = [[AG_Storage alloc] init];
item1.itemName = @"Test1";
[self.mainArray addObject:item1];


AG_Storage *item2 = [[AG_Storage alloc] init];
item2.itemName = @"Test2";
[self.mainArray addObject:item2];
NSLog(@"%@", self.mainArray);

 }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     return [self.mainArray count];
 }


 - (UITableViewCell *)tableView:(UITableView *)tableViewer cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = [tableViewer dequeueReusableCellWithIdentifier:@"thisCell"];
AG_Storage *toDoItem = [self.mainArray objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;

if (toDoItem.completed) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else{
    cell.accessoryType = UITableViewCellAccessoryNone;
}

return cell;
 }

 - (IBAction)unwindToList:(UIStoryboardSegue *)segue
 {
AG_AddItemViewController *source = [segue sourceViewController];
AG_Storage *item = source.store;

NSLog(@"%@", item);

if (item != nil) {

    NSLog(@"%@", source);
    NSLog(@"%@", item);
    [self.mainArray addObject:item];
    NSLog(@"%@", self.mainArray);
    [tableView reloadData];

 }
 }

- (void)didReceiveMemoryWarning
 {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
 }




 @end

in AG_AddItemViewController i have this

 - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (sender != self.doneButton) return;

if (self.textField.text.length > 0) {
    self.store =[[AG_Storage alloc] init];
    self.store.itemName = self.textField.text;
    self.store.completed = NO;
    NSLog(@"%@", self.store);
}

}

When i try to add something, my NSLogs print this out and nothing is added.

 2014-03-21 14:08:24.395 AgendaBk[25633:a0b] (
"<AG_Storage: 0x89af2f0>",
"<AG_Storage: 0x89a5ee0>"
 )
 2014-03-21 14:08:30.405 AgendaBk[25633:a0b] <AG_Storage: 0xc0e3000>
 2014-03-21 14:08:30.409 AgendaBk[25633:a0b] (
"<AG_Storage: 0x897b5c0>",
"<AG_Storage: 0x898db60>"
 )

Thanks in advance, you guys helped me see something last time i wouldn't have thought about!

  • 2
    Are you allocating/initializing your array somewhere? – Stonz2 Mar 21 '14 at 18:12
  • Since the logs seem to indicate that the array has two items in it, I'm not sure this is actually a duplicate of those other questions, which all dealt with `[nil count]` being 0. – Chuck Mar 21 '14 at 18:46
  • We have no way to relate the above log output to your code (and I suspect the log output does not correspond to your listing at all). Put meaningful text into the NSLog statements. – Hot Licks Mar 21 '14 at 18:55
  • You may be right, @Chuck, but the `NSLog()` statements in the code also don't seem to correspond correctly to the given log output. In particular, `NSLog(@"%@", source);`, where `source` is a view controller, lines up with what looks like a printout of an array. – jscs Mar 21 '14 at 18:57
  • Actually, from the above info there is no evidence that this is a case of an uninitialized array pointer, so I'm not convinced the closing reason is valid. If the OP were to actually provide some useful information (logs that matched the code, eg) then I might vote to reopen. – Hot Licks Mar 21 '14 at 22:01
  • This issue is that this specific method is not allowing me to add to the array, my other method loads initial data and it adds stuff fine. I don't think this is a duplicate, i'll edit it and add in the other classes. Basically i'm confused because the `[self.mainArray addObject]` works fine in the `loadinitialdata`, but not in `unwindtolist` – user3430084 Mar 24 '14 at 17:23
  • The more I play with the the more I wonder if its not adding because of the `if` statement. Maybe what you were saying about `nil` earlier has something to do with it. EDIT: No it seems to be that my program isn't calling `unwindtolist` at all... Weird – user3430084 Mar 24 '14 at 17:36
  • Success! I was segueing wrong, i never set `unwindtolist` up correctly! Thanks for the second pair of eyes guys! – user3430084 Mar 24 '14 at 17:46

1 Answers1

0

Declare the array:

@implementation ViewController {
    NSMutableArray *mainArray;
}

And allocate it in a method (I do it in ViewDidLoad):

mainArray = [[NSMutableArray alloc] initWithCapacity:20];

Note: 20 is just an example number. And make sure you allocate the item you are storing. Assuming you have a NSObject .m and .h file.

In which you would do:

item = [[objectThatYouHave alloc]init];
WKC
  • 11
  • 5
  • 1
    You don't need to declare the ivar explicitly like that; it won't change the behavior of the OP's code. – jscs Mar 21 '14 at 18:22
  • Yeah that's my bad. And you don't have to, but it's a way of doing so. – WKC Mar 21 '14 at 18:23
  • I'd like to avoid doing this because in earlier versions of my code i did this and it created some problems, and for some reason it didn't like passing items to my storage class. – user3430084 Mar 24 '14 at 17:29