1

I have a NSInteger which always returns zero :( I have it declared like:

NSInteger level;
@property(nonatomic, assign) NSInteger level;

The synthetize:

@synthesize fileName, filePath, theActualIndexPath, titleBar, level;

Then when using NSLog(@"%d", level) always returns 0, even after

level++; 

Or

level += 1;

What I'm doing wrong? I'm 100% sure of adding 1 to level but can't understand what's wrong :(

EDIT:

The if statement where I use this NSInteger

if (level == 0) {
    self.navigationItem.title = @"Test";

} else {
    self.navigationItem.title = self.titleBar;

}

That always ends on the first, even after adding 1

u0b34a0f6ae
  • 48,117
  • 14
  • 92
  • 101
pmerino
  • 5,900
  • 11
  • 57
  • 76
  • Did you check your RAM for corruption? Otherwise, this cannot happen when all info you gave us is right. –  Dec 09 '11 at 17:13
  • hmm I doubt it's being corrupted, as OS X and all apps seems to work fine. The only time I use 0 is on a if statement and on NSLog, and ofcourse when trying to sum one, but that's what's not working :( – pmerino Dec 09 '11 at 17:14
  • Show the `@synthesize statement` and the rest of the method surrounding setting and displaying level. – zaph Dec 09 '11 at 17:22
  • I don't think the problem is in that lines of code. Have you tried changing the compiler (from gcc to llvm or the other way around)? clean and build? build and analyze? – djromero Dec 09 '11 at 17:56
  • I think the problem is that I try to use a multi level navigation controller. For this I alloc the same view controller again, and I think the level integer is being reallocated – pmerino Dec 09 '11 at 18:06

3 Answers3

1

The variable that being changed is not the same variable being accessed (in the if statement).

In the console print the values and addresses of level both where it is being set (before and after) and where s is being accessed (in the if statement.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • 1
    I think the problem is that I try to use a multi level navigation controller. For this I alloc the same view controller again, and I think the level integer is being reallocated – pmerino Dec 09 '11 at 18:06
1

Why are you declaring level twice?

Remove this line: NSInteger level;

The first declaration of level is a globally scoped variable, the second an instance variable of the class. Depending on your scope, the level being incremented by ++level is different from the level being printed by NSLog("%d", level); and tested by if (level == 0).

EDIT

Yes. Why would you expect level to not be newly allocated if you alloc another instance of the view controller? If you want to do that then you've either got to make the variable static, which shares it among instances of the same class, or make sure you do something like this:

MyViewController *newViewController = [[MyViewController alloc] init]; // However this goes
newViewController.level = oldViewController.level; // Make sure you pass the level on
john
  • 3,043
  • 5
  • 27
  • 48
  • I think the problem is that I try to use a multi level navigation controller. For this I alloc the same view controller again, and I think the `level` integer is being reallocated – pmerino Dec 09 '11 at 18:06
0

Try this :
in the .h

@property(nonatomic) NSInteger level; // not an object assign not needed

in the .m

@synthesize level = _level;  
self.level = self.level + 1;  // and try this

And remove the line

NSInteger level;

Is this working?

Vincent Bernier
  • 8,674
  • 4
  • 35
  • 40
  • There a side effect to removing the declaration `NSInteger level;`, the value of level (or in the recommendation _level) will not be available in the debugger. – zaph Dec 09 '11 at 17:27
  • Still not working, even changed the variable name to folderLevel, it still doesn't adds one :/ – pmerino Dec 09 '11 at 17:29
  • why is 'folderLevel' a red flag? – pmerino Dec 09 '11 at 17:32
  • folderLevel is meant to be a integer to track the level of the folder hierarchy my app has – pmerino Dec 09 '11 at 17:36
  • @zad0xsis you need to post more code than that, because there is a lot that can go wrong and your little piece of code is not showing that. And with my code you ABSOLUTLY need to use `self` if you are testing directly for `level` without the `self` it won't work. And rule of tum, you should always use self.aProperty instead of bypassing your accessor all the time. (excpt in a custom accessor where you need direct acces) – Vincent Bernier Dec 09 '11 at 18:27