Reputation: 4614
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.
Upvotes: 1
Views: 2320
Reputation: 437642
If you just want to declare a new class, State
, you declare your instance variables as such (inside braces, no explicit initialization):
@interface State
{
NSString *forgotPassword;
NSMutableArray *categorySelection;
NSMutableArray *subCategorySelection;
NSString *string;
int tag;
int alertTag;
NSURL *stringURL;
NSURL *videoURL;
NSURL *imageURL;
int loginCount;
NSMutableArray *album;
NSString *videoFileName;
int videoCounting;
int loginUserId;
int imageTag;
}
@end
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 init
method.
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.:
@interface State
@property (nonatomic, strong) NSString *forgotPassword;
@property (nonatomic, strong) NSMutableArray *categorySelection;
@property (nonatomic, strong) NSMutableArray *subCategorySelection;
@property (nonatomic, strong) NSString *string;
@property int tag;
@property int alertTag;
@property (nonatomic, strong) NSURL *stringURL;
@property (nonatomic, strong) NSURL *videoURL;
@property (nonatomic, strong) NSURL *imageURL;
@property int loginCount;
@property (nonatomic, strong) NSMutableArray *album;
@property (nonatomic, strong) NSString *videoFileName;
@property int videoCounting;
@property int loginUserId;
@property int imageTag;
@end
And having declared the properties, you can now let the compiler synthesize the "setters" and "getters". For these @property
declarations, if you're using the latest compiler (Xcode 4.4 ... came out a week or so ago) you don't need to explicitly @synthesize
them anymore in your @implementation
. But if you're using an earlier compiler, you need to include @synthesize
for all of your @property
declarations, e.g.
@implementation State
@synthesize forgotPassword = _forgotPassword;
@synthesize categorySelection = _categorySelection;
@synthesize subCategorySelection = _subCategorySelection;
@synthesize string = _string;
// etc.
If you do that (declare a @property
and then @synthesize
it), 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 = _forgotPassword
will also generate the instance variable, but by including an underscore before the ivar name, you'll ensure you won't confuse the property self.forgotPassword
with 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, State
followed 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 existing State
class, as follows:
@interface StateSubClass : State
{
NSString *forgotPassword;
NSMutableArray *categorySelection;
NSMutableArray *subCategorySelection;
NSString *string;
int tag;
int alertTag;
NSURL *stringURL;
NSURL *videoURL;
NSURL *imageURL;
int loginCount;
NSMutableArray *album;
NSString *videoFileName;
int videoCounting;
int loginUserId;
int imageTag;
}
@end
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.
Upvotes: 4
Reputation: 4921
Variable declaration should be inside of brackets which should be like this
@interface State(private)
{
NSString *forgotPassword;
NSMutableArray *CategorySelection;
NSMutableArray *subCategorySelection;
NSString *string;
int Tag;
int alertTag;
NSURL *stringURL;
NSURL *videoURL;
NSURL *imageURL;
int loginCount;
NSMutableArray *album;
NSString *videoFileName;
int videoCounting;
int loginUserId;
int ImageTagg;
}
-(void)sampleMethod1;
-(void)sampleMethod2;
-(void)sampleMethod3;
@end
After Brackets you can declare methods that are using in the class
Upvotes: 0
Reputation: 4042
the problem is that you are initializing the variables in @interface itself. You can only declare variables, and the initialization is done in @implementation.
thus your @implementation should contain,
@implementation
forgotPassword=nil;
CategorySelection = nil;
subCategorySelection= nil;
string = nil;
Tag= 0;
alertTag=0;
stringURL =nil;
videoURL =nil;
imageURL = nil;
loginCount = 0;
videoFileName = nil;
videoCounting=0;
loginUserId = 0;
ImageTagg = 0;
@end
while your @interface contains,
@interface State
NSString *forgotPassword;
NSMutableArray *CategorySelection;
NSMutableArray *subCategorySelection;
NSString *string;
int Tag;
int alertTag;
NSURL *stringURL;
NSURL *videoURL;
NSURL *imageURL;
int loginCount;
NSMutableArray *album;
NSString *videoFileName;
int videoCounting;
int loginUserId;
int ImageTagg;
@end
Notice that private keyword has been removed. For declaring private variables in objectice C, we declare them in .m file itself. An example is given below,
.h file
@interface classname{
// variable declaration
}
//variable property declaration
//method declaration
@end
.m file
@interface classname()
//private variables
@end
@implementation
//do your logic here
@end
Upvotes: 0
Reputation: 726659
It looks like you declared a category on your class; you cannot add instance variables in a category.
You can, however, add them in a class extension. For that, remove the name of the category (private
), and remove initialization, leaving the code as follows:
@interface State () {
// Note how the initialization is gone:
NSString *forgotPassword;
NSMutableArray *CategorySelection;
// More variables; no initialization!
}
// More stuff...
@end
All initialization needs to happen in your designated initializer (e.g. your init
method), like this:
-(id)init {
if (self = [super init]) { // Yes, it's =, not ==
// Perform all your initialization here:
*forgotPassword= ...;
*CategorySelection = ...;
}
return self;
}
Upvotes: 4