[OC learning-5] How to instantiate an object in oc? And what is message transmission?

Source: Internet
Author: User

Our previous example is as follows:

 
Asstudent * student = [[asstudent alloc] init]; // instantiate and initialize an object [STUDENT release]; // release the memory occupied by this object

In fact, Both alloc and release are class methods in nsobject. (The front is the plus sign +), so you can call it directly using the class name, such as asstudent alloc.


Of course, the two methods mean to allocate a memory and reclaim a memory: the logic is alloc once, this object counter is + 1, release once, this object counter is-1, when the counter is 0, the system automatically releases the memory.


The above instantiation and object initialization are actually divided into two steps:

// Allocate the memory first, but it is actually a pointer to the memory. Therefore, student adds * to indicate that this object is actually an object pointer, when the two are not specifically declared, they can be called at Will // click to see whether the returned values of alloc and release in the nsobject class are (ID ), that is, common object pointers // common objects exist in the heap zone (stack zone, heap zone, data zone, and read-only zone). Objects in the heap zone cannot be directly accessed without a name, therefore, you need to name a pointer pointing to the object in this heap area. Use the pointer to use the object asstudent * student = [asstudent alloc]; // a pointer is returned later, the previous definition is also a pointer, so after being assigned a value // The memory is allocated, it is usually initialized to the default 0 of the root class, but sometimes we need to initialize it to the desired state, so you need to initialize it yourself. This will be detailed later, because you need to write this init function student = [STUDENT init]; // init is an instance method, so it is called by an object, the return value is also (ID), and the value is assigned to student itself, that is, once initialized.

If we instantiate an object but do not allocate a memory address to it, the object pointer will not point to it.Wild pointerBut if we do not want to give this object any content, we should useEmpty objectAssignment:

 
// NIL is an empty object, while null is a null value. Therefore, nil can only be used and nullasstudent * student = nil cannot be used;

Message Passing) What is it? Let's take a look at what we wrote earlier:

 
// Send a setname message to student, whose content is set to Tom [STUDENT setname: @ "Tom"];

Therefore, message transmission refers to calling a function. However, in OC, messages are actually transmitted, rather than calls. In C ++, they are called. This is generally the form of Student. setname ("Tom "). So pay attention to the following name conversion.

[OC learning-5] How to instantiate an object in oc? And what is message transmission?

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.