2

这就是我所拥有的:

甲类:

#import "ppCore.h"

@interface ppApplication : NSApplication {
    ppCore* core;
}

@property (assign) ppCore* core;

@end


@implementation ppApplication

@synthesize core;

- (void)awakeFromNib
{
    [self setCore:[[[ppCore alloc] init] retain]];
}

B类:

#import "someObject.h"
#import "anotherObject.h"

@interface ppCore : NSObject<NSApplicationDelegate>  {
    ppSomeObject* someObject;
    ppAnotherObject* anotherObject;
}

@property (assign) ppSomeObject* someObject;
@property (assign) ppAnotherObject* anotherObject;

@end


@implementation ppCore

@synthesize someObject, anotherObject;

- (void)applicationDidFinishLaunching:(NSNotification *)notification 
{
     [self setSomeObject:[[ppSomeObject alloc] init]];
     [self setAnotherObject:[[ppAnotherObject alloc] init]];
}

这是问题所在:

在稍后的某个阶段,在 中ppApplication,我试图访问core.

core有没有。

但是,当我试图访问任何core' 元素(例如[core someObject])时,一切都变成了 NULL (我已经在调试器中检查了它)......

我究竟做错了什么??

4

4 回答 4

2

您是否尝试过像这样声明您的对象:

@property (nonatomic, retain) ppCore* core;

@property (nonatomic, retain) ppSomeObject* someObject;
@property (nonatomic, retain) ppAnotherObject* anotherObject;
于 2012-04-05T12:08:32.380 回答
1

安东尼奥是对的,你还需要管理内存,

    #import "ppCore.h"

    @interface ppApplication : NSApplication {
        ppCore* core;
    }

    @property (nonatomic, retain) ppCore* core;

    @end


    @implementation ppApplication

    @synthesize core;

    - (void)awakeFromNib
    {
        ppCore* tempCore = [[ppCore alloc] init];   
        [self setCore: tempCore];
        [tempCore release];
    }

这可能会有所帮助。

于 2012-04-05T12:17:24.670 回答
1

我建议您删除整个core内容,因为您可以通过访问您的委托[[NSApplication sharedApplication] delegate]并将 someObject 和 anotherObject 的设置移动到委托的 init 方法。

于 2012-04-05T13:22:49.523 回答
0

为什么你相信- (void)applicationDidFinishLaunching:你的 ppCore 对象会被调用?在我看来,您必须从某个地方显式调用它。

于 2012-04-05T13:19:29.040 回答