Type coercion. Again.

I have the following problem:

I could do in ASOC :

and then I could do:

and get a nice variable to do calculations, like set floating to floating * 2.5.

But in the dreadful Objective-C.

. and back? I’m on it for two hours now. I get only useless (id), pointers*, nothing to perform calculations with.

I need help on this. There must be a way to tell the compiler “let me put this darn float into my dictionary and get it back!” May be another way to set the value for the key? I’m lost.

Well, you can only put objects, not primitive types like floats in a dictionary, but when you want to use the result just use floatValue to convert the NSNumber back to a float: val = [[dict valueForKey:@“key”] floatValue]

Ric

BTW, you have another error in your code – selectedObjects is an array, so you can’t use NSDictionary methods with it. You’ll have to get to a single object (which is presumably a dictionary) from the array first.

Maybe I should delete this post. I was sure to have tried this. But no.

I can put only objects in dictionaries. So I can’t put a nil object, but an object whose floatValue is nil is possible? In fact, I noticed that:

setValue: nil forKey: aKey // cell contents are erased

is not the same as:

setNilValueforKey: aKey // cell contents stay.

Anyway, thanks for the coercion. Don’t tell anybody.

EDIT:

I think the compiler is not aware of this. It’s working. Do not make noise.

Right, I forgot that NSArray has that method as well, but using setValue:forKey: on an array will change the value for each object in the array, which may or may not be what you want.

In fact, it was more that I wanted: in the ASOC version I set the controller to “single selection” and always regretted to have to change one line at a time. This time, I forgot this setting and saw the multiple selection be set at once! So I didn’t touch anything! It reminds me Bill Gate’s quotes :«It’s not a bug, it’s a feature». :lol:

float prevVal;
NSLog(@“Object status%@”, [[currentClassController selectedObjects] valueForKey:@“result”]);

// gives ; OK. Key does not exist or the value is undefined.

if ([[currentClassController selectedObjects] valueForKey:@“result”]== NULL){
NSLog(@“affecting 0”);
prevVal =0.0;
}else{
NSLog(@“affecting the value”);
prevVal = [[[currentClassController selectedObjects] valueForKey:@“result”]floatValue];}

The app regularly throws errors because it fails to detect and sends a floatValue message to the void:
__NSArrayI floatValue]: unrecognized selector sent to instance

I tried:
== NULL
== nil
isEqualTo : NULL
isEqualTo : nil
isEqualTo : [NSNull null]

but always failed to detect the null.

Help please!

If I understand the structure of your array, [[currentClassController selectedObjects] valueForKey:@“result”] returns an array (I’m assuming selected objects is an array of dictionaries). So the problem isn’t about nulls, it’s that NSArray doesn’t have a floatValue method.

Ric

Thank you, Ric, I was looking in a bad direction.

I’m loosing my time to program a new feature to enter values into this array and thought, well, maybe it’s time to put a little OOP into my application. Give me your advice:

If the user has to enter digital values into a table view, a way to do it is to make the column editable, implement a controlTextDidChange method, filter the non-numeric characters, and so on.

This approach works but has its drawbacks. The user may enter one line at a time, he has to pay attention to the keys he’s typing, must validate the entry, and so on. So I had the idea of a “calculator metaphor” (like Apple’s desktop calculator): it’s more intuitive, the characters are filtered, and the user can make a multiple selection and enter the same value, that will be attributed to several rows at a time.

This “calculator” could be an Objective-C class: some tokens on entry (digits, period and erase, all could be NSString objects), an internal value, some flags to indicate “new entry” or “period already entered”, and a floating-point value on return (the float value of the string): this “calculator” won’t have to calculate at all. :slight_smile:

The design is not very clear, but I think it would teach me a lot about +class and -instance methods, properties, private instance variables. Maybe I should first test the class into a separate project, then integrate it into the final application.

What do you think?

Regards,