开发者

Chaining NSManagedObject accessors: is this bad practice?

Sorry, this is going to be a little vague but here goes:

I remember reading once that you should not do the following to traverse NSManagedObject relationships, as the cascading can end up with part of your chain to NIL:

self.roomItemInstance.roomInstance.report

I've found that parts of the chain can indeed be set to NIL after a cascade as the result of a delete action etc, and this is c开发者_开发百科ausing subtle problems in my code.

What I want to know is, am I right to think that this is bad practise, and if so what do you recommend instead of this daisy chaining?

Thanks in advance


That's a perfectly acceptable way of traversing an NSManagedObjects properties as far as I can tell. I believe Apple actually recommends using these accessors (either dotted, as you've shown, or bracketed [[object property] property]) as opposed to the typical alternative 'getValue forKey' when accessing attributes/properties.


I believe it's ok, because of the Objective C nil object's capability to respond to selectors. If any of the relationships in the chain returns nil, you will be sending getter selectors to nil for the remaining relationships, which will automatically return nil. Apple actually encourages you to do it the above way because they want to say, "See, isn't Objective C cool? You couldn't do that in other languages."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜