Reputation: 409
I'd like to have some hints about one thing:
In an array, bound to a tableview column, I have some numeric (float) values. These values are entered (put into the array) using interface buttons, labelled "0", "1", "2" and so on. There is also a "C" button to cancel an entry.
Later, I have some calculations to do with this array. If no result was entered, the array's object cannot be 0. "No value" is certainly better represented by "nil"… but you are not allowed to insert a nil object into an array (nil means "end of array").
A NSArray contains objects. That is, here, these object must be NSNumbers. So I can initialize my array by filling it with [NSNumber numberWithDouble:…] double what? I cannot put nil here!
A true NSArray does not (can not) contain "holes". But mine has to, if for example, the third item never received a value. How can I:
implement an "undefined" value for my NSNumber? make the "C" button erase a previously entered result?
There must be a solution. In ApplescriptObjC I used a trick: I did the attribution in the method called by every button, using its title, so:
if sender's title as string is "C" then
set item theRow of gResult to missing value -- the row was the selected line, gResult was a AS list.
else
set item theRow of gResult to sender's title as real
end if
What should I do here? - choose a special value (say -1) to indicate an undefined value, then use a NSPredicate to filter my array? - how could I turn the cell to "nothing" as the special value IS something and appear into the cell (even )
I'm sure there is an elegant solution to this problem. But for now I just tear out what little hair I have left…
Thank for help…
Upvotes: 1
Views: 953
Reputation: 23359
An NSArray must always contains objects, nil is a flag indicating the end of the array. Thus in order to represent a nil value as an object you should use NSNull as follows:
[NSNull null]
Upvotes: 2
Reputation: 70165
You need a 'distinguished' object in your NSArray. When you see that distinguished object, your code does something different. Such a 'distinguished' object is often called a 'sentinel' and it can be anything. For example:
static NSNumber *theSentinel;
/* initialize theSentinel somewhere with */
theSentinel = [NSNumber numberWithFloat: <anyfloat-doesnt'-matter-which>];
then in your code:
NSNumber *selection = [NSArray objectAtIndex: <user selection>];
if (selection == theSentinel) { /* something */ }
else { /* main */ }
And in your predicates you can use (in words, not code):
predicate = (obj NOT EQUAL theSentinel) AND (rest of your predicate based on float)
Upvotes: 0