AppDelegate不使用实例变量

时间:2014-05-13 12:49:57

标签: objective-c macos cocoa appdelegate

我有两个类文件,我正在尝试使用彼此的变量。似乎我必须遗漏一些东西或者没有初始化正确的东西:

.h - AppDelegate

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate> {

    NSString *extString;

}

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

- (void)extStringSetter;

@end

的.m

#import "AppDelegate.h"
#import "MoarDelegates.h"

@implementation AppDelegate

@synthesize extString;

NSString *someString;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application

    [self extStringSetter];

}

- (void)extStringSetter {

    MoarDelegates *dualString = [[MoarDelegates alloc] init];
    dualString.someString = @"world!";

    NSLog(@"%@",someString);
}

@end

.h - MoarDelgates

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>

@class AppDelegate;

@interface MoarDelegates : NSObject {

    NSString *someString;
    AppDelegate *appdelegate;
}

@property (retain, nonatomic) AppDelegate *appdelegate;
@property (retain, nonatomic) NSString *someString;


- (void)someStringSetter;

@end

的.m

#import "MoarDelegates.h"
#import "AppDelegate.h"

@implementation MoarDelegates

@synthesize appdelegate;
@synthesize someString;

- (id)init {
    if ((self = [super init]) != nil) {
        appdelegate = [[AppDelegate alloc] init];
    }

    [self someStringSetter];

    return self;
}

- (void)someStringSetter {

    AppDelegate *dualString = [[AppDelegate alloc] init];
    dualString.extString = @"world!";

    NSLog(@"%@",someString);
}

@end

输出中:

(null)
(null)

是肯定的。我是obj-c的新手,所以请不要在火上耙我。我来自一长串书虫 - 我来这里学习。

0 个答案:

没有答案