Take the 2-minute tour ×

I have integers in a class1 that i need to use in class2. I imported the .h file for class1 in the .m file of class2, but i still can't access the variable. Don't know why! :(

I even created a property for each integer in the .h file of class1 and synthesized it in the .m file.

Anyone know what the problem is?

basically, this is what i have in class1.h

//interface here
    NSInteger row;
    NSInteger section;

@property NSInteger row;
@property NSInteger section;

and this is the .m file for class1.

@synthesize section = _section;
@synthesize row = _row;

and then in the implementation of class2, i have this

#import "Class2.h"
#import "Class1.h"

How do i access those integers in a method in class 2?

share|improve this question
are you trying Class1 * aClass1Var = [[Class1 alloc] init]; aClass1Var.intVal1 = 0; or just Class1.intVal1 = 0; ? –  A-Live Feb 23 '12 at 23:12
in Class1.h before @end add following line @property int myInt; then in Class1.m after @implementation add @synthesize myInt; Or post your code –  0xDE4E15B Feb 23 '12 at 23:14
i edited my question to include the code –  Hauwa Feb 23 '12 at 23:30
Great, but that doesn't show how you try to access the properties. Also, doesn't my answer work for you? –  sch Feb 23 '12 at 23:56
add comment

1 Answer

You need to create an instance (object) of class1 to be able to access the properties (variables).

// Create an instance of Class1
Class1 *class1Instance = [[Class1 alloc] init];

// Now, you can access properties to write
class1Instance.intProperty = 5;
class1Instance.StringProperty = @"Hello world!";

// and to read
int value1 = class1Instance.intProperty;
String *value2 = class1Instance.StringProperty;


// Create an instance of Class1
Class1 *class1Instance = [[Class1 alloc] init];

// Now, you can access properties to write
class1Instance.row = 5;
class1Instance.section = 10;

// and to read
NSInteger rowValue = class1Instance.row;
NSInteger sectionValue = class1Instance.section;
share|improve this answer
could you show me how to do that please? –  Hauwa Feb 23 '12 at 23:15
yay! Tried it! It works! Thanks! :D –  Hauwa Feb 24 '12 at 0:07
If this answer works for you, you should check (tick) it to reward @sch –  zenopolis Feb 24 '12 at 8:24
add comment

Your Answer


