• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: readonly property which is a mutable array
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: readonly property which is a mutable array


  • Subject: Re: readonly property which is a mutable array
  • From: "email@hidden" <email@hidden>
  • Date: Fri, 19 Nov 2010 09:29:29 +0000

On 19 Nov 2010, at 02:51, Ken Thomases wrote:

>>
>
> You don't need to implement the getter.  The Cocoa docs are very clear that callers must respect the declared type of properties (i.e. return type of getters).  That is, if the getter is declared to return an immutable NSArray, then callers must not interrogate the returned object to determine if it's really mutable nor invoke mutation methods on it.  It may be, and often is (even in framework classes), that such a method returns an object that is, in fact, an NSMutableArray.  That's an irrelevant implementation detail.
>

I agree that the Cocoa frameworks often returns a mutable class in place of an immutable.
Wether its irrelevant or not is another matter.

If I receive an NSArray instance from a  method I can for example identify the index of a particular object within the array, say at idx, safe in the knowledge that my object is at the given index.

If however the object I receive is an NSMutableArray instance then the assumption is false as the array may get mutated beneath me.
That is unless it can be relied upon that the NSMutableArray instance won't get mutated.

From what I recall this behaviour as exhibited in the frameworks is to there, among other reasons, to overcome the necessity of returning copies.
This has been discussed here (and in the comments below):
http://www.cocoabuilder.com/archive/cocoa/224795-nsdictionary-mutability-test.html#224823

quoting from the above link:

As has been noted, the AppKit/Foundation
APIs are written such that a method declared as returning
(NSDictionary*) will not have said dictionary modified after the
fact.  Writing code that detects mutability and then mutates, if
possible, would violate this contract and will cause unpredictable,
potentially crashy, behavior.

IFIRC NSTextView -string returns an NSMutableString subclass - NSBigMutableString.
This does however get mutated as the NSTextView content changes.
This is noted in the docs.

Regards

Jonathan Mitchell

Developer
Mugginsoft LLP
http://www.mugginsoft.com


_______________________________________________

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

  • Follow-Ups:
    • Re: readonly property which is a mutable array
      • From: Andreas Grosam <email@hidden>
References: 
 >readonly property which is a mutable array (From: Remco Poelstra <email@hidden>)
 >Re: readonly property which is a mutable array (From: Keary Suska <email@hidden>)
 >Re: readonly property which is a mutable array (From: Ken Thomases <email@hidden>)

  • Prev by Date: [iPhone] Make UIWebView content empty before loading next page
  • Next by Date: Re: readonly property which is a mutable array
  • Previous by thread: Re: readonly property which is a mutable array
  • Next by thread: Re: readonly property which is a mutable array
  • Index(es):
    • Date
    • Thread