• 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: William Squires <email@hidden>
  • Date: Wed, 11 Jul 2012 08:10:04 -0500

This is (one) application of the "wrapper" design pattern - make a class that wraps (contains) an NSMutableArray, and only expose those properties and methods you want clients to have access to. This is possible by declaring public readonly @properties, but redeclaring them in your implementation file using an anonymous category. Ex:

MyFoo.h
@interface MyReadonlyFoo : NSObject

@property (nonatomic, readonly) NSMutableArray *myFooObjects;

MyFoo.m
#import "MyFoo.h"

@interface MyReadonlyFoo ()
@property (nonatomic, readwrite) NSMutableArray *myFooObjects;
@end

@implementation MyReadonlyFoo

@synthesize myFooObjects;

@end

Now your implementation has access to read-write accessors, but everyone else only sees readonly accessors. They should still be KVC/KVO compliant.
(Note they syntax may be slightly off - this is just what I remember from various 'learn ObjC' books…)

On Jul 11, 2012, at 1:17 AM, 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


References: 
 >Limited-access, KVO-compliant mutable array? (From: Rick Mann <email@hidden>)

  • Prev by Date: How to implement an object whose properties are really dictionary entries.
  • Next by Date: Re: How to implement an object whose properties are really dictionary entries.
  • Previous by thread: Re: Limited-access, KVO-compliant mutable array?
  • Next by thread: Does codesigning or Dev ID ever avoid the quarantine dialog?
  • Index(es):
    • Date
    • Thread