i am new to objective c. i have added an interface in one class file and i have declared some variables there . while compiling it shows “Cant declare variable inside @interface and @protocol” error in all the declaration.
this is the sample code
#import "State.h"
@interface State(private)
NSString *forgotPassword=nil;
NSMutableArray *CategorySelection = nil;
NSMutableArray *subCategorySelection= nil;
NSString *string = nil;
int Tag= 0;
int alertTag=0;
NSURL *stringURL =nil;
NSURL *videoURL =nil;
NSURL *imageURL = nil;
int loginCount = 0;
NSMutableArray *album;
NSString *videoFileName = nil;
int videoCounting=0;
int loginUserId = 0;
int ImageTagg = 0;
@end
@implementation State
+(void) setforgotPasswordText:(NSString *) passwordText{
forgotPassword = passwordText;
}
I am the beginner , so guide me to fix this issue. thanks.
If you just want to declare a new class,
State, you declare your instance variables as such (inside braces, no explicit initialization):If you’re using ARC, you don’t need to initialize them, because it will set all of these to zero or nil. If you’re not using ARC (but why wouldn’t you), you’d initialize these in your
initmethod.And I notice that you’re writing your own “setter” (e.g.
setForgotPassword). If you want the compiler to do this for you (i.e. to “synthesize” them for you), first declare these variables as properties, e.g.:And having declared the properties, you can now let the compiler synthesize the “setters” and “getters”. For these
@propertydeclarations, if you’re using the latest compiler (Xcode 4.4 … came out a week or so ago) you don’t need to explicitly@synthesizethem anymore in your@implementation. But if you’re using an earlier compiler, you need to include@synthesizefor all of your@propertydeclarations, e.g.If you do that (declare a
@propertyand then@synthesizeit), the compiler will, behind the scenes, create the instance variable for you and then automatically generate a “setter” (i.e. a method that is “set” followed by your variable name, e.g. “setForgotPassword”) and a “getter” method (a method with the same name as your variable which will retrieve the variable contents for you) for each of your properties. Note, for all of these properties, the@synthesize forgotPassword = _forgotPasswordwill also generate the instance variable, but by including an underscore before the ivar name, you’ll ensure you won’t confuse the propertyself.forgotPasswordwith the instance variable_forgotPassword.If you wanted it to be a category (basically the addition of new methods to be applied to an existing class, designated by referencing an existing class,
Statefollowed by a category designator,State (private)), then you can’t include new variables. I don’t know if that was really your intent (I doubt it). But, if you really want to do that but you really need these new variables, you could instead subclass your existingStateclass, as follows:And if you’ll notice, I also changed your variable names to conform to Apple conventions of initial lowercase letter. Classes start with uppercase, variables start with lowercase.