1

Ok, I've read a lot around these days about this topic and I alwyas get confused because the answers is different every search I make.

I need to know the best way to declare instance variables in iOS. So far I know I should only declare them inside .m file and leave .h clean. But I can't do it: the compiler gives me compilation erros.

Here is some code from .m only.

@interface UIDesign ()

// .m file
{
    NSString *test2 = @"test2";
}

@property (nonatomic, assign) int privateInt;

@end

@implementation UIDesign
{
    NSString *test1 = @"test1";
}

Both strings are declared incorrectly and I don't know why. The compiler says: expected ';' at end of declaration list.

So the question is: how can I declare instance variables? I will only need them inside the class.

1
  • MY friend , Variable declaration is different than definition. here you are initialzing a variable at the place of Declaration. Commented May 13, 2014 at 4:51

2 Answers 2

1

You cannot initialize instance variables. They are all initialized to nil or zeroes. So compiler expect a semicolon when you are writing an equal sign.

You can initialize them in init method.

Sign up to request clarification or add additional context in comments.

5 Comments

I don't have a init method. Should I make one just to set the values of my variables properly?
If you want to initialize them yes. You have.
I think you don't have to but you can. For example you can use the (void)viewDidLoad method to store your init lines. Because this method is called when the view is loaded (as it's said…). For my own, I rarely write my own init method in some viewController, except if I really have a lot of variables. In iOS, I just use the viewDidLoad method to init my variables.
This class is not a ViewController. It's just a class I implement to help me recycling some of my code.
Beside other motivations for an -init… (parameters on init, i. e.) you can make up your decision by this conclusion: 1) The RTE automatically set all ivars to representations of 0 (NULL, nil, Nil, 0, 0.0). 2) You should not set an ivar to the representation of 0 because of 1). 3) If you do not have an ivar, set to an value other then a representation of 0 (and no other init-code like registration $whereever), an -init… would be empty = useless. The other way round: If you have to set an ivar initially to something else than an representation of 0, add an -init…
1

You are attempting to add an instance variable to a class extension or category which is unsupported. [EDIT 2013-05-12 06-11-08: ivars in class extension are supported, but not in categories.] As an alternative:

@interface UIDesign : NSObject
@end

@interface UIDesign ()

@property (nonatomic, assign) int privateInt;
@end

@implementation UIDesign

@synthesize privateInt = _privateInt;

- (void)someMethod {
    self.privateInt = 42;
}

@end

On the other hand, if you just want to declare an instance variable inside the implementation, just do it there:

@implementation UIDesign {
    int _privateInt;
}

@end

EDIT: just noticed that you're also attempting to initialize instance variables in the declaration which is also unsupported. So:

@interface UIDesign : NSObject
@end

@implementation UIDesign {
    NSString *_test;
}

- (id)init {
    self = [super init];
    if( !self ) return nil;

    _test = @"Foo";

    return self;
}

@end

5 Comments

I don't have a init method. Should I make one just to set the values of my variables properly?
It depends. If the proper functioning of your class depends on an initial value for an ivar, then yes.
synthesizing is no longer necessary
Adding Ivars in a class extension is supported.
Yes, of course. My bad. In class extensions, but not categories. Edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.