Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Fix crash when inserting nil as KVC value #11

Closed
wants to merge 1 commit into from

Conversation

R1ckye
Copy link

@R1ckye R1ckye commented May 16, 2014

When mapping the values, Motis' validator returns that nil is valid (and it might be the case somewhere) but when you are calling setValue:forKey: value can't be nil, so in this case we have to check for that too. If it gets through as nil it's simply going to crash the app.

When mapping the values, Motis' validator returns that nil is valid (and it might be the case somewhere) but when you are calling setValue:forKey: value can't be nil, so in this case we have to check for that too. If it gets through as nil it's simply going to crash the app.
@vilanovi
Copy link
Contributor

Hello R1ckye,

KVC methods setValue:forKey: can handle nil values if the property for the given key is of type "object". Therefore, if you do [myInstance setValue:nil forKey:@"objectPropertyKey"] KVC will nilify the property objectPropertyKey without any problem.

However, what KVC doesn't support (and throws an exception that may crash your app if you are not caching it) is if you set nil via KVC to a property of any non-object types (as integers, structs, enums, etc). To handle those cases you must override the method -setNilValueForKey: in your objects and "nilify" non-object properties for the given keys (i.e. reset to a default value because non-object types cannot be nil). You can read more about here.

To get a bigger understanding of why Motis nilify values you must know that Motis deserializes JSON using NSJSONSerializationclass. Therefore null JSON values are be converted into [NSNull null] and when Motis finds a NSNull instance automatically converts it to nil and proceeds to nilify the corresponding property via KVC.

This particular case is already documented in Motis and being tested in the unit tests you will find in the Motis sample project.

Moreover, if your crash is not induced because a KVC nilification of a non-object property type, please, explain more in detail your case so I can reproduce it and fix it.

Thanks,

Joan

@R1ckye
Copy link
Author

R1ckye commented May 16, 2014

Hi Joan,

Thanks for your quick reply.
You are right, the property of my key should be a float and I get nil instead of it so it does crash.
However, I think Motis should check for nil, because otherwise all apps using this would have to implement the -setNilValueForKey: in objects using Motis' mapping on a non-object key.
Or maybe more elegant ways would be to either check (somehow) for the supposed type of the key's property and if it isn't an object, then don't try to insert it or simply put it in a try-catch.

Thanks,
Richard

@vilanovi
Copy link
Contributor

Hello Richard,

When implementing Motis support for properties nilification I followed the KVC pattern. Therefore, it is the developer responsibility to handle basic-types nilification and override and implement the setNilValueForKey: method. Otherwise KVC will raise an exception, which is absolutely normal as it is undefined how to nilify a non-object-type property.

Exceptions are very useful to detect undefined or incorrect behaviours. In this particular case, KVC is responsible of assigning the new nil value to a property and it is the developer's fault (and not Motis) to not handle basic-types nilifications. Motis responsibility is only to convert, validate and redirect JSON values into Objective-C object instances. �Therefore, I consider valid an exception warning the user that KVC can't handle a specific case, which is the developer who has forgotten to handle.

Thanks,
Joan

@vilanovi vilanovi closed this May 16, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants