Re: singleton design pattern
Re: singleton design pattern
- Subject: Re: singleton design pattern
- From: Abhinay Kartik Reddyreddy <email@hidden>
- Date: Wed, 19 May 2010 17:35:40 -0400
everytime you ask for a uniqueInstance it looks like it retrieves a new instance not the existing instance... since you set the uniqueinstance to nil inside that function, your function will discard the previous instance if any and then create a new instance. Singleton is supposed to return existing instance if any and if none create one.
Also i guess the scope of your static is local.
correct me if i am wrong.
Thanks,
Abhinay.
On May 19, 2010, at 12:30 PM, Alejandro Marcos Aragón wrote:
> Hi Abhinay,
>
> Why declaring the static variable outside the function? If I have it inside, I make sure that it's initialized before it's being used.
>
> aa
>
>
> On May 18, 2010, at 5:58 PM, Abhinay Kartik Reddyreddy wrote:
>
>>
>> On May 18, 2010, at 6:42 PM, Mike Abdullah wrote:
>>
>>>
>>> On 18 May 2010, at 23:28, Alejandro Marcos Aragón wrote:
>>>
>>>> Hi all,
>>>>
>>>> I've been staring at this piece of code now to try to find out what's wrong with it and I can't think of anything at this point. I'm trying to have a single instance of an NSMutableDictionary inside a class:
>>>>
>>>>
>>>>
>>>> // in .h file
>>>> @interface ClassA : NSObject {
>>>>
>>>> }
>>>>
>>>> + (NSMutableDictionary*) uniqueInstance;
>>>>
>>>> @end
>>>>
>>>> // in .m file
>>>> @implementation ClassA
>> static NSMutableDictionary* uniqueInstance = nil;
>> // the static variable has to be initialized before you enter the uniqueInstance method.
>>>>
>>>>
>>>> + (NSMutableDictionary*) uniqueInstance {
>>>>
>>>> //static NSMutableDictionary* uniqueInstance = nil;
>>> you don't actually need '= nil' unless you prefer it.
>>>
>>>>
>>>> if (uniqueInstance == nil) {
>>>>
>>>> // directory to save files
>>>> NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
>>>> NSString *dir = [paths objectAtIndex:0];
>>>>
>>>> NSFileManager *fileManager = [NSFileManager defaultManager];
>>> uh, this never gets used.
>>>
>>>>
>>>> NSString *customFilePath = [[NSString alloc] initWithString:
>>>> [dir stringByAppendingPathComponent:@"dict.plist"]];
>>> nonsense, [dir stringByAppendingPathComponent:@"dict.plist"] by itself is what you want.
>>>>
>>>>
>>>> uniqueInstance = [[NSMutableDictionary alloc] initWithContentsOfFile:customFilePath];
>>>> }
>>>>
>>>> return uniqueInstance;
>>>> }
>>>>
>>>>
>>>> I can't use this code because there is a EXC_BAD_ACCESS according to the debugger.
>>> What's the backtrace? Very hard for us to debug without. Your singleton method looks fine provided it's only ever accessed from a single thread at a time. From my comments above, sure you've posted all the relevant code?
>>>>
>>>> I tried moving the definition of the uniqueInstance outside the function and that didn't help. This code seems alright to me, but I can't find why it doesn't work. Can someone point out the problem?
>>>>
>>>> aa
>>>>
>>>> _______________________________________________
>>>>
>>>> Cocoa-dev mailing list (email@hidden)
>>>>
>>>> Please do not post admin requests or moderator comments to the list.
>>>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>>>
>>>> Help/Unsubscribe/Update your Subscription:
>>>>
>>>> This email sent to email@hidden
>>>
>>> _______________________________________________
>>>
>>> Cocoa-dev mailing list (email@hidden)
>>>
>>> Please do not post admin requests or moderator comments to the list.
>>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>>
>>> Help/Unsubscribe/Update your Subscription:
>>>
>>> This email sent to email@hidden
>>
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden