开发者

Why does my class variable in my Application Delegate not get instantiated?

开发者 https://www.devze.com 2023-03-16 01:51 出处:网络
This issue has been bugging me for a while now and somehow I cannot find what I\'m doing wrong. I must say I am new to Objective-C and Xcode.

This issue has been bugging me for a while now and somehow I cannot find what I'm doing wrong. I must say I am new to Objective-C and Xcode.

So the issue is that I try to declar开发者_如何学JAVAe an instance variable (NSMutableArray) but for some reason the init function is NEVER reached. The variable is always NULL.

So I have a class named PropertyProvider which contains a NSMutableArray named "properties".

@interface PropertyProvider : NSObject {
    NSMutableArray *properties;
}

@property (nonatomic, retain) NSMutableArray *properties;

..

@end

I then instantiate this NSMutableArray in the init method of this PropertyProvider class as the following:

@implementation PropertyProvider

@synthesize properties;

- (id) init {
    self = [super init];

    NSLog(@"Instantiating PropertyProvider");

    if (self) {
        properties = [[NSMutableArray alloc] init];
    }

    return self;
}

.. more code ..

@end

In my Application delegate I try to instantiate the PropertyProvider as "prp":

@implementation MyAppDelegate

@synthesize prp = _prp;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [[_prp init] alloc];
    [self.window makeKeyAndVisible];
    return YES;
}

.. more code ..

- (void)dealloc
{
    [_prp release];
    [super dealloc];
}

But thus, for some reason it never reaches the init method of my PropertyProvider. Why o why?!

Thanks for your help!


The correct way of initializing _prp as an instance of PropertyProvider would be,

_prp = [[PropertyProvider alloc] init];

Since _prp is an instance variable, it is nil by default and hence messages to it don't do anything which is the reason why you're not getting any errors.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号