Objective-c about object reference counts in non-arc mode

Source: Internet
Author: User

Objective-c is a simple, powerful and flexible programming language with both object-oriented features and functional programming features. Because it is a C-language vest, that is, objective-c can convert its source code into a pure C language and then compiled into the final target code, so we can also use it to write pure C language code, it is fully compatible with C! (This is different from the C + + features that are compatible with the syntax features)

Because there are many objective-c enthusiasts in the ARC mode objective-c feel very confused, so I hope to learn more about the traditional non-arc mode of programming rules. By objective-c The working mode of the non-arc mode, we can even have a deeper understanding of the operational core of the entire cocoa framework. Why am I not recommending the use of Arc mode?

你用了ARC就得去记__strong、__weak、__unsafe_unretained、__autoreleasing、__bridge等等杂七杂八的关键字~这些乱七八糟的概念本身会把你搞晕,而且当你半懂不懂的时候一旦乱用反而会产生各种奇怪的bug~这些玩意儿倘若充斥在你的代码中,一来很丑,二来对于一些新手很容易被弄晕……所以说,ARC这货自其出生就带来了许多灾难!

Instead of the traditional non-Arc mode, property on a assign, a retain,nsobject call Retain/release and Autorelease method ~ and Apple's rules are very simple--" You do not create it, you do not need to release it, you create it to release it. "This sentence will solve all the problems ~

In addition, whether you use ARC or non-arc, you need to understand the Apple Cocoa Framework's message loop mechanism, that is, how autorelease works. Otherwise, the Objective-c object of your assign or weak property will not be known when it is released.

In summary, if for programming convenience, maintainable, extensible, we can completely turn off the ARC compilation option! In addition, "method invocation" is often described as "message sending" in Objective-c. For example, [obj MSG] is generally described as an Obj object calling its MSG member method. In formal terms, it should be described as sending MSG messages to obj objects. In which object's method executes this statement, the object is called the message sender, and the MSG is referred to as a message (that is, a method) and obj is called a message receiver. Speaking so much, the following begins to cut to the chase!

In objective-c based on the FOUNDATION/COCOA framework, we define a class that is often required to inherit the base class of the NSObject Foundation. When we call NSObject's Alloc class method, the object to be created is allocated storage space, and then the Init member method of NSObject is called to initialize the created object. The reference count for this object is set to 1. Unlike traditional C + +, objective-c based on the FOUNDATION/COCOA framework, it comprehensively determines its lifecycle by specifying a reference count for each object. When the reference count of an object is reduced to 0 o'clock, the Dealloc member method that invokes the object is triggered. The Init method above has set the reference count of the object to 1. When we call the release member method of NSObject, the reference count of the object is reduced by 1. When we call NSObject's retain member method, the object's reference count is added by 1. Normally, we don't have to rewrite NSObject's release and retain member methods ourselves.

Objective-c about object reference count in non-arc mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.