使 NSArray 全局化

发布于 2024-11-10 11:57:48 字数 1224 浏览 7 评论 0原文

我有 NSarray,我想从我的所有方法访问它,(我希望它是全局的),我将在 .m 文件中定义的方法之一中填充此数组(仅一次)。 我的问题是...是否真的可以在 .h 中声明一个 NSArray 并在其他地方定义它,或者只需要在声明(初始化)时定义它。

我当前的代码

.h 文件

@interface slots2ViewController : UIViewController {
NSArray* paylinesArr;
}

我正在从 ViewDidLoad 调用以下方法

.m 文件

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

我无法从其他方法使用 paylinesArr 数组,如果调用以下函数,应用程序将崩溃

-(IBAction)ShowVal

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

或者 我应该使用 NSMutabbleArray 代替吗?

如果你想看的话,我已经在使用调试工具时上传了我的桌面视频! 在此视频中的视频链接

我按下调试器的录制按钮(i已启用 ns 僵尸并保留计数),应用程序启动,我按下旋转按钮,显然它崩溃了...然后我向您展示保留

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

paylineArr的代码

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 : UIViewController {
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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×眷恋的温暖 2024-11-17 11:57:48

NSMutableArray 不会帮助你!错误的原因是您的 paylinesArr 变量是 autorelease 变量,因此可能在调用 ShowVal 之前已释放它。尝试保留它,就像

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

这应该有效。但不要忘记在 viewDidUnload 方法中释放它:

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

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文