Re: Need help wrapping my head around bindings
Re: Need help wrapping my head around bindings
- Subject: Re: Need help wrapping my head around bindings
- From: Cem Karan <email@hidden>
- Date: Tue, 11 Dec 2007 22:07:30 -0500
Thanks for writing so soon, any and all help is appreciated.
On Dec 10, 2007, at 11:59 PM, mmalc crawford wrote:
On Dec 10, 2007, at 6:21 PM, Cem Karan wrote:
1) When I create a class instance, it has instance variables. The
Objective-C 2.0 method for accessing these is via the @property
directive. Does using @property automatically make an instance KVO
compliant
No. Just as with KVC (<http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_5_section_5.html
>), KVO and properties are orthogonal technologies.
Using @property per se does not "automatically make an instance KVO
compliant": typically it will be KVO-compliant by default, but your
class may suppress automatic key-value observing and you could
specify non-KVO-compliant accessors for the property.
Hmmm... interesting, and it makes sense considering KVO/KVC is not a
part of the language itself...
That is, would the following declaration (the the corresponding
@synthesize) make 'contents' something I can bind to?
@interface CFKcontroller : NSObject {
NSMutableArray *contents;
}
@property(retain) NSMutableArray *contents;
@end
Given this specification, yes you could bind to 'contents'.
But to preserve encapsulation you should almost certainly copy
rather than retain the array, and you should implement your own set
accessor to create a mutable copy. For efficiency, you should also
implement the indexed accessors as shown in <http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/AccessorConventions.html#//apple_ref/doc/uid/20002174-178830-BAJEDEFB
>.
Is this correct then?
@interface CFKcontroller : NSObject {
NSMutableArray *contents;
}
- (unsigned int)countOfContents;
- (NSString *)objectInContentsAtIndex:(unsigned int)index;
- (void)getContents:(NSString **)strings range:(NSRange)inRange;
- (void)insertObject:(NSString *)string inContentsAtIndex:(unsigned
int)index;
- (void)removeObjectFromContentsAtIndex:(unsigned int)index;
@end
@implementation CFKcontroller
- (id)init
{
if ((self = [super init]) != nil)
{
contents = [[NSMutableArray alloc] init];
[contents addObject:@"Dummy String"];
NSLog(@"Just loaded the array. Contents are: %@", contents);
}
return self;
}
- (unsigned int)countOfContents {
return [contents count];
}
- (NSString *)objectInContentsAtIndex:(unsigned int)localIndex {
return [contents objectAtIndex:localIndex];
}
- (void)getContents:(NSString **)strings range:(NSRange)inRange {
[contents getObjects:strings range:inRange];
return; // handy place to put a breakpoint
}
- (void)insertObject:(NSString *)string inContentsAtIndex:(unsigned
int)localIndex {
NSLog(@"Inserting a string at %u. The string is: %@",
localIndex, string);
[contents insertObject:string atIndex:localIndex];
return; // handy place to put a breakpoint
}
- (void)removeObjectFromContentsAtIndex:(unsigned int)localIndex {
NSLog(@"Removed the string at index %u", localIndex);
[contents removeObjectAtIndex:localIndex];
return; // handy place to put a breakpoint
}
@end
2) Assuming that #1 is correct, how do I bind to it using an array
controller?
Just as you would any other array.
Hmmm.... OK, I'm doing something seriously wrong then...
I have tried to a) using an array controller bound to CFKcontroller
for its content, with 'contents' as its key,
What actual binding did you set -- 'contentArray'?
I bound it to 'myController', which is an Object that is an instance
of CFKcontroller. The model key path is 'self', although I have also
tried to attributes to include 'contents' in the Object Controller
block, and in that case I set the model key path to 'contents'. In
each case, I have a termination exception; here is a typical one:
2007-12-11 21:59:23.947 ArrayControllerTest[1760:10b] *** -
[_NSControllerObjectProxy copyWithZone:]: unrecognized selector sent
to instance 0x103d170
2007-12-11 21:59:23.948 ArrayControllerTest[1760:10b] An uncaught
exception was raised
2007-12-11 21:59:23.948 ArrayControllerTest[1760:10b] *** -
[_NSControllerObjectProxy copyWithZone:]: unrecognized selector sent
to instance 0x103d170
2007-12-11 21:59:23.949 ArrayControllerTest[1760:10b] *** Terminating
app due to uncaught exception 'NSInvalidArgumentException', reason:
'*** -[_NSControllerObjectProxy copyWithZone:]: unrecognized selector
sent to instance 0x103d170'
2007-12-11 21:59:23.952 ArrayControllerTest[1760:10b] Stack: <long
list of numbers>
I can send you copies of how I did it both ways; the combined projects
zipped are 108 KB, and most of that is Xcode's data. I can't send it
to the list as it gets rejected for being too large, and I don't have
a convenient place to upload it to.
Thanks for the help, I appreciate it.
Thanks,
Cem Karan
_______________________________________________
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