Re: takeValue:forKey and BOOL
Re: takeValue:forKey and BOOL
- Subject: Re: takeValue:forKey and BOOL
- From: Jane <email@hidden>
- Date: Fri, 4 Apr 2003 04:05:31 -0700
Thank you for responding to my message. The NSKeyValueCoding
documentation states: " Scalar values (such as int or float) can be
accessed as well. Their values are just passed using NSNumber objects,
instead. The key-value coding methods automatically detect the scalar
instance variables and convert them to and from NSNumber objects as
needed."
The BOOL is correctly converted to an NSNumber object in the
valueForKey: method and set as a 0 or 1 in the NSTableView. I expect
the reverse to happen when I use takeValue:forKey. That is, the
NSString object should be converted to an NSNumber object. Further
more, we use int and float types with NSKeyValueCoding and they are
correctly converted to NSNumber objects via NSKeyValueCoding without
adding an NSNumberFormatter to the NSTableColumn data cell.
I did, however, try your suggestion and changed my awakeFromNib to:
- (void)awakeFromNib {
aMutableArray = [[NSMutableArray arrayWithObject:[[TestObject alloc]
init]] retain];
[tableView setDataSource:self];
[[[tableView tableColumns] objectAtIndex:0] setIdentifier:@"aBool"];
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init]
autorelease];
[numberFormatter setFormat:@"0"];
[[[[tableView tableColumns] objectAtIndex:0] dataCell]
setFormatter:numberFormatter];
}
and the BOOL does get correctly set via the accessor method. This is a
better workaround then changing the setABool type to int.
On Thursday, April 3, 2003, at 09:25 PM, Ryan Dingman wrote:
Vince and Jane,
I don't think that this is a bug in key/value coding, but rather a bug
in Jane's code; more specifically, a problem with the configuration of
the NSTableView. Key/value coding does provide type coercion between
scalars and objects. For BOOL, key/value coding will convert to/from
an NSNumber. I think that your problem likely is due to the fact that
when you edit the value via a field in NSTableView that NSTableView is
setting an NSString on your value and hence the exception that you
get. Try applying an NSNumberFormatter to the column in which you are
editing your BOOL value in the NSTableView. The NSNumberFormatter
will convert the NSString to an NSNumber before it gets sent through
key/value coding to get set on you object.
I hope this helps.
ryan
On Thursday, April 3, 2003, at 09:46 AM, Vince DeMarco wrote:
*This message was transferred with a trial version of CommuniGate(tm)
Pro*
On Thursday, April 3, 2003, at 8:16 AM, Jane wrote:
We have found what appears to be an error in takeValue:forKey when
the instance variable in the target object is a BOOL. The following
code illustrates problem:
AppController.h
@interface AppController : NSObject {
IBOutlet NSTableView *tableView;
NSMutableArray *aMutableArray;
}
@end
AppController.m
#import "AppController.h"
#import "TestObject.h"
@implementation AppController
-(void)awakeFromNib {
aMutableArray = [[NSMutableArray
arrayWithObject:[[TestObject alloc] init]] retain];
[tableView setDataSource:self];
[[[tableView tableColumns] objectAtIndex:0]
setIdentifier:@"aBool"]; }
-(int)numberOfRowsInTableView:(NSTableView *)aTableView {
return [aMutableArray count]; }
-(id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex {
return [[aMutableArray objectAtIndex:rowIndex]
valueForKey:[aTableColumn identifier]]; }
-(void)tableView:(NSTableView *)aTableView
setObjectValue:(id)anObject forTableColumn:(NSTableColumn
*)aTableColumn row:(int)rowIndex {
[[aMutableArray objectAtIndex:rowIndex] takeValue:anObject
forKey:[aTableColumn identifier]]; }
@end
TestObject.h
@interface TestObject : NSObject {
BOOL aBool;
}
-(void)setABool:(BOOL)yesNo;
-(BOOL)aBool;
@end
AnObject.m
#import "TestObject.h"
@implementation TestObject
-(id)init {
if (self = [super init]) aBool = YES;
return self;
}
-(void)setABool:(BOOL)yesNo { aBool = yesNo; }
-(BOOL)aBool { return aBool; }
@end
IB was used to add one NSTableView with one column to the main
window.
The initial value of aBool displays in the table correctly as "1".
But when the cell is selected, a value of 0 is typed, and ENTER
pressed, the following error displays on the console:
2003-04-02 09:03:59.908 TakeValueError[576] *** -[NSCFString
charValue]: selector not recognized
2003-04-02 09:03:59.915 TakeValueError[576] Exception raised during
posting of notification. Ignored. exception: *** -[NSCFString
charValue]: selector not recognized
Is this a known problem or should I use Apple's error submission
process to notify Apple?
We did discover the following workaround:
- (void)setABool:(int)yesNo { aBool = yesNo; }
I think its a bug, please file one.
vince
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.