开发者

How to tell if an object has already been allocated, so it isn't allocated multiple times?

Please be nice.. I am a noob with this stuff. I want to allocate and use an object through a method that I call many times, and instead of making it a global variable and allocating it in my initWithFrame method I want to alloc 开发者_如何学编程it in just the one method, and make it a local variable that I allocate then and use then.


You need to step back and think about how many different things need to access said object.

If it is everyone, then you should likely implement the shared instance pattern. Something like:

+ (MyObject *) mySharedObjectThatEveryoneUses
{
    static MyObject *sharedInstance;
    if (!sharedInstance) {
        sharedInstance = [MyObject alloc];
        sharedInstance = [sharedInstance init];
    }
    return sharedInstance;
}

Note that the above does not account for threading on initialization. I.e. if multiple threads simultaneously call the method for the first time, you might end up with multiple instances. Also note that the splitting of +alloc and -init is a pattern that is unique to this situation. Doing so avoids the situation where the -init method causes + mySharedObjectThatEveryoneUses to be invoked, creating a second instance before the assignment can happen.

Now, if you are talking about access within an object graph; access to the single instance within some subset of objects in your application, then you will need to have an instance variable somewhere that all of the objects can get to.

For example, say you are implementing a game where there is a Player object and that Player object has a bunch of Piece instances. When a Piece is created, you would typically have a property that refers to the owning player and that would be your single point of reference.

If you need something more transient, you should likely look to what should be your controller layer. I.e. there has to be some object somewhere that can act as a coordinator for the set of objects that need access to that single instance.


If I understood your question correctly, you're looking for a static variable. A static variable keeps its value like a global variable, but it "lives inside" a function like a local variable. Your function could look something like this:

+ (MyObject *)getSingletonObject {
   static MyObject *obj = [[MyObject alloc] init];
   return obj;
}

obj would be created the first time you called this method, but calling the method again would return the same object it had already created.


What you want is called a Singleton. You can read up on Apple's recommended way to create singletons here:

http://developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW32

Look for "Creating a Singleton Instance" in the "Cocoa Objects" section. There are a few more methods it's good to define beyond just a class level initializer.


I'm not entirely sure I understand your question, but you can't really allocate the same object many times.

Each time you want a new instance, you ask the Class object to allocate a new instance for you (yes, classes being objects themselves is mind blowing). So this code:

NSString* s;
s = [[NSString alloc] init];
s = [[NSString alloc] init];

Here you are creating two instances of NSString. This code is bad because we have alloc'd the first instance and have not released, and assigned its only reference (s) to a new instance. This would cause a memory leak.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜