Cocoa Puffs
Cocoa Puffs

Reputation: 774

NSString works with one method but not the other

I have two different methods of trying to grab a variable from another class. The first one, which I would prefer using doesn't work - The second one, which I don't prefer does. Could someone please explain why?

AppDelegate.h

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate> {

    NSString *someString;
}

@property (assign) IBOutlet NSWindow *window;
@property (retain, nonatomic) NSString *someString;

- (void)manualSetVariable;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "GrabFromAppDelegate.h"

@implementation AppDelegate

@synthesize someString;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification  
{
    someString = @"The Variable";
    NSLog(@"In AppDelegate (1): %@",someString);

    GrabFromAppDelegate *getThis = [[GrabFromAppDelegate alloc] init];
    getThis.varSet = someString;
}

- (void)manualSetVariable {        // THIS METHOD WORKS (2)

    someString = @"The Variable";
    NSLog(@"In AppDelegate(2): %@",someString);
}

@end

GrabFromAppDelegate.h

 #import <Foundation/Foundation.h>

@interface GrabFromAppDelegate : NSObject {

    NSString *varSet;
    IBOutlet NSTextField *variable;
}

@property(retain, nonatomic) NSString *varSet;

- (IBAction)showVariable:(id)sender;
- (IBAction)manuallyGrabVariable:(id)sender;

@end

GrabFromAppDelegate.m

#import "GrabFromAppDelegate.h"
#import "AppDelegate.h"

@implementation GrabFromAppDelegate

@synthesize varSet;

- (IBAction)showVariable:(id)sender {

    if (varSet != NULL) {
        [variable setStringValue:[NSString stringWithString:varSet]];
        NSLog(@"Got String Using (1): %@",varSet);
    }
}

- (IBAction)manuallyGrabVariable:(id)sender {     // THIS METHOD WORKS (2)

    AppDelegate *getString = [[AppDelegate alloc] init];
    [getString manualSetVariable];

    if ([getString someString] != NULL) {
        [variable setStringValue:[NSString stringWithString:[getString someString]]];
        NSLog(@"Got String Using (2): %@",[getString someString]);
    }
}

@end

Upvotes: 0

Views: 65

Answers (2)

stevesliva
stevesliva

Reputation: 5665

This is horribly wrong:

AppDelegate *getString = [[AppDelegate alloc] init];

That allocates an AppDelegate instance, but it's not [[NSApplication sharedApplication] delegate], so the getString instance will never execute the applicationDidFinishLaunching method and your someString iVar will never get set. If you'd set someString within an init function, [[AppDelegate alloc] init] would, of course, call init. But since getString isn't attached to an NSApplication instance, there's nothing delegating the applicationDidFinishLaunching method to it.

If you want to get a pointer to the application delegate, you can do:

AppDelegate *getString = [[NSApplication sharedApplication] delegate];

or, for brevity's sake:

AppDelegate *getString = [NSApp delegate];

Upvotes: 1

KnightIn
KnightIn

Reputation: 18

In second method you are calling method from app delegate and value is set at that time in someString variable and you get response. However if you are setting value for a varibale in appDelegate using an instance the set value will passed to that particular instance of controller class not to all instances. Hence either you create a shared instance of that variable or call that particular instance to get the value somestring by showVariable method.

Upvotes: 0

Related Questions