• 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: problem with pure automatic KVO for array (no controllers...)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: problem with pure automatic KVO for array (no controllers...)


  • Subject: Re: problem with pure automatic KVO for array (no controllers...)
  • From: mmalcolm crawford <email@hidden>
  • Date: Sat, 21 May 2005 15:34:21 -0700


On May 21, 2005, at 3:15 PM, Michael Monscheuer wrote:

Yes I know, but... Reading the KVC doc it says:
//If the to-many related objects are mutable, and the -<key> method does not
//return an NSMutableArray, you must also implement - insertObject:in<Key>AtIndex:
//and -removeObjectFrom<Key>AtIndex:
My observed accessor -testArray *does* return a mutable array.
So my class implements -<key> and returns a mutable array.
So I should not need to implement more?!


insertObject:atIndex: is not KVC or KVO-compliant, hence:

[[testObject testArray] insertObject:[[NSObject alloc] init] atIndex:0];

doesn't post a KVO notification for testArray. You're "changing the array behind the controller's back".

You must either use the setTestArray: method, or implement and invoke the collection accessors (see below -- thanks Kevin!).

Please look at the various examples at <http://homepage.mac.com/mmalc/ CocoaExamples/controllers.html> and elsewhere.

mmalc

@interface DSDFooTest:NSObject {
    NSMutableArray *testArray;
}
    // simple accessors
-(NSMutableArray *)testArray;
-(void)setTestArray:(NSMutableArray *)newArray;


- (unsigned int)countOfTestArray;
- (id)objectInTestArrayAtIndex:(unsigned int)index;
- (void)insertObject:(id)anObject inTestArrayAtIndex:(unsigned int) index;
- (void)removeObjectFromTestArrayAtIndex:(unsigned int)index;
- (void)replaceObjectInTestArrayAtIndex:(unsigned int)index withObject:(id)anObject;


@end

@implementation DSDFooTest

    // init
-(id)init
{
    self = [super init];
    if (self) {
        testArray = [[NSMutableArray array] retain];
    }

    return self;
}

    // deallocation
-(void) dealloc {
    [testArray release];
    [super dealloc];
}

    // get accesor
-(NSMutableArray *)testArray {
    return testArray;
}

    // set accessor
-(void)setTestArray:(NSMutableArray *)newArray {
    [testArray release];
    testArray = [newArray retain];
}


- (unsigned int)countOfTestArray { return [testArray count]; }

- (id)objectInTestArrayAtIndex:(unsigned int)index
{
    return [testArray objectAtIndex:index];
}

- (void)insertObject:(id)anObject inTestArrayAtIndex:(unsigned int)index
{
    [testArray insertObject:anObject atIndex:index];
}

- (void)removeObjectFromTestArrayAtIndex:(unsigned int)index
{
    [testArray removeObjectAtIndex:index];
}

- (void)replaceObjectInTestArrayAtIndex:(unsigned int)index withObject:(id)anObject
{
[testArray replaceObjectAtIndex:index withObject:anObject];
}



@end


// ------- main ------------

int main(int argc, char *argv[]) {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        // creating observed instance
    DSDFooTest *testObject = [[DSDFooTest alloc] init];
        // creating observing instance
    DSDFooObserver *observer = [[DSDFooObserver alloc] init];

[testObject addObserver:observer forKeyPath:@"testArray" options: 0 context:NULL];

// this insertion will inform observer
[testObject insertObject:[[[NSObject alloc] init] autorelease] inTestArrayAtIndex:0];



[pool release];

    return 0;
}


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >problem with pure automatic KVO for array (no controllers...) (From: Michael Monscheuer <email@hidden>)
 >Re: problem with pure automatic KVO for array (no controllers...) (From: mmalcolm crawford <email@hidden>)
 >Re: problem with pure automatic KVO for array (no controllers...) (From: Michael Monscheuer <email@hidden>)

  • Prev by Date: Re: hyper-link style button
  • Next by Date: Re: User Authentication
  • Previous by thread: Re: problem with pure automatic KVO for array (no controllers...)
  • Next by thread: NSDistributedObjects & Fast User Switching
  • Index(es):
    • Date
    • Thread