1

I create a property countryNameInitials as NSMutableArray in AppDelegate.m interface and synthesize it. The purpose is to store global data to display in table view. In the table view controller:

#import "AppDelegate.h"
...

@implementation
#define AppDelegate [[UIApplication sharedApplication] delegate]
...

The problem is that I cannot use AppDelegate.countryNameInitials to access the data. Any idea?

Philip007
  • 3,190
  • 7
  • 46
  • 71
  • This is probably a naming conflict. Your #define has the same name as the AppDelegate class. Try changing your #define to another name. – vakio Sep 13 '12 at 09:12
  • Good thought. Yet I tried other names and still don't work – Philip007 Sep 13 '12 at 10:01

2 Answers2

3

The fact is that

 [[UIApplication sharedApplication] delegate]

will give you a generic UIApplcationDelegate; while you need your MyOwnAppDelegate (don't know how you called it) type to be able to use your countryNameInitials property. So, try with:

 #define AppDelegate ((MyOwnAppDelegate*)[[UIApplication sharedApplication] delegate])

then

AppDelegate.countryNameInitials

should work.

(I would not use the same name for the class and the macro, though).

sergio
  • 68,819
  • 11
  • 102
  • 123
  • I copy paste your #define line yet get an error "missing '[' at start of message send expression" – Philip007 Sep 13 '12 at 09:57
  • can you paste the line where you get the error? that syntax is working for me for property dereferencing... – sergio Sep 13 '12 at 10:04
  • `#define AppDelegate ((TableWithIndexingAppDelegate *)[[UIApplication sharedApplication] delegate])` – Philip007 Sep 13 '12 at 10:08
  • it seems strange to me that the `#define` itself produces the error... I can paste it in any project of mine and it compiles... – sergio Sep 13 '12 at 10:11
0

The problem of accessing countryNameInitials is due to I declared the property in AppDelegate.m. After I move it to AppDelegate.h, errors are gone.

Side notes: 1. AppDelegate as the identifier name works for me. Naming conflicts suggested by @Vakio is not relevant.

Philip007
  • 3,190
  • 7
  • 46
  • 71