NSNull 处理 NSManagedObject 属性值
我正在为 NSManagedObject
的属性设置值,这些值来自从 JSON 文件正确序列化的 NSDictionary
。我的问题是,当某个值为 [NSNull null]
时,我无法直接分配给该属性:
fight.winnerID = [dict objectForKey:@"winner"];
这会引发 NSInvalidArgumentException
"winnerID"; desired type = NSString; given type = NSNull; value = <null>;
我可以轻松检查该值[NSNull null]
并分配 nil
:
fight.winnerID = [dict objectForKey:@"winner"] == [NSNull null] ? nil : [dict objectForKey:@"winner"];
但我认为这并不优雅,而且需要设置很多属性,而且会变得混乱。
另外,在处理 NSNumber
属性时,这会变得更加困难:
fight.round = [NSNumber numberWithUnsignedInteger:[[dict valueForKey:@"round"] unsignedIntegerValue]]
NSInvalidArgumentException
现在是:
[NSNull unsignedIntegerValue]: unrecognized selector sent to instance
在这种情况下,我必须处理 [dict valueForKey:@"round"]< /code> 之前为其创建
NSUInteger
值。而单行解决方案已经消失了。
我尝试创建一个 @try @catch 块,但是一旦捕获第一个值,它就会跳转整个 @try 块,并且下一个属性将被忽略。
是否有更好的方法来处理 [NSNull null]
或者使这完全不同但更容易?
I'm setting values for properties of my NSManagedObject
, these values are coming from a NSDictionary
properly serialized from a JSON file. My problem is, that, when some value is [NSNull null]
, I can't assign directly to the property:
fight.winnerID = [dict objectForKey:@"winner"];
this throws a NSInvalidArgumentException
"winnerID"; desired type = NSString; given type = NSNull; value = <null>;
I could easily check the value for [NSNull null]
and assign nil
instead:
fight.winnerID = [dict objectForKey:@"winner"] == [NSNull null] ? nil : [dict objectForKey:@"winner"];
But I think this is not elegant and gets messy with lots of properties to set.
Also, this gets harder when dealing with NSNumber
properties:
fight.round = [NSNumber numberWithUnsignedInteger:[[dict valueForKey:@"round"] unsignedIntegerValue]]
The NSInvalidArgumentException
is now:
[NSNull unsignedIntegerValue]: unrecognized selector sent to instance
In this case I have to treat [dict valueForKey:@"round"]
before making an NSUInteger
value of it. And the one line solution is gone.
I tried making a @try @catch block, but as soon as the first value is caught, it jumps the whole @try block and the next properties are ignored.
Is there a better way to handle [NSNull null]
or perhaps make this entirely different but easier?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果将其包装在宏中可能会更容易一些:
然后您可以编写类似的内容
或者您可以预处理字典并在之前将所有
NSNull
替换为nil
甚至尝试将其填充到您的托管对象中。It might be a little easier if you wrap this in a macro:
Then you can write things like
Alternatively you can pre-process your dictionary and replace all
NSNull
s withnil
before even trying to stuff it into your managed object.好吧,我今天早上刚醒来就找到了一个很好的解决方案。怎么样:
使用接收可变数组和字典的选项序列化 JSON:
从 leafDict 中获取一组具有
[NSNull null]
值的键:从 Mutable leafDict 中删除过滤的属性:
现在当您调用
fight.winnerID = [dict objectForKey:@"winner"];
WinnerID 将自动为(null)
或nil
与
或[NSNull null]
相反。与此无关,但我也注意到,在将字符串解析为 NSNumber 时最好使用 NSNumberFormatter,我所做的方法是从 nil 字符串获取 integerValue,这给了我一个不想要的
0
NSNumber,而我实际上希望它为零。之前:
之后:
Ok, I've just woke up this morning with a good solution. What about this:
Serialize the JSON using the option to receive Mutable Arrays and Dictionaries:
Get a set of keys that have
[NSNull null]
values from the leafDict:Remove the filtered properties from your Mutable leafDict:
Now when you call
fight.winnerID = [dict objectForKey:@"winner"];
winnerID is automatically going to be(null)
ornil
as opposed to<null>
or[NSNull null]
.Not relative to this, but I also noticed that it is better to use a
NSNumberFormatter
when parsing strings to NSNumber, the way I was doing was gettingintegerValue
from a nil string, this gives me an undesired NSNumber of0
, when I actually wanted it to be nil.Before:
After:
我编写了几个类别方法,以便在使用之前从 JSON 生成的字典或数组中去除空值:
如果标准 JSON 解析库默认具有此行为,那就太好了 - 忽略空对象几乎总是比包含空对象更可取作为 NSNull。
I wrote a couple of category methods to strip nulls from a JSON-generated dictionary or array prior to use:
It would be nice if the standard JSON parsing libs had this behaviour by default - it's almost always preferable to omit null objects than to include them as NSNulls.
另一种方法是
在这种情况下,您可以
在标头 NSKeyValueCoding.h 中定义“值为 NSNull 的字典条目导致
-setValue:nil forKey:key
消息发送到接收者。缺点是您必须将字典中的任何键转换为接收器中的键。
Another method is
In this scenario you could do
In the header NSKeyValueCoding.h it defines that "Dictionary entries whose values are NSNull result in
-setValue:nil forKey:key
messages being sent to the receiver.The only downside is you will have to transform any keys in the dictionary to keys that are in the receiver. i.e.
我遇到了同样的问题,找到了这篇文章,以稍微不同的方式做到了。仅使用类别 -
为“NSDictionary”创建一个新的类别文件并添加这个方法 -
稍后在代码中使用它,用于属性可以有 NSNULL 就这样使用它 -
就是这样
I was stuck with the same problem, found this post, did it in a slightly different way.Using category only though -
Make a new category file for "NSDictionary" and add this one method -
Later on to use it in code, for properties that can have NSNULL in them just use it this way -
Thats it