I am trying to create an application were 2 classes share a variable. Just to keep the code looking a little bit cleaner I created a 3rd class. This “third class” sole job is to house this variable.
In class 3 I put a “get” and “set” method.
SharedURL.H (Class 3)
@interface SharedURL : NSObject {
NSString *theURL;
}
-(NSString *)getTheURL;
-(void)setTheURL:(NSString *)blah;
@property (readwrite, copy) NSString *theURL;
@end
Implementation:
#import "SharedURL.h"
@implementation SharedURL
@synthesize theURL;
-(NSString *)getTheURL;
{
return theURL;
}
-(void)setTheURL:(NSString *)blah;
{
theURL=blah;
}
@end
In classes 1 and 2:
I Import the class header
I set up the instance variable like so
SharedURL *XMLURL;
I define the property like so
@property (readwrite, assign) SharedURL *XMLURL;
Then in the implementation I set the set method like this
[XMLURL setTheURL:@"http://localhost:8888/xml/MyXMLFile.xml"];
However whenever I implement the fallowing code the getter method returns nil.
NSLog(@” the url is %@”, [XMLURL getTheURL]);
How can I get this to actually save the variable that I imput and then return it. I’m looking at some sample code and i cannot find my error it looks to me like I am doing it perfectly fine I think I am overlooking something stupid.
If I understand this right you are calling class 3 from either class 1 or 2 (lets say 1) and set the URL then your go to class 2 and and only ask for the URL, right?
I think your problem is that you are calling something that is independent for each object. I think you can fix this by instead of saying
-(NSString *)getTheURLand-(void)setTheURLyou need to change it to+(NSString *)getTheURLand+(void)setTheURL(in both the .h and .m files) making it not variable dependent.