• 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: Some crash.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Some crash.


  • Subject: Re: Some crash.
  • From: "Clark Cox" <email@hidden>
  • Date: Sun, 3 Aug 2008 08:56:38 -0700

On Sat, Aug 2, 2008 at 9:15 PM, Sandro Noel <email@hidden> wrote:
> Chris.
>
> Thank you for the explaination!!
>
> but it leaves me with a question, where should i use it.
>
> I'm actually adding objects to the array, using addObject message, in the
> model,
> where should i use the mutableArrayValueForKey, and what keyPath should I
> use.

My general strategy for this is to never access the instance variable
directly, except when implementing init, dealloc and the KVC
accessors. All other access (even from other methods of the same
class) uses the proxy returned by mutableArrayValueForKey:. Something
like:

@interface MyClass : NSObject {
  NSMutableArray *myObjects;
}

@end

@implementation MyClass

-(id)init {
  if(self = [super init]) {
    myObjects = [[NSMutableArray alloc] init];
  }
  return self;
}

-(void)dealloc {
  [myObjects release];
  [super dealloc];
}

-(NSUInteger)countOfMyObjects {
  return [myObjects count];
}

-(id)objectInMyObjectsAtIndex:(NSUInteger)index {
  return [myObjects objectAtIndex: index];
}

-(void)insertObject:(id)object inMyObjectsAtIndex:(NSUInteger)index {
  [myObjects insertObject: object atIndex: index];
}

-(void)removeObjectFromMyObjectsAtIndex:(NSUInteger)index {
  [myObjects removeObjectAtIndex: index];
}

-(void)doSomething {
  NSMutableArray *arrayProxy = [self mutableArrayValueForKey: @"myObjects"];

  [arrayProxy addObject: ...];
  [arrayProxy removeObjectAtIndex: ...];
  //Or any other NSMutableArray calls
}

@end


That way, all edits to the array provide the appropriate notifications
to any observing objects. It also allows me to change the
implementation at a future date, and only have to change the
accessors. The reason that it is safe to access the array directly in
init and dealloc is that another object can't yet be listening at the
time of init (also the reason that your code worked when you initially
filled the array), and by the time of -dealloc, all of the observers
should have stopped their listening.

> the workflow is quite simple, the user selects a file, the controller calls
> my model to load the file in various classes,
> and one of the property of the model is transactions which is a
> MutableArray,
> that array is bound to the array controller, which is in turn providing the
> tableview with data.
>
> I'm new to cocoa... sorry bout that.
> thank you for your help.

--
Clark S. Cox III
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: Some crash.
      • From: Sandro Noel <email@hidden>
References: 
 >Some crash. (From: Sandro Noel <email@hidden>)
 >Re: Some crash. (From: "Adam R. Maxwell" <email@hidden>)
 >Re: Some crash. (From: Sandro Noel <email@hidden>)
 >Re: Some crash. (From: Chris Hanson <email@hidden>)
 >Re: Some crash. (From: Sandro Noel <email@hidden>)

  • Prev by Date: Re: -[NSBrowser scrollToVisible] not making all of the column visible
  • Next by Date: Re: Some crash.
  • Previous by thread: Re: Some crash.
  • Next by thread: Re: Some crash.
  • Index(es):
    • Date
    • Thread