Accessing variables in another class - objective-c

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.
//implementation
#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?

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;
Edit
// 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;

I shared the answer for the similar issue (take a look at How can I access variables from another class?). However, I could repeat it here.
In "XCode" you need to make import, create object by declaring it as the property, and then use "object.variable" syntax. The file "Class2.m" would look in the following way:
#import Class2.h
#import Class1.h;
#interface Class2 ()
...
#property (nonatomic, strong) Class1 *class1;
...
#end
#implementation Class2
//accessing the variable from balloon.h
...class1.variableFromClass1...;
...
#end

Related

Declaring instance variables in iOS - Objective-C

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.
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.
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

Accessing public fields in Objective C

I've tried the following sample code:
#import "Foundation/Foundation.h"
#interface example
{
#public NSString* name;
}
#end
#implementation example #end
int main()
{
example* me;
me->name = #"World";
}
And it appears my code hates me at this point. I do understand how much of a bad idea it is to make a field public, but I'm not sure why I'm getting an error at that last line in main().
There is a lot wrong here
You class should subclass NSObject so it should be declared as
#interface example : NSObject
{
#public NSString* name;
}
#end
Next you actually need an instance of the class to get at it's values e.g.
example *me = [[example alloc] init];
NSLog(#"%#", me->name);
Next classes are named starting with an uppercase letter and normally have a prefix e.g. I would use
PSExample
Next please don't access instance variables like this, you should make your objects state available through accessors rather than give direct access.
You're not allocating or initializing your me variable. You probably want to inherit from NSObject and then use this:
example *me = [[example alloc] init]
At the very least you need to alloc it.
You need to initialize your variable before you can access it. Also you should derive your class from NSObject.
#import "Foundation/Foundation.h"
#interface example : NSObject
{
#public NSString* name;
}
#end
#implementation example #end
int main()
{
example* me = [[example alloc] init];
me->name = #"World";
}

Access private instance variable of parent class

I can't figure out why class B can access class A private instance variable.
Here is my code
A.h
#import <Foundation/Foundation.h>
#interface A : NSObject
{
#private
int x;
}
#property int x;
-(void)printX;
#end
A.m
#import "A.h"
#implementation A
#synthesize x;
-(void)printX
{
NSLog(#"%i", x);
}
#end
B.h
#import "A.h"
#interface B : A
{
}
#end
main.m
B *tr = [[B alloc] init];
tr.x = 10;
[tr printX];
Here I can access instance variable of A class x despite it is declarated as private ?
You are not accessing the private variable there, at least not directly: you are accessing a public property, which has legitimate access to the private ivar.
Your code is equivalent to this:
B *tr = [[B alloc] init];
[tr setX:10];
[tr printX];
The #synthesize statement created the getter and the setter methods for you. If you want only a getter to be available, mark your property readonly, and do all writings through an ivar in the A class.
In your implementation file do this on the top..
#interface A : NSObject
{
#private
int x;
}
#property int x;
#end
this way x will be private since it is in the implementation file. not the interface section...all classes import the interface section of A ..so it's variable are accessible to its subclasses.

How can I assign values to other class variable in objective-c

The below coding is working and I can see the values in my second screen. But I am using the same in other classes with different variables in this format. But it dosent show me the variable if after i type the classname with a dot. I cant figure this out. Is there any way to pass values to other class.
InstallProfiler_2 *installProfiler2 = [[InstallProfiler_2 alloc] initWithNibName:#"InstallProfiler_2" bundle:nil];
installProfiler2.profilerType2 = profilerType;
[self.navigationController pushViewController:installProfiler2 animated:NO];
[installProfiler2 release];
Make sure that:
You have imported the class header.
The #property declarations are in this header and not a class extension.
#property refers to ivars so when you say
if after i type the classname with a dot
this terminology is incorrect, you probably mean after you start typing the name of the variable which has points to an instance of a class.
ClassA.h
#interface ClassA : NSObject
#property (nonatomic, weak) NSInteger myInt;
#end
ClassA.m
#implementation ClassA
#synthesize myInt = _myInt;
#end
ClassB.m
#import "ClassA.h" // <- Import the header of the class
# implementation ClassB
// .. other methods and stuff
- (void)myMethod;
{
ClassA *instanceOfClassA = [[ClassA alloc] init]; // <- Working with an instance not a class
instanceOfClassA.myInt = 1;
}
#end
UPDATE
Make sure your #property () does not have readonly between the round brackets.
Also make sure you have either #synthesize'd the ivar in the implementation or have provided both a getter and a setter for the ivar.
Failing that show some relevant code so we can actually see what your doing - we are answering pretty blindly here.
The dot syntax is only available with property/synthesize
Create a custom setter/getter:
+ (BOOL)awesomeClassVar {
return _classVar;
}
+ (void)setAwesomeClassVar:(BOOL)newVar {
_classVar = newVar;
}
then call as a method from the other class:
BOOL theOtherClassVar = [AwesomeClass awesomeClassVar];
[AwesomeClass setAwesomeClassVar:!theOtherClassVar];

How can I access variables from another class?

There is probably a very simple solution for this but I can't get it working.
I have got multiple classes in my Cocoa file. In one of the classes class1 I create a variable that I need to use in another class class2 as well. Is there a simple way to import this variable in class2?
You can either make the variable public, or make it into a property. For example, to make it public:
#interface Class1
{
#public
int var;
}
// methods...
#end
// Inside a Class2 method:
Class1 *obj = ...;
obj->var = 3;
To make it a property:
#interface Class1
{
int var; // #protected by default
}
#property (readwrite, nonatomic) int var;
// methods...
#end
#implementation Class1
#synthesize var;
...
#end
// Inside a Class2 method:
Class1 *obj = ...;
obj.var = 3; // implicitly calls [obj setVar:3]
int x = obj.var; // implicitly calls x = [obj var];
You could expose the variable in class2 as a property. If class1 has a reference to class2, class1 can then see the variable. Honestly, though, it sounds like you're a beginner to both Objective-C and object oriented programming. I recommend you read up more on both.
Here is a place to start for object oriented programming with Objective-C.
try making a file that holds your variables that need to be accessed throughout the app.
extern NSString *stringVariable;
#interface GlobalVariables
#property (retain, nonatomic) NSString *stringVariable;
#end
and in the GlobalVariables.m file add
#import "GlobalVariables.h"
#implements GlobalVariables
#synthesize stringVariable;
NSString *stringVariable;
#end
And then as long as you import GlobalVariables.h into which ever .m files you need to access that variable in you can assign and access anywhere throughout your program.
EDIT
My answer that I have given above is differently not the way I would go about doing this now.
It would be more like
#interface MyClass
#property (nonatomic, strong) NSString *myVariable;
#end
then in the .m file
#implementation MyClass
#sythesize = myVariable = _myVariable; // Not that we need to do this anymore
#end
Then in another class in some method I would have
// .....
MyClass *myClass = [[MyClass alloc] init];
[myClass setMyVariable:#"My String to go in my variable"];
// .....
In "XCode" you need to make import, create object by declaring it as the property, and then use "object.variable" syntax. The file "Class2.m" would look in the following way:
#import Class2.h
#import Class1.h;
#interface Class2 ()
...
#property (nonatomic, strong) Class1 *class1;
...
#end
#implementation Class2
//accessing the variable from balloon.h
...class1.variableFromClass1...;
...
#end
Thanks! :-)