Skip navigation.
 
mlRe: Pb implementing shared instance
FROM : Robbie Haertel
DATE : Sat Nov 20 17:04:00 2004

First of all, get rid of your "static" statement that is outside of
the class because you aren't using it in the code.  Second, since
sharedInstance is a class method, you'll need to either do [[aData
class] sharedInstance], or the easier [classA sharedInstance].
Lastly, I'm not sure your implementation of sharedInstance will do
what you want.  It creates a brand new instance of classA. So, if an
instance classA already exists and you want classB to get that
instance, it won't work because a new one will be created instead.
But if you don't have any instances of classA yet, and you want classB
to create a new instance (and then any other calls to the same class
method will used that same instance), then your code is fine.

Robbie


On Sat, 20 Nov 2004 15:18:37 +0100, Kubernan @ 10191 Technologies
<<email_removed>> wrote:
> Hello,
>
> After reading the archive mailing list (and some web pages) i'm still
> not able
> to implement a shared instance (!).
> I have a compilation warning and i don't understand why :
>
>    compiling classB.m
>        " classA may not respond to '-sharedInstance' "
>
> What i need is to establish a reference of class A in class B.
> If you have an idea ...
>
> Here's my code :
>
>        file classA.h
> @interface classA : NSObject {
>
>        // other...
> }
> +(classA *)sharedInstance;
>        // other ..
> @end
>
>        file classA.m
> #import classA.h
>
> @implementation classA
> static classA *_sharedInstance = nil;
> +(classA *)sharedInstance
> {
>        static classA *_sharedInstance = nil;
>        if (_sharedInstance == nil)
>                _sharedInstance = [[self alloc) init];
>        return _sharedInstance;
> }
> @end
>
> I use the shareInstance here :
>        file classB.h
> #import classA.h
> @interface classB : NSObject {
>        classA *classARef;
>        // other...
> }
> -(void)setAReferanceToClassA:(classA *)aData;
>
>        file classB.m
> #import "classB.h"
> @implementation classB
> -(void)setAReferanceToClassA:(classA *)aData
> {
>        classARef = [aData sharedInstance]; // <-- THE COMPIL WARNING (!?)
> }
> @end
>
> Thanks a lot,
> K.
>
>  _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list      (<email_removed>)
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/robbie.<email_removed>
>
> This email sent to robbie.<email_removed>
>
>

Related mailsAuthorDate
mlPb implementing shared instance Kubernan @ 10191 T… Nov 20, 15:18
mlRe: Pb implementing shared instance Robbie Haertel Nov 20, 17:04