Re: NSMatrix problem
Re: NSMatrix problem
- Subject: Re: NSMatrix problem
- From: Bill Bumgarner <email@hidden>
- Date: Tue, 3 Jul 2001 10:26:59 -0400
Is there a reason why you need to create the matrix of checkboxes
programmatically? It can be done in IB-- simply drag out a checkbox,
hold down the option(alt) key, and drag out as many checkboxes you
need. The end result is are buttoncells configured as a checkboxes that
live within an NSMatrix.
At the very least, you could do the above and figure out how they are
configured to figure out what is wrong with your code. I suspect it is
a configuration problem on the NSMatrix; in particular, when you click
on the checkbox, it unchecks itself but the matrix promptly rechecks it
as it "selects" the cell containing the checkbox.
---
NSNumber is an immutable data type as a performance optimization.
Think of it as being very much like an int.
As such, you wouldn't say...
1 = 5;
... any more than you would change the value contained in an NSNumber.
You need to write your code such that the NSNumber instance is treated
as a static scalar value. I.e.:
myValue = [[NSNumber numberWithInt: 5] retain];
And you might write:
- (void) setMyValue: (int) newValue
{
[myValue release];
myValue = [[NSNumber numberWithInt: newValue] retain];
}
But there is a bigger problem here. You wrote:
- (void) setTypeValue:(Type)value{
[self release];
self = [[NSNumber alloc] initWithType:value];
}
You *really* do not want to do this....
Consider:
myValue = [[NSNumber numberWithInt: 5] retain];
[myValue setTypeValue: 12];
What state is myValue now? Unfortunately, it is now a released
reference to the original NSNumber that contained 5. Next message to
myValue will very likely crash your application.
Self is a pointer to an object like any other pointer. Assigning a
value to self changes the value of self in that local method, it doesn't
update the references to the original pointer value everywhere else.
Think container versus reference.
Self is a reference to an object that contains a value. If you change
the value (which NSNumber does not allow for a variety of good reasons),
all references to that instance will refer to a container containing the
changed value. If you change the reference, only that reference is
changed to refer to a new container.
b.bum
On Tuesday, July 3, 2001, at 06:03 AM, email@hidden
wrote:
Message: 6
Date: Tue, 3 Jul 2001 03:02:38 -0400
To: email@hidden
From: Mark T <email@hidden>
Subject: NSMatrix problem
I'm having an amazingly tough time doing something that I believe should
be incredibly simple. I'm trying to make a 1x2 NSMatrix consisting of
just 2 NSBussonCells (checkboxes). The NSMatrix is configured as radio
mode, allowing empty selection. But when you click on a checked
checkbox,
it doesn't uncheck. I've tried numerous attempts at fixing this in code,
but with no success, so if anyone could point out how to do this, I
would
be grateful.
On another matter entirely, isn't it a little odd that NSNumber doesn't
allow you to change its value after the object has been created? I
expected to find setIntValue, setShortValue, etc... methods for it, and
was surprised to find that they weren't there. Is there a reason for
this? I ended up writing my own very simple methods to account for this
omission, implemented as a category, of the following form:
- (void) setTypeValue:(Type)value{ [self release]; self = [[NSNumber
alloc] initWithType:value];}
Can anyone see any obvious problems with this, and if not, why didn't
Apple implement it?
Thanks,Mark T.