Bro*_*die 2 iphone objective-c nsmutablearray ipad
在我的应用程序中,一个NSMutableArray在viewDidLoad中填充了一个对象(最终会有很多对象,但我只是在做一个直到我让它正常工作).我还启动了一个计时器,它启动一个需要每隔几秒访问NSMutableArray的方法.NSMutableArray在viewDidLoad中工作正常,但只要该方法完成,它就会丢失该对象.
myApp.h
@interface MyApp : UIViewController {
NSMutableArray *myMutableArray;
NSTimer *timer;
}
@property (nonatomic, retain) NSMutableArray *myMutableArray;
@property (nonatomic, retain) NSTimer *timer;
@end
Run Code Online (Sandbox Code Playgroud)
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??
}
Run Code Online (Sandbox Code Playgroud)
myApp.m不会保留数组self.myMutableArray,除非你使用它来分配它,除非你使用self.前缀你没有得到的好处(nonatomic, retain).
您的结果指向一个在您从中读取时未分配的数组.它是这个或者在使用之前没有分配数组addObject(不太可能给出你的NSLog结果).
- (void) viewDidLoad {
self.myMutableArray = [NSMutableArray array];
...
}
Run Code Online (Sandbox Code Playgroud)
可能会解决这个问题.
| 归档时间: |
|
| 查看次数: |
499 次 |
| 最近记录: |