views:

64

answers:

2

I set a value in one class and then I want to retrieve that value without creating an object for it.

When I use classname.variableName from another class(which I declare the variableName as property and synthesize it) I get an unknown class method error.

How can I just set an NSString in one class and just reference it from another. I dot want to create an object.

+3  A: 

Perhaps you are setting it up incorrectly. Here is how you could set it up:

@interface ClassThatAlreadyExists : NSObject {
    // Other ivars
    NSString *variableName;
}

// Other @property's
@property (nonatomic, copy) NSString *variableName;

@end

@implementation ClassThatAlreadyExists

// other @synthesize's
@synthesize variableName;

// rest of implementation

@end
rickharrison
I do exactly that, but cannot access the string in another class. e.g. SecondClassThatAlreadyExists. Let's say the string is set in ClassThatAlreadyExists and I want to use that new string value in the second class, how is that done?
alJaree
Import the ClassThatAlreadyExists.h. Then create an instance of it and reference the ivar using: `instanceOfClassThatAlreadyExists.variableName`.
Rengers
That results in null. I want to set the value in one class and just reference that value.
alJaree
Yes, it results in null if the variable is not yet set. You need to set it first using `instanceOfClassThatAlreadyExists.variableName = @"proString". Then retrieve it using: `instanceOfClassThatAlreadyExists.variableName`;
Rengers
+2  A: 

make sure you are using #import "ClassThatAlreadyExists.h" in your SecondClassThatAlreadyExists. Also, following the above example, to get the string variable you would use this in SecondClassThatAlreadyExists:

//assuming you haven't declared and initialized the object yet.
ClassThatAlreadyExists *objectThatAlreadyExists = [[ClassThatAlreadyExists alloc] init];
objectThatAlreadyExists.variableName = @"hey im the sample string that is being set";
Jesse Naugher
No, I know how to do that. The thing I want to do is set the variable name value in one class and then use it in another class. I have tried creating objects in one class, but then that object is undeclared in the other class. I dont know how to solve this
alJaree
so there is an object that has an ivar. you create an instance of this ObjectOne in ClassOne, then want that same object in ClassTwo? ClassTwo would have to be able to reference ClassOne somehow, either through parent-child relationship (under it in a UINavigationController for instance), or have an instance of ClassOne (which has an instance of ObjectOne) in ClassTwo. Another option is to have the variable you want in a singleton class and reference it that way.
Jesse Naugher