Re: A category for NSMatrix?
Re: A category for NSMatrix?
- Subject: Re: A category for NSMatrix?
- From: Ondra Cada <email@hidden>
- Date: Wed, 12 Sep 2001 13:25:31 +0200
Rafael,
>
>>>>> Rafael K. Kobylinski (RKK) wrote at Wed, 12 Sep 2001 11:17:21 +0200:
RKK> I am new to Objective-C and Cocoa and working my way through the
RKK> O'Reilly book.
I haven't read it, but generally it is said to be quite bad.
RKK> In Chapter 13, the author defines a simple utility
RKK> function clearButtonMatrix - I was wondering whether this really
RKK> should be rather implemented in a new category for NSMatrix
Right you are. It should. OTOH there might be one slight reason not to do
that in this particular case:
RKK> concept of categories is new to me, I am not quite sure what the
RKK> implications would be).
There's just one potential problem of name clash, but it applies for
function as well (unless the function is static -- and that's that).
Though, the particular function is extremely non-flexible. If you do need to
do such things, they should generally be static. A method (regardless
whether in category or not) can't be static. So, there is some (not too
strong) reason to use a static function instead of a method here.
(That was an academic debate though, since the function as you've copied it
here is not static anyway ;)
RKK> The function is defined as follows:
RKK>
RKK> void clearButtonMatrix(id matrix)
RKK> {
RKK> int i, rows, cols;
RKK> [matrix getNumberOfRows: &rows columns: &cols];
RKK> for (i = 0; i < rows; i++)
RKK> [[matrix cellAtRow: i column: 0] setState: NO];
RKK> }
Actually, I would rename the method/function, the name does NOT look to me
as an intuitive one for the task. Also I would add at least the very trivial
flexibility -- even if I don't need it just now, I might in the next upgrade,
and it costs nothing:
@interface NSMatrix (MyNiceMatrixCategory)
-(void)setStateOfCellsInColumn:(int)col to:(int)state;
@end
@implementation NSMatrix (MyNiceMatrixCategory)
-(void)setStateOfCellsInColumn:(int)col to:(int)state {
int i, rows=[self numberOfRows];
for (i=0;i<rows;i++) [[self cellAtRow:i column:col] setState:state];
}
@end
RKK> I noticed that the input paremter is typed to id rather than
RKK> NSMatrix, although it seems to me that this function is always called
RKK> with a NSMatrix.
Well, it should be typed NSMatrix* here. If it was typed id (with a real
desire to use the function for different objects which just happen to
understand the API) there should be something like
void clearButtonMatrix(id matrix) {
if ([matrix respondsToSelector:@selector(getNumberOfRows:columns:)] &&
[matrix respondsToSelector:@selector(cellAtRow:column:)]) {
...
} // else report error, if needed
}
RKK> Anyone willing to share his insights on the possible drawbacks of
RKK> using a category instead in this case?
Actually, none (some very very slight and in practice utterly unimportant
loss of speed -- sending of a message is roughly twice slower than calling a
function).
If they used a static function (as it should have been), then the globality
of a category.
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
private email@hidden
http://www.ocs.cz/oc