Zoltan Varadi
Zoltan Varadi

Reputation: 2488

iOS initializing a static object's member fails

I have the following code that fails when i run it. my .h file:

 @interface OutlineManager : NSObject
    {
            NSMutableArray* mOutlines;
    }

    @property(nonatomic,strong)NSMutableArray* Outlines;


    +(void)initialize;

    @end

    static OutlineManager* outlnManager;

in the .m file:

#import "OutlineManager.h"

@implementation OutlineManager

@synthesize Outlines = mOutlines;

+(void)initialize
{
    outlnManager = [[[OutlineManager superclass]alloc]init];
    if(outlnManager)
    {
        outlnManager.Outlines = [[NSMutableArray alloc]init]; //it crashes here
    }
    NSLog(@"OUTLINEMANAGER INITIALIZED");
}

@end

when i run (void)initialize the application crashes in the if section where i put the comment, but i don"t know why. Can't I init an array of a static object like this?

I guess it's something very basic, but i'm pretty new at Obj C /iOS , so please don't hold this against me.

Thank you very much for your help!

Sincerely, Zoli

Upvotes: 0

Views: 455

Answers (2)

Schulze Thomas
Schulze Thomas

Reputation: 465

OutlineManager.h:

replace

static OutlineManager* outlnManager;

with:

OBJC_EXPORT OutlineManager* outlnManager;

and add in OutlineManager.m:

OutlineManager* outlnManager = nil;
+(void)initialize { // you should rename it to an other name.. initialize will be called twice
    outlnManager = [[OutlineManager alloc]init];
    if(outlnManager) {
        outlnManager.Outlines = [[NSMutableArray alloc]init];
    }
    NSLog(@"OUTLINEMANAGER INITIALIZED");
}

Upvotes: 0

Schulze Thomas
Schulze Thomas

Reputation: 465

You wrote:

outlnManager = [[[OutlineManager superclass]alloc]init];

That superclass means you calling alloc on NSObject and not on OutlineManager. Just replace this line with this:

outlnManager = [[OutlineManager alloc]init];

Oh and you should declare your static variables in the .m file.

Upvotes: 1

Related Questions