RE: Is Apple's singleton sample code correct?
RE: Is Apple's singleton sample code correct?
- Subject: RE: Is Apple's singleton sample code correct?
- From: Jeff Laing <email@hidden>
- Date: Fri, 2 Dec 2005 16:22:13 +1100
>
> Alright, I do know that it might really be an NSMutableString under
>
> the covers so its a poor example.
>
>
Actually this is a very good example. In this case, you want to work
>
with this string and make sure that nothing you don't have control
>
over will change it, and so you want to copy it. You shouldn't be
>
concerned whether it's actually an NSString, or an NSMutableString
>
disguised as a string.
Agreed, which makes it *not* a good example when discussing singletons.
>
By "functionally independent" I take it to mean that if you change
>
any of the values of the object and its variables, then this only
>
changes the object that you have as a copy. ... in the case of a
>
singleton, the object variables of the copy are the same as the
>
object variables of the original, which I think breaks the contract
>
of the NSCopying protocol. Please correct me if I am wrong here.
I don't think you are wrong, according to the letter of the contract. But I
suspect this is one of those cases where no-one cares to uphold the exact
letter of the law.
Shaun and Malcolm have already pointed out two scenarios where copying of a
singleton might happen, even though the copier didn't want a "writable" copy
or even an "functionally independent" one. But because they were the
general case (dictionary keys, accessors), they were *required* to take the
copy
In both cases, I think, the probability of the scenarios actually happening
(one of my accessors returning the [NSDocumentController sharedInstance], or
using it as a key in a dictionary) are pretty small. But I concede that
they are not completely irrational, so it does make sense for your singleton
to munge copyWithZone:
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden