I’ve got 2 types of var in my appDelegate : int and NSMutableArray
I can access the int var in my viewController but cannot access the NSMutableArray
Here is the code :
appDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,NINetworkImageViewDelegate,FBSessionDelegate, FBDialogDelegate>
{
UIWindow *window;
UINavigationController *navController;
int nbNewsNonLues;
NSMutableArray *tableauNews;
}
@property (strong, nonatomic) NSMutableArray *tableauNews;
@property (nonatomic) int nbNewsNonLues;
@end
these var are well initialized in AppDelegate.m (checked it)
then I try to acces them with this code in ViewController.m
@synthesize appDelegate = _appDelegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%@", appDelegate.tableauNews);
NSLog(@"%d", appDelegate.nbNewsNonLues);
NSLog(@"%@", appDelegate);
}
return self;
}
the log returns
2012-07-23 02:58:21.475 Aviso_0.1[2990:11303] (null)
2012-07-23 02:58:25.432 Aviso_0.1[2990:11303] 2
2012-07-23 02:58:25.432 Aviso_0.1[2990:11303]
So I cant access tableauNews but I can access and modify nbNewsNonlues??
I know this is a noob problem but I read and googled for hours, there must be smthing I missed about delegation
Help needed,
These two lines are not the same:
In the first one, you create an iVar called tableauNews. In the second one, you create a property called tableauNews with a backing iVar called _tableauNews. Note the under score in the backing iVar: _tableauNews. So you have created two different objects. When you allocate, access, etc. the iVar tableauNews and the property self.tableauNews, you are allocating, accessing two different objects. But the iVar _tableauNews is the same as the property self.tableauNews as noted earlier. To prevent this type of error in the future, the convention is to add a prefix underscore with your iVar.
Updated to answer the question in comment section:
In this line
@synthetise tableauNews = _tableauNews, that is how you declare a backing iVar (_tableauNews) for the property (tableauNews). When you declare the propertytableauNewsas above and in the .m file when you try to declare the corresponding@synthesize tableauNews, Xcode automatically suggests the under score version (_tableauNews) as the backing iVar for you so you do not need to declare it (_tableauNews) in the iVars declaration section of the header file. You can however associate your property with your own backing iVar. For example, if in the header file you have declared an iVar calledtableauNewsTestthen you can theoretically do this in the .m file@synthesize tableauNews = tableuNewsTest. But it is not recommended.