• 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: resetting ivars safely
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: resetting ivars safely


  • Subject: Re: resetting ivars safely
  • From: Chris Hanson <email@hidden>
  • Date: Wed, 12 Sep 2007 15:13:24 -0700

On Sep 12, 2007, at 1:59 PM, Daniel Child wrote:

Here is the relevant code. Thanks much....

Since your class manages a couple of mutable collections, I'd just set those up in -init, release them in -dealloc, and otherwise manipulate the collections themselves in the accessor methods.


I also wouldn't provide an accessor to the underlying mutable array, only an immutable array. Instead, I would write the appropriate KVC accessor methods (see <Foundation/NSKeyValueCoding.h> for details) and either call those directly or use -mutableArrayValueForKey: on the property to manipulate it from my own code. Doing so will ensure the proper KVO messages are posted whenever the property is changed, which is important for bindings. These methods will also be invoked directly by bindings (since they use -mutableArrayValueForKey:) making them more efficient.

Using the KVC accessor methods also lets the compiler help you with type safety, assuming that your "wordCandidates" collection holds "Word" objects. A tool like Accessorizer can make it really easy to generate these rather than write them by hand.

Below is how I would write that code. I'm assuming a *non*-singleton WordParser here, and only showing enough to give an example of what I'd do.

  -- Chris

@class Word; // forward declaration

@interface WordParser : NSObject {
@private
    NSMutableArray *wordCandidates;
}

- (NSArray *)wordCandidates;
- (void)setWordCandidates:(NSArray *)value;
- (unsigned)countOfWordCandidates;
- (Word *)objectInWordCandidatesAtIndex:(unsigned)index;
- (void)insertObject:(Word *)object inWordCandidatesAtIndex: (unsigned)index;
- (void)removeObjectInWordCandidatesAtIndex:(unsigned)index;


@end

@implementation WordParser

- (id)init {
if (self = [super init]) {
// using a random value, pick a sensible default for your data set
wordCandidates = [[NSMutableArray alloc] initWithCapacity:16];
}


    return self;
}

- (void)dealloc {
    [wordCandidates release];

    [super dealloc];
}

- (NSArray *)wordCandidates {
    return wordCandidates;
}

- (void)setWordCandidates:(NSArray *)value {
    [wordCandidates setArray:value];
}

- (unsigned)countOfWordCandidates {
    return [wordCandidates count];
}

- (Word *)objectInWordCandidatesAtIndex:(unsigned)index {
    return [wordCandidates objectAtIndex:index];
}

- (void)insertObject:(Word *)object inWordCandidatesAtIndex: (unsigned)index {
[wordCandidates insertObject:object atIndex:index];
}


- (void)removeObjectFromWordCandidatesAtIndex:(unsigned)index {
    [wordCandidates removeObjectAtIndex:index];
}

@end

_______________________________________________

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: resetting ivars safely
      • From: "Shawn Erickson" <email@hidden>
References: 
 >resetting ivars safely (From: Daniel Child <email@hidden>)

  • Prev by Date: Re: resetting ivars safely
  • Next by Date: Animated NSWindow effects?
  • Previous by thread: Re: resetting ivars safely
  • Next by thread: Re: resetting ivars safely
  • Index(es):
    • Date
    • Thread