【发布时间】:2012-04-05 12:04:42
【问题描述】:
这就是我所拥有的:
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(我已在调试器中检查过)...
我做错了什么??
【问题讨论】:
-
您确定正在调用此
[self setSomeObject:[[ppSomeObject alloc] init]];吗?如果是这样,你得到空值的代码是什么样的? -
而且,顺便说一句,您不需要在
[ppCore alloc]...上额外保留,因为 alloc 已经这样做了。 -
为什么会在 ppCore 对象上调用 - (void)applicationDidFinishLaunching:(NSNotification *)notification?
-
您也不需要在 Application 类中设置
core,因为如果 ppCore 是您的应用程序的委托,则应用程序已经可以通过delegate属性访问它 -
我建议你删除整个
core东西,因为你可以通过[[NSApplication sharedApplication] delegate]访问你的委托,并将someObject和anotherObject的设置移动到委托的 init 方法。因为整个代码看起来过于复杂了,也许你已经把基本的 NSApplication 架构搞砸了,所以出了点问题
标签: objective-c cocoa object memory-management alloc