Re: Cocoa Documentation on NSFormatter
Re: Cocoa Documentation on NSFormatter
- Subject: Re: Cocoa Documentation on NSFormatter
- From: Ondra Cada <email@hidden>
- Date: Sat, 15 Dec 2001 23:46:48 +0100
Thomas,
>
>>>>> Thomas Lachand-Robert (TLR) wrote at Sat, 15 Dec 2001 23:15:57 +0100:
TLR> Anyway, in my own subclass of NSFormatter, getObjectValue... is called
TLR> first with the string I typed in the text field. I return a custom
TLR> object which is here a subclass of NSObject, say myObjectClass. SINCE
TLR> the declaration declares the object of type 'id', I presume that any
TLR> object should be convenient, but it is NOT.
But it _IS_, so far as the formatter is concerned!
Quite another question is _how you use the thing_. I guess you probably use
it as some cell's formatter (after all, a vast majority of formatters is used
that way). *IF* you do that, though, you should have looked at the NSCell
docs, which cleanly declare
-(void)setObjectValue:(id <NSCopying>)object
Therefore:
- it is quite all right to return *ANY* id from the NSFormatter method as 'obj';
- THOUGH, of course, you have to consider how it will be used!
- and if it happens to be used as an object value of a cell, it HAS to
conform to NSCopying.
It is documented nicely and well.
TLR> Just after the return, I get
TLR> the exception, which apparently means that Apple expects here a
TLR> NSObject<NSCopying>*, not just a basic 'id'.
They don't give a damn what's returned from NSFormatter; they, though, want
a cell's objectValue to be "copiable". Therefore...
TLR> It turns out that my object
TLR> is not intended to be copied, and doesn't implement NSCopying (it would
TLR> be complicated and very inconvenient to do so).
...you either have to make the object to conform NSCopying, or have not to
use NSCell (including, of course, subclasses).
TLR> So I believe there is
TLR> something wrong in Apple's doc, here, and don't know exactly what to do.
Nothing's wrong with the docs at all. The problem is that you would want to
use some class (actually, NSCell), a way it was not intended to be (actually,
to contain an object value which does not conform to NSCopying).
As for "what to do", well, you *HAVE* to make cell-shown objects copiable,
since otherwise the display might be inconsistent.
If you want to be responsible for consistency yourself (and if making the
real objects copiable is not an option indeed), one very simple solution
might be to use a plain NSValues as the cell value, and in the formatter
convert it to the real object by interpreting its contents as the id. Of
course, you have to ensure that no real object would vanish while displayed.
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
2K Development: email@hidden
http://www.2kdevelopment.cz
private email@hidden
http://www.ocs.cz/oc