This question of mutability…

Hello,

NSNumber, the wrapper class for integers, float, chars etc. is immutable. Ok, it must be a good reason for this. You can’t access the internal value: it is read-only. Attempting to set this value stops you at compile time, with a disgusting, infamous reddish rectangle over the guilty line. Don’t touch this value.

Well, this is pretty-like constants. OK, a bit expensive constants.

Now, if you want to store them in a property list, you can, for example, bind a NSNumber with a boolean value to a checkbox. Plist file is loaded, your property is set to NO, the checkbox is unchecked. Life is wonderful.

Now, you decide this checkbox deserves to be checked, because you really want this option of your app to be active. You check. Then you quit the application. Preferences are saved. All this has a taste of paradise.

When you launch the application again, the checkbox, of course, is checked. Accidents never happen in a perfect world.

But. wait. Does it mean the value of the property has been changed? But you can’t change it, the compiler has said «bad boy, leave this poor boolean value alone», so how, when, at which missed page of the docs could this immutable value be changed?

Thanks for an explanation.

Hi,

NSNumber objects are immutable, but variables and readwrite properties are not.
You can change the value of a variable containing an NSNumber object by assigning a new NSNumber instance


NSNumber *boolValue;
boolValue = [NSNumber numberWithBool:NO];
// some
// lines
// of
// code
boolValue = [NSNumber numberWithBool:YES];

Is it what the framework does when you change a property? Because I never had to re-assigned a property. Everything is AS IF the NSNumber (a true NSNumber, not a key/value into a dictionary) become suddenly mutable.

Well. I solved the problem with a different solution, but imagine you have such a flag (say “recordIsModified”) somewhere into your code. You have to store the property into an instance variable of the “main script”, use it and. then? If you want to store the value into the property, what would you do?

Is it possible to subclass NSNumber or add a category like

NSBoolNumber *myBool;
[myBool switch];

Or [myInteger addOne];

Do such things exist somewhere or are they useless (worse, stupid)?

You’re thinking too complicated.
Use primitives like BOOL and float as much as possible.
NSUserDefaults converts primitives to objects and vice versa automatically.

This is also the moment when custom objects (classes) come in.
You can define a custom class as a replacement for NSDictionary.
The custom class can contain primitive and object properties.
The primitive properties are handled correctly in table views and there are classes (NSCoder) to convert (aka serialize) custom classes to property lists.

NSDecimalNumber can do math operations

[[NSDecimalNumber zero] decimalNumberByAdding:[NSDecimalNumber one]];

As you can see the math operation is done with creating a new instance. This is a normal habit for immutable objects

Praised be it!

That certainly why objects must “conform to coding”.