0

I tried to search for an answer about this (simple) question but nothing seems to work well, even books are not so specific (atleast the books I've read), or probably I'm simply missing something important, since I'm a little bit confused I've decided to try here.

Here's the question:

Say that I have a ClassA which contains one or many instance variables. Then I have a ClassB which contains an Instance Method that modify ClassA variable (is this possible right?) the classes have NOT inheritance from each other, both inherits from NSObject

Then I want to call the method of the ClassB over the ClassA object on the UIViewController. I believe I need a reference between classes but i'm not sure on how to set them to make them works. Imagine I have a Paper which contains writes or numbers (in this case numbers) and a class erase to modify it's variable and erase numbers (for example by subtracting).

here's some code:

Paper.h

@interface Paper : NSObject

@property (nonatomic) int numbers;

@end

Paper.m

#import "Paper.h"

@implementation Paper

@synthesize numbers;

@end

and Eraser.h

@interface Eraser : NSObject

-(void)eraseMethod;

@end

Eraser.m

#import "Eraser.h"

@implementation Eraser

-(void)eraseMethod {
//here I want to make a subtraction of the ivar declared in Paper
}

@end

and finally I'm trying to call it on the UIViewController like so

[paperObject eraseMethod];

I tried by declaring @class in each files as I've read somewhere but this won't help in any way... I hope this is clear as question

  • Is not very clear what you're trying to achieve here. You send an `eraserMethod` message to your paper object, but obviously your object doesn't respond to that message (not even the eraser object doesn't respond to that exact message). If you look for a way to forward the message to paper's supposedly internal eraser object, then I must say that in this specific case (eraser/paper) it's a very bad design. – Rad'Val Feb 17 '13 at 19:55
  • if the `Paper` is not derived from the `Eraser`, how do you expect you can call the `Eraser` class's `–eraseMethod` method via the instance of the `Paper` class, which is a totally independent of it? do you have the basics of the `OOP`? – holex Feb 17 '13 at 20:10
  • @holex I perfectly know that this is not the way it works, did you read the question? I'm asking how to do it properly. –  Feb 17 '13 at 20:12
  • @Loudequal, yes, I've read it and it looks it is a little mystery what you'd like to achieve here exactly... maybe the solution is creating a `delegate` class but the goal is not quite clear to say bravely it is the solution for you. – holex Feb 17 '13 at 20:16

1 Answers1

-1

Ivars are just storage slots and they are private to the class when without specifying access conditions.You can use property for the purpose of accessing from another class. more info here

in order to achive what you speak of,make the method a class method see here

#import "Eraser.h"
@implementation Eraser

+(void)eraseMethod {
//here I want to make a subtraction of the ivar declared in Paper
}

@end

thus you can achieve the method in your VC.

Community
  • 1
  • 1
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • "and they are private to the class" Not true. Instance variables can have the access modifiers `@public`, `@protected` or `@private`. The default for instance variables declared in the interface is `@protected`, and for ones declared in the implementation is `@private`. But the user can explicitly set the access modifier to something else. – newacct Feb 17 '13 at 21:13
  • solved, I did using a class method, thanks (thank you even for the links) –  Feb 17 '13 at 23:05