NSNull and NSMutableDictionary

I have been trying to enter a null value into a field of an NSMutableDictionary.

I have read that you cannot use nil values in a dictionary which I believe means I cannot use missing value.

I also read that you should use the NSNull class for this purpose.

It does not appear to work with:

set tValue to NSNull’s |null|()

when I place this into the dictionary. It does work if i use 0.0 as it’s value.

The field is a numeric currency value.

Does anyone have any ideas please.

Thanks

Terry

Can you give more information on what you are trying to do? Is this just a single dictionary or an array of dictionaries that you are binding to a table, for instance.

It is an NSMutableArray of NSMutableDictionaries.

An NSTableView is involved and the NSMutableArray is the data source.

No error is generated but when I use missing value in the key/value pairing the NSMutableDictionary is not created.

If I use a plain 0.0 number the dictionary is created.

This is what prompted me to start looking at the dictionary and I found that you cannot pass a nil as a field value of a NSMutableDictionary.

In cocoa you should use [NSNull null] but NSNull’s |null|() does not work in applescriptobjc or I ca’t get it to work?

Interestingly if I

log NSNull’s |null|() it says missing value in the console.

All the best

Terry

Hi,

maybe the setNilValueForKey: method could help

Invoked by setValue:forKey: when it’s given a nil value for a scalar value (such as an int or float).

But why do you want a dictionary with a nil value? I don’t know if you’re trying to do something like I did with a program to keep track of weather stats at my house. I add a date, low temp, high temp, rain amount, and a comment with the following statement:

theData's addObjectsFromArray_({{aDate:today, lowTemp:newLow, highTemp:newHigh, rainAmount:newRain, comment:newComment}})

But, of course there are a lot of days with no rain, so if newRain is missing value, then the key, rainAmount, just isn’t created for that dictionary and you have a blank cell in that column of the table, which is what I want. If I go back and edit that cell later, and put a number in there, then the dictionary is created.

Ric

Hi,

I did think of this but in my case I want to read the dictionary and check whether there was rain on a particular day.
If the value/key does not exist how can I do that?

I suppose answering my own question, I check for the existence of the key, if the key does not exist then the value is “blank”.

Could this be the way to do it?

All the best

Terry

Hi,

I suppose this is very similar to what rdelmar was suggesting.

Don’t create the field if the value does not exist?

I will try both and report back.

All the best

Terry

NSNull is a class, so that should be:

set tValue to current application's NSNull's |null|()

Hi,

Not if you declare at the top of your script:

property NSNull : class "NSNull"

I prefer the shorter form to save typing.

Unless there are particular places where it does not apply and Apple have failed to inform us. As everyone knows the documentation is rather sparse.

All the best

Terry

Hi,

The format I used was to check for the key, if it does not exist, create it with the value. Basically rdelmar’s solution. Thanks.


--If the key does not exist
--create a value for the key

if tThisRecDict's valueForKey_("myKey") = missing value then
tThisRecDict's setValue_forKey_(myValue, "myKey")
end if

But there was no hint of that in your post.

IAC, NSNull’s |null|() is working as expected here:

		set myDict to current application's NSMutableDictionary's dictionaryWithCapacity_(2)
		tell myDict to setObject_forKey_("Blah", "Bar")
		tell myDict to setObject_forKey_(current application's NSNull's |null|(), "Foo")
		log myDict
		set x to myDict's valueForKey_("Foo")
		log x --> <null>
		log x's |class|() --> NSNull
		log x = current application's NSNull's |null|() --> 1
		log Foo of (myDict as record) = missing value --> 1

The problem, perhaps, is that you were hoping for NSNull to be bridged to missing value, but it looks like you need to coerce its container to do that.

Hi,

Interesting:

This also works in your test script:


tell myDict to setValue_forKey_(current application's NSNull's |null|(), "Foo")

I had moved on because it did not seem to work in my original script, however, I will now try to recreate it and post further.

ps I will make it clear in future posts if I am using myDict’s or NSNull’s.

I am not sure whether it is good or bad that there are so many correct syntax methods.

Thanks

Terry