开发者

Shorthand way of releasing all retained properties?

We all know an object's properties should be released through its dealloc method, but often for objects with many properties this can be pretty cumbersome. It's kind of a headache especially when adding or removing new properties to remember to go back to dealloc and add and remove release calls.

Is there any method of releasing all of an object's properties generically? I wasn't able to find anything while looking through the docs, but could this be done through开发者_如何学Python reflection if it's not already implemented?

I guess another simple option might be to just place all the properties in an array or other container object and always just release the container. Any other options?


I saw one once (and even used it). It involves using the Objective-C Runtime to loop through the properties of a class, check which ones have either a retain or copy flag, and then set them to nil. Then, your -dealloc implementation can be reduced to something like [self cleanupProperties] or something.

The long story short, however, I've stopped using that because of really wacky problems that I can't explain. I don't know for sure that this is what caused it, but it just seems clever enough that it would have some sort of nasty, unforeseen side-effects.

So, in answer to your question: it's definitely possible, but I'd advise you don't. Use garbage collection if possible! :)


Unless you can turn on garbage collection you're pretty much down to two options. As you suggested you could stuff all the property references into a single NSDictionary (which you would release in -dealloc). Otherwise you're stuck with the way it's usually done.

You can see more about garbage collection in Objective-C 2.0 here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜