Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an app that reads from an sqlite table and saves the data in an NSMutableArray that is declared in the main ViewController class subclass of UIViewController, this method is called at viewDidLoad. this NSMutable Array is declared as follows:

@property (strong, nonatomic) NSMutableArray *dataForTable;

This data is displayed in a table which is loaded fine at app launch. Now I declare a method just to log the contents of this NSMutableArray

-(void) arrayTest {
NSLog(@" data in test array is%@",dataForTable);}

and the output is also fine when I call it anywhere inside this class like for eg at

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[self arrayTest]; . . . . . . }

but this data in this NSMutableArray suddenly becomes (null) when I call to log from another class (a subclass of UITableViewCell created to track touchbegan and touchmoved for sliding of cell from left to right)

[[ViewController alloc]arrayTest];

this gives me output

data in test array is(null)

why is that? now if I call -textFieldShouldBeginEditing the output is fine again. why does array empty itself (or maybe i am just thinking it does) and then outputs the content fine again later?

share|improve this question
    
Well, first off you shouldn't do [[ViewController alloc]arrayTest]. Second, if you do NSLog(@"arrayTest = %@", [[[ViewController alloc] init] arrayTest]); you will be accessing a NEW instance of ViewController, so anything you did previously in another instance of ViewController will not be seen. –  Hot Licks Sep 13 '13 at 16:41

4 Answers 4

up vote 0 down vote accepted

The mistake is simple and yes quite stupid. As Hot Licks points out:

if you do NSLog(@"arrayTest = %@", [[[ViewController alloc] init] arrayTest]); you will be accessing a NEW instance of ViewController, so anything you did previously in another instance of ViewController will not be seen.

I was creating a new instance of viewController that does not already have any array filled with data. The data from the array can only be fetched if the same instance of array is accessed.

share|improve this answer

The instance needs to be initialized first. Try using [[[ViewController alloc] init] or [ViewController new] then call your method arrayTest .

share|improve this answer
    
this is not an answer. –  Stark Sep 13 '13 at 17:10
    
@Popeye sorry, I could not get ur point, could u plz explain –  singhabhi Dec 10 '13 at 11:41

The only problem I see is your line of code to test from outside the class: [[ViewController alloc]arrayTest];

This line allocates space for a new instance of ViewController but it does not actually initialize the instance variables.

Try: [[[ViewController alloc] init] arrayTest];

share|improve this answer
    
As I said above, if you do NSLog(@"arrayTest = %@", [[[ViewController alloc] init] arrayTest]); you will be accessing a NEW instance of ViewController, so anything you did previously in another instance of ViewController will not be seen. –  Hot Licks Sep 13 '13 at 16:42

It's likely you didn't allocate the array in your init method. Just because you have declared a @property for the array, doesn't mean it's created for you:

- (id)init {
    self = [super init];
    if (self) {
        self.arrayTest = [NSMutableArray array];
    }
    return self;
}
share|improve this answer
    
does not change. databForTable was 'alloc' and 'init' in viewDidLoad already. 'dataForTable = [[NSMutableArray alloc]init];' –  Ishaan Sejwal Sep 13 '13 at 16:19
1  
@IshaanSejwal if you want to test for the existence of dataForTable immediately after creating an instance of ViewController, then you need to create it in the ViewController init method, not viewDidLoad. –  Mathew Sep 13 '13 at 16:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.