5

NSMutableArray is empty if called from outside?

 2 years ago
source link: https://www.codesd.com/item/nsmutablearray-is-empty-if-called-from-outside.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

NSMutableArray is empty if called from outside?

advertisements

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?


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;
}




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK