Use of square brackets [] for objective-C understanding

Source: Internet
Author: User

1. It is used to notify an object of what to do, that is, the notification object executes an action.

1.1. in objective-C, square brackets also have other meanings: they are used to notify an object of what to do.

For example:

[Shape draw];// This statement indicates that the shape of the notification object is used to execute the draw action. For example, a rectangle is used to draw a rectangle.

1.2. In objective-C, the notification object performs an operation, which is called sending a message.

For example:

[Shape draw];// Send a draw message to the object shape.

2. Create a new object

To create a new object, we need to send a new message to the corresponding class.

After receiving and processing the new message, this class will get a new object instance that can be used.

For example:

Id shape [3];

Shape [0] = [circle new];// Send a new message to the Circle class. A new object is created and assigned to shape [0].

3. Send messages to the superclass in the class

3.1 for example:

@ Interface circle: Shape

@ End // circle

@ Implementation circle

-(Void) setfillcolor :( shapecolor) c {

If (C = kredcolor ){

C= Kgreencolor;

}

[Super setfillcolor: C];
// Send a message to the superclass shape. The superclass will execute its setfillcolor method.

}

@ End // circle

3.2 for example:

Self = [Super init];// Enables the superclass nsobject to complete its initialization. And if a new object is returned,

// Update self.

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.