0

I have a variable (var1) that gets set in my AppDelegate. I have another class MyClass where I'd like to retrieve the variable from AppDelegate. I can set the variable (var2) defined in MyClass just fine:

AppDelegate:

- (void)setVariable {

    var1 = @"TEST";

    MyClass *setVar = [[MyClass alloc] init];
    setVar.var2 = var1;
    NSLog(@"var2: %@",setVar.var2);  // Outputs TEST
}

When I try to get the variable in MyClass it's Null:

MyClass

- (void)getVariable {

     AppDelegate *getVar = [[AppDelegate alloc] init];
     var2 = getVar.var1;
     NSLog(@"var2: %@",var2);  // Outputs NULL
}

It works if I also include [getVar setVariable]; but that's not exactly what I want to do, as it would be setting the variable as a static value. I'm trying to get the variable as it was set previously in AppDelegate.

  • 1
    Creating setVar in the app delegate creates a new instance of MyClass that never is on screen and will be deallocated as soon as the setVariable method goes out of scope. Creating a new instance of AppDelegate is just wrong -- you should never create a new instance of the app delegate. You really need to get yourself a good book on Objective-C programming and try to understand objects. – rdelmar Feb 06 '15 at 19:52
  • @rdelmar, Which book would you recommend? I often see people saying to get a good book, but there's so many. A specific title would be nice, thanks. – Jack O'Leery Feb 07 '15 at 00:32
  • I'm not sure. The ones I used, I wouldn't recommend. I see a lot of people recommending the ones from Big Nerd Ranch. I also liked the videos from the Stanford online course, CS193P, which is specifically for iOS programming. – rdelmar Feb 07 '15 at 00:34

3 Answers3

0

If you create a new instance of your app delegate, you won't be able to retrieve updated property values; you'll only ever see the default.

Instead, you probably want to use the app delegate that you've assigned to your application:

[(AppDelegate *)[[UIApplication sharedApplication] delegate] var1];

Side note: you appear to have typos in your provided source code. AppDelegate *getVar * [AppDelegate alloc] init]; is missing an = and a [.

Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51
  • One question about this, if I had another class instead of `AppDelegate` I wanted to apply the same method to how could I? When I tried changing the `[(AppDelegate *)]` to some other class it gives me `unrecognized selector`. – Jack O'Leery Feb 06 '15 at 20:56
  • That really depends on how you've declared your other class instance. You might add a static method to fetch the value on the class itself. You might have a singleton that retrieves the shared instance of the class. You might have many instances of the class and want to retrieve a specific one by a lookup method. You might pass an instance of the class to your view controller. Your question has many possible answers and nobody can guess at your code architecture. – Ian MacDonald Feb 06 '15 at 21:03
  • The solution you provided works great if it's specific to AppDelegate, so if there is something similar that works with another class (probably an NSView) that's what I'm after. Nothing fancy really - just grabbing the variable value as you've shown. – Jack O'Leery Feb 06 '15 at 21:11
  • As I said, that will depend on the implementation of your classes. There are many ways to do such a thing. For `AppDelegate`, it just happens to be the case that you have access to the instance from anywhere within your application. If you have another class that you want to use to share data instead, you need to figure out how to share that instance to where you need to retrieve the data. – Ian MacDonald Feb 06 '15 at 21:13
  • Likely it would be an NSView, or NSObject. – Jack O'Leery Feb 06 '15 at 21:14
  • If you're in iOS and not OSX, I would urge you to use `UIView` instead of `NSView`. You'll need to make an instance of that object available to both sides of your command chain. The `set` needs to have a pointer to the object and the `get` needs to have a pointer to the same object. You can do this by passing the pointer between the two classes, creating a singleton, using a static method on a class, using a delegate pattern, etc. There are many ways to do this, but it is far beyond the scope of the question you originally asked. – Ian MacDonald Feb 06 '15 at 21:20
  • I didn't know about singletons until you mentioned it here, so thank you. Just to let you know I was able to find a tutorial and example that did exactly what I was looking to do: http://www.galloway.me.uk/tutorials/singleton-classes/ – Jack O'Leery Feb 06 '15 at 23:40
0

When you do AppDelegate *getVar = [[AppDelegate alloc] init];, you creating a new instance of the AppDelegate class. This, by default, will not have a value assigned to var1. That is why after you call [getVar setVariable]; once, it outputs the correct value.

Assuming you have already called setVariable on the instance of AppDelegate that is delegated to your application, you can start off by retrieving that instance of AppDelegate: (AppDelegate *)[[UIApplication sharedApplication] delegate];.

So your code would look like:

- (void)getVariable {

     AppDelegate *getVar = (AppDelegate *)[[UIApplication sharedApplication] delegate];
     var2 = getVar.var1;
     NSLog(@"var2: %@",var2);
}
golddove
  • 1,165
  • 2
  • 14
  • 32
  • One question about this, if I had another class instead of `AppDelegate` I wanted to apply the same method to how could I? When I tried changing the `[(AppDelegate *)]` to some other class it gives me `unrecognized selector`. – Jack O'Leery Feb 06 '15 at 21:02
  • The AppDelegate is a singleton-like class that is shared throughout your application, so thats why you can access it through `[[UIApplication sharedApplication] delegate]`. Other classes cannot be accessed the same way. What exactly are you trying to do? If you created an object somewhere with: `MyClass myObj = [[MyClass alloc] init];`, you can later access any of its properties with `myObj.myProperty`. I suggest you start a new question for this with more explanation of what you are trying to achieve. – golddove Feb 06 '15 at 22:02
  • It works if I set the value for the other class, like `myObj.myNewProperty = myProperty`, in the class setting it. but then when I try and access myNewProperty on the class it's supposed to be set on it's NULL. – Jack O'Leery Feb 06 '15 at 22:40
  • There are two ways to do this. You can use a static variable (http://stackoverflow.com/questions/695980/how-do-i-declare-class-level-properties-in-objective-c). Or, you can make sure you are setting and getting from the same instance of the class. I believe you are creating a new instance, so that is why it is null. – golddove Feb 06 '15 at 22:46
  • Thanks very much, If i could upvote I would - everything I was able to take from the answers here helped. – Jack O'Leery Feb 06 '15 at 23:38
0
You are again creating another instance of AppDelegate which is already there in memory.

You need to access the same same AppDelegate object what it was created at beginning of your app.

You can access AppDelegate object and its property using following code.


id appDelegate = [[UIApplication sharedApplication] delegate];

 NSLog(@"[appDelegate valueForKey:var1]=%@",[appDelegate valueForKey:@"var1"]);
Shashi3456643
  • 2,021
  • 17
  • 21