1
votes

How to access variables of other class? This is how i implemented it.

@interface Class1 :NSObject {

NSString *Data; } @property (nonatomic, retain) NSString *Data;

@implementation Class1
@synthesize Data;
someMethod{
self.Data = @"something";
}

and in Class2 :

@implementation Class2
someMethodOfClass2{
Class1 *c=[[Class1 alloc]init];
[c someMethod];
NSString *str=c.Data;
}

I get c.Data as null in Class2. Am i doing anything wrong here?

-----------myClass1--------------

@interface APIManager : NSObject { NSString *Data; }

@property (nonatomic, retain) NSString *Data;

-(void)getData;

@end

@implementation APIManager

@synthesize Data;

-(void)getData{ self.Data=@"response"; }

--------myClass2-------------

@interface Search : NSObject {

}

-(void)searchForItems:(NSString *)query;

@end

@implementation Search

-(void)searchForItems:(NSString *)query { APIManager *apiManager=[[APIManager alloc]init]; [apiManager getData]; NSLog(@"%@",[apiManager Data]); }

2

2 Answers

1
votes

You should probably use self.Data = @"something" instead of self.Data = "something"

0
votes

In Objective-C you have to use @"something" instead of "something". Also aren't you missing the variable declaration? In your @interface you should do something like NSString *Data.