Re: NSFormatter stringForObjectValue: (what does parameter refer to?)
Re: NSFormatter stringForObjectValue: (what does parameter refer to?)
- Subject: Re: NSFormatter stringForObjectValue: (what does parameter refer to?)
- From: Jonathan Jackel <email@hidden>
- Date: Sun, 30 Jan 2005 20:13:32 -0500
On Jan 30, 2005, at 6:51 PM, Jeremy French wrote:
What object does the "anObject" parameter refer to, in the following
NSFormatter method?
- (NSString *)stringForObjectValue:(id)anObject
The NSFormatter class documentation says the above method, "returns
the NSString that textually represents the cell’s object for display
and—if editingStringForObjectValue: is unimplemented—for editing.
First test the passed-in object to see if it’s of the correct class.
If it isn’t, return nil; but if it is of the right class, return a
properly formatted and, if necessary, localized string."
For example, I created a subclass of NSFormatter, which I named
"MyFormatter". I instantiated MyFormatter in Interface Builder, and
assigned it as the formatter for an instance of a NSTextField.
Now when the stringForObjectValue: method is invoked, the class name
of anObject is NSCFString. This is different from what I expected: I
thought the class name would be NSTextField.
I used respondsToSelector: to determine if anObject responds the
following NSControl methods: doubleValue, floatValue, intValue,
objectValue and stringValue. The anObject responds to the first three
(doubleValue, floatValue, intValue), but not the last two (objectValue
and stringValue).
Questions:
1) Why isn't the class name (of anObject) NSTextField, since the
formatter is assigned to an isntance of NSTextField?
Because anObject is the object that is represented to the user in the
text field, not the text field itself. -stringForObjectValue: gives the
formatter a chance to take any old custom object and show it to the
user in the form of a string by, for instance, returning [anObject
description].
2) Why doesn't XCode 1.5 Help display documentation for NSCFString
class, when that class name is typed in the documentation search box?
Because that is not a public class. It is a subclass of NSString,
which is a "class cluster." When you create or receive an NSString,
what you get is actually a subclass of NSString that is generally
optimized for the kind of work it is doing.
Basically, use the NSString docs. And familiarize yourself with class
clusters before you start subclassing any Foundation objects.
3) Why doesn't NSCFString appear in either the Application Kit
Reference or Foundation Reference documentation?
See above.
4) Why doesn't anObject respond to objectValue and stringValue, but
does respond to doubleValue, floatValue, intValue?
Because, as far as you should be concerned, it's an NSString, and
NSStrings respond as you have described.
Jonathan
_______________________________________________
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