ARC, worth it or not?

前端 未结 7 1616
没有蜡笔的小新
没有蜡笔的小新 2021-02-05 09:53

When I moved to Objective C (iOS) from C++ (and little Java) I had hard time understanding memory management in iOS. But now all this seems natural and I know retain, autoreleas

7条回答
  •  Happy的楠姐
    2021-02-05 10:19

    Here's what you really need to know about ARC:

    The compiler understands Objective-C and Cocoa better than you. I don't mean this as an insult; it understands it better than me. I think you could safely say it understands the rules better than all but maybe a dozen people worldwide. And it knows tricks to use them to a degree that you and I can't repeat, even if we understood as well as it does.

    The rest is just details:

    • You will write a lot less boring code. Code so boring it's easy to make mistakes.
    • As a blended compile time and run time process, it has access to tricks that you don't.
      • It will a better a job of writing memory management code than you can, even if you write the theoretical perfect memory management code.
      • It will reduce "high tide" memory usage (somewhat) without any effort on your part.
      • With zeroing weak references, it's much easier to avoid crashes caused by dangling pointers.
    • If you are starting a new application, stop thinking about it and just use it.
    • If you have an existing application:
      • You will need to re-test it. You need to make sure you have no circular references.
      • If you have an existing application that targets iOS before iOS 5, zeroing weak references are not supported. You should seriously consider requiring iOS 5.
      • If you have an existing application that targets iOS before iOS 4, you can't use it at all. What are you thinking, supporting crap that old?!?
    • In the last version of Xcode, it was not entirely bug free. It probably still isn't. But it's still worth using.

提交回复
热议问题