可可:已分配但无法正常访问的对象?

时间:2012-04-05 12:04:42

标签: objective-c cocoa object memory-management alloc

这就是我所拥有的:

A类:

#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(我在调试器中检查过它)... < / p>

我做错了什么?

4 个答案:

答案 0 :(得分:2)

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

@property (nonatomic, retain) ppCore* core;

@property (nonatomic, retain) ppSomeObject* someObject;
@property (nonatomic, retain) ppAnotherObject* anotherObject;

答案 1 :(得分: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];
    }

这可能会有所帮助。

答案 2 :(得分:1)

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

答案 3 :(得分:0)

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

相关问题