开发者

Obj-C: Passing pointers to initialized classes in other classes

开发者 https://www.devze.com 2023-01-02 10:19 出处:网络
I initialized a class in my singleton called DataModel.Now, from my UIViewController, when I click a button, I have a method that is trying to access that class so that I may add an object to one of i

I initialized a class in my singleton called DataModel. Now, from my UIViewController, when I click a button, I have a method that is trying to access that class so that I may add an object to one of its dictionaries. My get/set method passes back the pointer to the class from my singleton, but when I am back in my UIViewController, the class passed back doesn't respond to methods. It's like it's just not there. I think it has something to do with the difference in passing pointers around classes or something. I even tried using the copy method to throw a copy back, but no luck.

UIViewController:

ApplicationSingleton *applicationSingleton = [[ApplicationSingleton alloc] init];

DataModel *dataModel = [applicationSingleton getDataModel];

[dataModel retrieveDataCategory:dataCategory];

Singleton:

ApplicationSingleton *m_instance;
DataModel *m_dataModel;

- (id) init {
    NSLog(@"ApplicationSingleton.m initialized.");
    self = [super init];
    if(self != nil) {
        if(m_instance != nil) {
            return m_instance;
            }
        NSLog(@"Initializing the application singleton.");
        m_instance = self;
        m_dataModel = [[DataModel alloc] init];   
        }
    NSLog(@"ApplicationSingleton init method returning.");
    return m_instance;
    }

-(DataModel *)getDataModel {
    DataModel *dataModel_COPY = [m_dataModel copy];
    return da开发者_开发问答taModel_COPY;
    }

For the getDataModel method, I also tried this:

-(DataModel *)getDataModel {
    return m_dataModel;
    }

In my DataModel retrieveDataCategory method, I couldn't get anything to work. I even just tried putting a NSLog in there but it never would come onto the console.

Any ideas?


Most likely you are sending messages that get ignored, e.g. they're being sent to objects which don't exist/aren't the one you're looking for, and for some reason aren't crashing. This occurs in the case of messaging nil, or possibly other illegitimate values. Although you seem to expect that the m_ variables will be initialized to 0, this is not good form, and furthermore you are not following a very typical objc pattern for your singletons -- m_dataModel should be an ivar of m_instance, and m_instance should probably be declared static, as you probably don't want it accessed from other files directly. In addition, the most likely source of your bug is somehow the -init method, which should never be called on a singleton -- instead do something like this:

+ (ApplicationSingleton *)sharedInstance {
     static ApplicationSingleton *instance = nil;
     if(!instance) {
          instance = [[self alloc] init]; //or whatever custom initializer you would like, furthermore some people just put the initialization code here and leave -init empty
     }
     return instance;
}

the code you have now leaks because you allocate an object (self) and don't release it before returning a potentially different instance (the shared one if one already exists), such that the newly allocated one is typically lost.

0

精彩评论

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

关注公众号