@implementation, vars and ARC

感情迁移 提交于 2019-12-12 03:28:26

问题


In my project There is a controller A with next code:

@implementation NKAddPostViewController
int _characterCounter = 512;
...

I change this variable in code, that it value is 400. Than i do popViewControllerAnimated.

But when i go to this controller again the value still 400. In viewWillAppear, viewDidLoad it still 400. Why? It seems that controller A is retained, but when i debug properties in viewDidLoad, they are nill until they are initialized again.

Here is the implementation of transfer, so nothing retain controllerA:

NKAddPostViewController *aContr = [NKAddPostViewController new];
[self.navigationController aContr animated:YES];

2 questions:

  1. So why _characterCounter retains?
  2. Why when controller initializing the line int _characterCounter = 512; don't assign 512 to _characterCounter?

回答1:


In your comments you say "3) implement like ivar in implementation and immediately assign value. 3-rd way is easier and faster"

3rd way is wrong. "ivar in implementation" is actually a private static variable, and does not do what you want.

You can create a private category and define a new instance variable there:

In your .m file, add this:

@interface NKAddPostViewController()
{
  int _characterCounter;
}

- (instancetype) init;
{
  self = [super init]; 
  if (!self) 
    return nil;
  _characterCounter  = 512;
}

That should do what you want.




回答2:


What you did is declare a global variable. It's not tied to the class at all, and has absolutely nothing to do with it. Assigning the value of 512 happens when the app loads (really at build time), and only "happens" once. Because it's global, any value you set remains until you change it again.

If you want an instance variable that's tied to the class, declare it like this...

@implementation NKAddPostViewController
{
    int _characterCounter;
}

Note that you can't initialize variables with the declaration. Use your init override or viewDidLoad:.



来源:https://stackoverflow.com/questions/36912666/implementation-vars-and-arc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!