开发者

making NSArray global

开发者 https://www.devze.com 2023-03-08 22:58 出处:网络
i have NSarray which i want to acces from all of my methods,(i want it Global), i m going to populate this array in one of my methods defined in .m file(only once).

i have NSarray which i want to acces from all of my methods,(i want it Global), i m going to populate this array in one of my methods defined in .m file(only once). my question is ... is it really possible to declare a NSArray in .h and define it place somewhere else or it just has to be defined when it is declared(initialization).

MY CURRENT CODE

.h file

@interface slots2ViewController : UIViewContr开发者_开发问答oller {
NSArray* paylinesArr;
}

i m calling follwing method from ViewDidLoad

.m file

-(void)init_payline_arr
  {
NSString* filePath = @"/Users/net4uonline/Desktop/slots2/paylines.txt";//filepath...
NSString *fileContents = [NSString stringWithContentsOfFile: filePath];
paylinesArr = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    }

i m not able to use paylinesArr array from other methods the app crashes if follwing function is getting called

-(IBAction)ShowVal

 {
  NSLog(@"number of elements! %@",[paylinesArr count]);
  }

Or should i use NSMutabbleArray instead?

if you want to see then i have uploaded my desktop video while i m using debug tools! the link to video

in this video i press the record button of the debugger(i have ns zombie enabled and the retain count as well),the app starts,i press spin button and apparently it crashes...then i show you the code which has

   paylinesArr = [[fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] retain]; 

which retains the paylineArr


NSMutableArray won't help you! The reason of your error is that your paylinesArr variable is autorelease variable, so probably it was deallocated before ShowVal is called. Try to retain it like

-(void)init_payline_arr {
    ...
    paylinesArr = [[fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] retain];
    ...
}

This should work. But don't forget to release it in viewDidUnload method:

- (void)viewDidUnload {
    [super viewDidUnload];
    ...
    [paylinesArr release];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号