• 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: Limited-access, KVO-compliant mutable array?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Limited-access, KVO-compliant mutable array?


  • Subject: Re: Limited-access, KVO-compliant mutable array?
  • From: Rick Mann <email@hidden>
  • Date: Tue, 10 Jul 2012 23:51:51 -0700

Thanks to Roland and Quincy for quick and definitive answers. I'll do that.

It still bugs me, in the sense that publishing an NSArray* property seems to create a (conceptual) contract that while the property may change, that is, a different NSArray may get assigned to it, the contents of a given NSArray won't change. From what you've both said, this is clearly not the case. But it just smells a little funny.

One last question: What if the underlying data structure isn't an array at all, but rather a dictionary? It's not possible to insert into it at a particular index. Although that wouldn't prevent me from implementing the methods, it seems it would break, because the OS (and clients) would assume remove and insert are paired, and guaranteed to work on the same index (that is, insert followed by remove should result in an unchanged array).

--
Rick


On Jul 10, 2012, at 23:38 , Roland King wrote:

> That's exactly what you do.
>
> Declare the property as NSArray, that's what your clients see. In most cases you actually use an NSMutableArray as the backing variable so you can easily mutate it, you can either return that mutable array as the value of your property, or you can return a non-mutable copy of it. I normally return the actual array as the property is declared non-mutable so code that tries to mutate it produces lots of errors and warnings. If you're paranoid, copy it before return.
>
> Then, in the class which owns it, either implement the KVC indexed accessor methods (like insertObject:in<Key>AtIndex:) and call those, or use [ self mutableArrayValueForKey:<Key> ] to get a mutable array which also generates the correct KVC calls. Any observer of the property will get the correct KVO calls.
>
>
> On Jul 11, 2012, at 2:17 PM, Rick Mann wrote:
>
>> I feel certain people have run into this before, but my Googling didn't turn up exactly what I was looking for.
>>
>> I'd like to create a "mutable read-only" array property. I want clients of the class to be able to observe changes to the *contents* of the array (that is, if an element is added or removed), in addition to changes of the entire array. But I don't want those clients to be able to add or remove anything to/from the array themselves; I only want the class on which the property is defined to be able to do that.
>>
>> Is this possible? My first thought is to make a read-only NSArray* property, but then it seems that addition or removal of elements to/from that property wouldn't get observed; i.e., an NSArray should never change its contents.
>>
>> What's the right way to do this? Thanks!
>> --
>> Rick
>>
>>
>> _______________________________________________
>>
>> 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

  • Follow-Ups:
    • Re: Limited-access, KVO-compliant mutable array?
      • From: Quincey Morris <email@hidden>
References: 
 >Limited-access, KVO-compliant mutable array? (From: Rick Mann <email@hidden>)
 >Re: Limited-access, KVO-compliant mutable array? (From: Roland King <email@hidden>)

  • Prev by Date: Re: Instance not responding to selector
  • Next by Date: Re: Limited-access, KVO-compliant mutable array?
  • Previous by thread: Re: Limited-access, KVO-compliant mutable array?
  • Next by thread: Re: Limited-access, KVO-compliant mutable array?
  • Index(es):
    • Date
    • Thread