Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

srinivas.be88

macrumors newbie
Original poster
Aug 10, 2010
24
0
hi folks,
i am new to iPhone programming.i have a one question.that is how to pass the variable defined in one class to another class . these two class didnot use inheritence feature.
for eg:
Code:
 class A
 {
     int go;
    // some methods ..
 }
 

class B
{
   some method
{ 
 i want to use "go" variable in this method . is it possible to access the variable from the class A.
}

}


please help me to clear the above problem
 

ranguvar

macrumors 6502
Sep 18, 2009
318
2
Yes, it's possible. You'll have to decide whether you want to use an instance or a class variable. In the case of an instance variable, you'd write:

Code:
@interface SomeClass : NSObject {
	int someInt;
}
- (int)someInt;
@end


@implementation SomeClass
- (int)someInt {
	return someInt;
}
@end

and:

Code:
@interface AnotherClass : NSObject  {
}
@end


@implementation AnotherClass
- (void)someMethod {
	SomeClass *instance = [[SomeClass alloc] init];
	NSLog(@"someInt from SomeClass instance: %d", [instance someInt]);
}
@end


If you want a class variable, you'd write something like this:

Code:
@interface SomeClass : NSObject  {
}
+ (int)someInt;
@end


@implementation SomeClass
+ (int)someInt {
	return 4;
}
@end

and:

Code:
@interface AnotherClass : NSObject  {
}
@end

@implementation AnotherClass
- (void)someMethod {
	NSLog(@"someInt from SomeClass: %d", [SomeClass someInt]);
}
@end

In most cases, you'll probably want to use the instance variable. Also, make sure you read this document thoroughly.
 

srinivas.be88

macrumors newbie
Original poster
Aug 10, 2010
24
0
reply

thank you for your reply sir,
sir, already i had created the object for two classes.
so i changed the value of variable where defined in one class. and displays that variable in another class..
for eg:
Code:
 @interface ClassA
 { 
    int a;
  }
 -(void) change : (int) a;
@end


@implement ClassA
 -(void) change : (int) a
{
  a=a+10;
}

@end

----------------------------------

Code:
@interface ClassB
{
 }
-(void) display;
@end

@implement ClassB
 -(void)display
{
//here i want to display the variable defined in ClassA's change method.
}


hope you understood this...is it possible to display the variable defined in the ClassA's chang method.
 

ranguvar

macrumors 6502
Sep 18, 2009
318
2
Hm, I don't understand what you mean. If you say "display", do you mean that you want to display the variable in a UIView, or do you mean that you want to further access and modify the int defined in ClassA? And are you sure that you have understood object orientation?
 

firewood

macrumors G3
Jul 29, 2003
8,108
1,345
Silicon Valley
Forget doing it with objects.

Make your variable a global variable that you can access from anywhere.

After you learn to write programs without cognitive overload, then you can learn how to develop larger projects using reusable objects with cleanly encapsulate instance state.

Humans have to stumble around like a toddler before running 5k's.
 

RonC

macrumors regular
Oct 18, 2007
108
0
Chicago-area
You're combining two different concepts into one thing. I see 2 requirements in your code/description:
  1. Add 10 to a number
  2. Retrieve the current value of a number

You don't need 2 methods for that, you just need to implement it such that the 1 method does both things.

In the code you presented, you're almost there. Change the signature of the change method to return an int. Change the code in the implementation to actually return the value.

Then in the code that calls "change," you get the incremented value back.

There is an opportunity to improve the design. When I think about these things, modifiers (like the first requirement) are separate concepts from accessors (like the second). I like to make them separate methods - it focuses the code on doing one thing, and makes it easy to compose more complicated actions from simpler ones. For example, if you ever want to get the value WITHOUT incrementing it, then you'd probably be better off creating two methods: one to do the incrementing (which may or may not return the current value) and another to do the fetching of the current value.

Given the current list of requirements you've implied/stated, this improvement is not necessary.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.