2

That's what I have :

Class 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]];
}

Class 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]];
}

And here's the issue :

AT SOME LATER STAGE, in ppApplication, I'm trying to have access to core.

core is there.

But, when I'm trying to access any of core's elements (e.g. [core someObject]), everything is turning up NULL (I've checked it in the Debugger)...

What am I doing wrong??

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • Are you sure this `[self setSomeObject:[[ppSomeObject alloc] init]];` is being called? If so, what does the code look like where you're getting nulls? – Phillip Mills Apr 05 '12 at 12:12
  • 1
    And, as an aside, you shouldn't need the extra retain on `[ppCore alloc]...` since alloc already does it. – Phillip Mills Apr 05 '12 at 12:15
  • @PhillipMills Yep, the `init` function IS called. Actually the "function getting the nulls" responds to an AppleScript event (the app is AppleScript-able), so ALL objects are set (I'm already accessing them from other classes, but not from that particular one; and I have no idea why...) – Dr.Kameleon Apr 05 '12 at 12:16
  • 1) Use ARC. 2) Generally use `retain` or under ARC `strong` for objects. – zaph Apr 05 '12 at 12:17
  • 1
    Why the hell would - (void)applicationDidFinishLaunching:(NSNotification *)notification be called on the ppCore object? – tux91 Apr 05 '12 at 12:19
  • @tux91 Because (I forgot to mention that) ppCore is acting as my Application Delegate... ;-) (JUST re-edited the initial post) – Dr.Kameleon Apr 05 '12 at 12:21
  • Did you try putting an NSLog statement in applicationDidFinishLaunching to see if it gets called at all? Also, try moving the two setting lines in the init method of the delegate. – tux91 Apr 05 '12 at 12:25
  • @tux91 The method IS called, everything from then on is working fine (and I'm dealing with a HUGE code base). The only thing that is NOT working is getting access to ppCore's contents from ppApplication... :-S – Dr.Kameleon Apr 05 '12 at 12:27
  • 1
    You also don't need to set `core` in your Application class, since if ppCore is your app's delegate, the Application already has access to it through the `delegate` property – tux91 Apr 05 '12 at 12:27
  • 1
    I suggest you remove the whole `core` thing since you can access your delegate through `[[NSApplication sharedApplication] delegate]` and move the setting of `someObject` and `anotherObject` to the delegate's init method. Because the whole code just seems unnecessarily complex and maybe you've screwed with the basic NSApplication architecture too much so something is wrong – tux91 Apr 05 '12 at 12:32
  • Also, try creating `ppSomeObject` and `ppAnotherObject` on their own and see if they are not nil, because maybe there's something wrong with their initialization. – tux91 Apr 05 '12 at 12:34
  • @tux91 I dropped them in the `init` function, and they ARE accessible. Now, MOST OF my objects CAN be in the init function, except for 2 which CANNOT... what about them? (We're really close) – Dr.Kameleon Apr 05 '12 at 12:41
  • @Dr.Kameleon Why is that they can be in appDidFinishLaunching but not in the init function? – tux91 Apr 05 '12 at 12:45
  • @tux91 Accessing `core` via `(ppCore*)[[NSApplication sharedApplication] delegate]`, plus moving a few objects' initialization in the `init` function DID the trick. So,... in a few words, please post it as an "Answer" and you'll get all the credit you deserve. Thanks a lot, really! ;-) – Dr.Kameleon Apr 05 '12 at 13:14

4 Answers4

2

Have you tried declaring your objects like this:

@property (nonatomic, retain) ppCore* core;

@property (nonatomic, retain) ppSomeObject* someObject;
@property (nonatomic, retain) ppAnotherObject* anotherObject;
Antonio MG
  • 20,382
  • 3
  • 43
  • 62
  • 1
    +1 -- the `assign` property only creates a setter, not a getter. More details at http://stackoverflow.com/questions/918698/why-are-objective-c-delegates-usually-given-the-property-assign-instead-of-retai – Michael Dautermann Apr 05 '12 at 12:12
  • 1
    Whoa! 'assign' only describes storage; there's still a getter if you synthesize an 'assign' property. For a simple assigned and synthesized property, something like `self.whoAmI = @"ViewController"; NSLog(@"%@", self.whoAmI);' works fine. – Phillip Mills Apr 05 '12 at 12:24
1

Antonio is right, bit you need to manage memory as well,

    #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];
    }

This might help.

Nilesh
  • 1,493
  • 18
  • 29
1

I suggest you remove the whole core thing since you can access your delegate through [[NSApplication sharedApplication] delegate] and move the setting of someObject and anotherObject to the delegate's init method.

tux91
  • 1,674
  • 10
  • 16
0

Why do you believe - (void)applicationDidFinishLaunching: on your ppCore object is ever getting called? It seems to me you'll have to explicitly invoke it from somewhere.

JeremyP
  • 84,577
  • 15
  • 123
  • 161