Replacement for using nil in dictionaries in objective-C

I work in the IPhone SDK and am pretty new to objective-c. I am currently working with NSUserDefaults to save and restore settings in my IPhone application. To save the created classes, I will encode them in the form of a dictionary, and then save the NSdictionary.

My problem is that I cannot find a reasonable way to store a non-value (IE class variable that is nil) in a dictionary in a reasonable way. To be more specific, let's say I have the Dog class, and it got the tail color NSString *. Suppose I'm trying to save an instance of a dog class without a tail, so the tail for this instance is zero. What is a smart way to keep a dog as a dictionary? This will not allow me to save nil in NSdictionary. @ "is not very good, because if I do if (@" "), @" "is true. I would like it to be false as zero.

Hope my question makes sense and thanks for your help!

+3
source share
4

NSNull. :

[NSNull null]

Archiving, .

+7

, nil objectForKey:. nil , ? objectsForKeys:notFoundMarker:, nil.

, , , .

NSNull, , IMO.

+9
+1

The best solution is not to keep values ​​that are "zero" in your case. When reading, if there is no value for this key, the dictionary will return you zero.

0
source

Source: https://habr.com/ru/post/1723483/


All Articles