0

im building a chart view and I'm giving it a datasource to which it can ask for a Y value that corresponds to a given X index. In some cases, there is no Y value for a given X index. I can't send nil in this circumstance, as 0 may be a perfectly valid Y value. What is the best way in objective C to communicate that there is No Value at all?

0

5 Answers 5

3

It seems to me that you're sending the data between the model and the view using some sort of object, perhaps NSNumber, that's holding a value, and you are retrieving the value by sending it a message, and you know that if the object is nil, the message send will return 0.

That's correct. In this case, simply check if the object is nil, and proceed accordingly:

- (void)plotValue:(NSNumber *)number
{
    if (number == nil) {
        // no value
    } else {
        // Value may be 0, but you can now safely plot it.
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Don't use primitives in your data source, instead use NSNumber. Then you can differentiate between nil and an NSNumber storing the value 0.

Comments

2

nil is not 0. They are different.

If you are using NSNumber, then nil is clearly disjoint from the set of NSNumber.

If you are using int, then you can't use nil.

14 Comments

Except in Objective-C++, where nil is #defined to 0.
@H2CO3: It's defined to __null.
@DietrichEpp keep following the #defines until you hit the root definition. Eventually, it's 0 or some cast of 0.
@DietrichEpp Follow the macro expansion. In turn, that's __DARWIN_NULL (or the alike), which is then just 0 if __cplusplus is defined, or ((void *)0) if it isn't.
@H2CO3: I'm telling you, it's __null, even after all the macros are expanded.
|
1

If you don't want to use an object (NSNumber), you can return a negative number (if possible) or the maximum/minimum integer that is unlikely to be returned by the datasource.

Comments

0

I'm assuming you're talking about some scalar like an NSInteger or something. If literally any integer is valid for the Y value, then there is no way to do this. However, most likely there is some practical range, so you can just choose some value outside this range to be "special". For example, Cocoa has the value NSNotFound, which NSArray will return if you ask for the index of an object that is not in the array. It is defined as the largest possible NSInteger. You could reuse this yourself if your Y value will never be NSIntegerMax.

The other option, assuming you don't want to create a special type just for this, would be to have your method return both Y and whether or not Y was actually found. For example, some rough pseudocode:

- (NSInteger)yForX:(NSInteger)x wasFound:(BOOL *)found {
    if (yWasFound) {
        if (found) 
            *found = YES;
        return y;
    } else {
        if (found)
            *found = NO;
        return 0;
    }
}

You could use NSNumber, as some have suggested, but I find it a little awkward to deal in NSNumbers just to get nil.

1 Comment

I'd probably do it the other way around, return BOOL and have the method named getYForX:. Most methods with get in the name indicate caller-supplied buffers and also many methods with get in the name return BOOL indicating success or failure (if such a method could fail).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.