views:

41

answers:

1

Heres the code I'm using, see my error afterwards

@interface MyAppDelegate : NSObject  {
  NSString *userName;
}
@property (nonatomic, retain) NSString *userName;
...
@end

and in the .M file for the App Delegate you would write:

@implementation MyAppDelegate
@synthesize userName;
...
@end

Then, whenever you want to fetch or write userName, you would write:

MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
someClass.someString = appDelegate.userName;  //..to fetch
appDelegate.userName = ..some NSString..;     //..to write

warning: type 'id ' does not conform to the 'MyAppDelegate' protocol

What am I missing in my code ?

+5  A: 

You should add a cast to MyAppDelegate

MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];

Guy Ephraim
Added code snippet
Guy Ephraim
This is the answer--cast to your type of App Delegate as you pull the reference from UIApplication. That said, if you have many of those sorts of data fields, you should consider housing them in a data manager singleton. Keeping all your data as properties of your application delegate isn't a fantastic approach.
Dan Ray
Dan, I agree, it is not the best practice to the the AppDelegate for these kind of hostings.
Guy Ephraim
I'm using an NSNumber as well as a NSString and I'm getting error, see below my code.
Jules
You should do intValue and not integerValue
Guy Ephraim
This should probably be asked as a separate question, as I can't type a well formatted answer in a comment. Basically, arrays only store objects, not primitive types, so your passing an object to numberWithInt: when it expects an integer.
Robot K