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
Leem.fin
  • 40,781
  • 83
  • 202
  • 354

3 Answers3

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.

Avi
  • 7,469
  • 2
  • 21
  • 22
  • 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. – Leem.fin Aug 17 '16 at 08:00
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

Community
  • 1
  • 1
Ahmed Abdallah
  • 2,338
  • 1
  • 19
  • 30
  • 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 – Leem.fin Aug 17 '16 at 08:01
  • Like Avi told you just in visibility that this variable will be `@public or @protected or @private`, just it :). – Ahmed Abdallah Aug 17 '16 at 08:16
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

Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39