Reputation: 317
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]);
}
Upvotes: 1
Views: 742
Reputation: 6389
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
.
Upvotes: 0
Reputation: 18775
You should probably use self.Data = @"something"
instead of self.Data = "something"
Upvotes: 1