• 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: takeValue:forKey and BOOL
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: takeValue:forKey and BOOL


  • Subject: Re: takeValue:forKey and BOOL
  • From: Ryan Dingman <email@hidden>
  • Date: Fri, 4 Apr 2003 08:41:09 -0800

On Friday, April 4, 2003, at 03:05 AM, Jane wrote:

*This message was transferred with a trial version of CommuniGate(tm) Pro*
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.

This will not happen. takeValue:forKey: will only perform one level of type conversion. In your case this is BOOL <--> NSNumber. The problem arises when calling -takeValue:forKey: with object values that you get from text fields. Text fields generally return an NSString. So, something needs to perform the conversion between NSString and NSNumber. You can hardly expect key/value coding to do this. It cannot and should not attempt to deal with every type of object that you throw at it. This is exactly why formatters were invented, to converted between NSString's and other types objects which one might use in their model.

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.

This is because key/value coding will call -intValue on the object when converting to an int scalar and -floatValue on the object when converting to a float scalar. NSString just happens to responds to both of these methods, but it does not respond to -boolValue which is the method that gets called when attempting to convert to a BOOL. An alternative, would have been for you to implement -boolValue on NSString in a category.

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.

This is hardly a "workaround". This is how Cocoa has worked in this regard for a very long time.

Hope this helps.

ryan
_______________________________________________
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.

References: 
 >Re: takeValue:forKey and BOOL (From: Jane <email@hidden>)

  • Prev by Date: Extracting Safari's favicons cache
  • Next by Date: NSTexView not updated after setString
  • Previous by thread: Re: takeValue:forKey and BOOL
  • Next by thread: Re: takeValue:forKey and BOOL
  • Index(es):
    • Date
    • Thread