iphone NSMutableArray loses objects at end of method

Posted by Brodie4598 on Stack Overflow See other posts from Stack Overflow or by Brodie4598
Published on 2010-06-11T01:02:21Z Indexed on 2010/06/11 1:22 UTC
Read the original article Hit count: 288

Hello - in my app, an NSMutableArray is populated with an object in viewDidLoad (eventually there will be many objects but I'm just doing one til I get it working right). I also start a timer that starts a method that needs to access the NSMutableArray every few seconds. The NSMutableArray works fine in viewDidLoad, but as soon as that method is finished, it loses the object.

myApp.h

@interface MyApp : UIViewController {

    NSMutableArray *myMutableArray;
    NSTimer *timer;
}

@property (nonatomic, retain) NSMutableArray *myMutableArray;
@property (nonatomic, retain) NSTimer *timer;
@end

myApp.m

#import "MyApp.h"

@implementation MyApp
@synthesize myMutableArray;

- (void) viewDidLoad {
    cycleTimer = [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(newCycle) userInfo: nil repeats:YES];
MyObject *myCustomUIViewObject = [[MyObject alloc]init];
[myMutableArray addObject:myCustomUIViewObject];
[myCustomUIViewObject release];
NSLog(@"%i",[myMutableArray count]);  /////outputs "1"
}

-(void) newCycle {
    NSLog(@"%i",[myMutableArray count]);  /////outputs "0" ?? why is this??

}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c