0

In Objective c, what is the differences between instance variables var1 and var2 showing below?

(This code is in one .m file, does it make any difference if the @interface is in a header file & @implementation is in implementation file? I mean any difference comparing with that in one file regarding to the two instance variables.)

@interface MyService {
 NSString *var1;
}

@end

@implementation MyService {
 NSString *var2;
}
@end
2
  • What solution do you expect Leem.fin? Commented Aug 17, 2016 at 12:20
  • Do you understand my answer? Commented Aug 18, 2016 at 6:44

3 Answers 3

4

The difference between them is visibility. The variable defined in the @interface section is visible to any code which imports the interface. The variable declared in the @implementation section is only visible to code within the class implementation.

If the @interface is declared in the implementation file, it will act, for all practical purposes, the same as declaring it in the @implementation section.

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

1 Comment

I don't understand your second paragraph, it sounds like "if @interface is declared in the implementation file, it is the same as it is declared in implementation file" what? Could you please use some code snippet to explain more clearly? thanks.
0

Instance variables declared in the implementation are implicitly hidden (effectively private) and the visibility cannot be changed - @public, @protected and @private do not produce compiler errors (with the current Clang at least) but are ignored.

you can found it here

2 Comments

What about my question about define in one implementation file (both @interface & @implementation like my code shows VS separate files, header file & implementation files), does it make any difference regarding to var1 and var 2
Like Avi told you just in visibility that this variable will be @public or @protected or @private, just it :).
0

I have deep searched for your question.Well asked Leem.fin brother.I tried sample one

#import "SecondViewController.h"
@interface SecondViewController ()
{
    NSString *variableOne;
}
@end

@implementation SecondViewController
{
   NSString *variableTwo;
}

- (void)viewDidLoad
{
  [super viewDidLoad];
  // Do any additional setup after loading the view.
  [self setValueToString];
   NSLog(@"The variable One is - %@",variableOne);
   NSLog(@"The variable Two is - %@",variableTwo);
}

-(void)setValueToString
{
    variableOne = @"iOS";
    variableTwo = @"iPhone";
}

The printed results are

 The variable One is - iOS
 The variable Two is - iPhone

But when I tried to access these in Class method

+(void)changeStrings
{
   variableOne = @"iPad"; //I get error here
   variableTwo = @"iMac"; //I get error here
}

The error shows

Instance variable 'variableOne' accessed in class method

Instance variable 'variableTwo' accessed in class method

From above code I understood

Both are instance variables

That can be accessed only in instance methods

There is no difference between them

So Where to put

Difference between them

Difference between putting variable inside interface and implementation

Comments

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.