Introduction to classes and objects in Objective-c

Source: Internet
Author: User
Tags shallow copy

Knowledge points

1. Introduction to Classes

A class is the type of an object that is an instance of a type.

Instance variables: Used to distinguish between different objects of the same class, which are used to describe objects. Instance variables can be of various types: basic data types, pointers, composite types, and other classes.

Instance method: A function used to manipulate an object of a class, and an instance method is to give an external access interface.

In OC, use #import to include the header file. Prevents header files from being included repeatedly.

2. Declarations of classes

@interface Classname:parentclass<protocol,.. >

{

    • Declaration of an instance variable

}

Method declarations//Declaration of methods

@end

3. Declaration of methods:

Mtype (returntype) Name: (type) param name1: (type1) param1 ....

Method type return value type label name parameter type parameter and first name

Example:-(ID) initwithx: (int) _x AndY: (int) _y;

      • + (int) allnum;

Object method:-Begins with an object that can be called only by an instance of the object, which accesses instances of the current object.

Class method: The + sign can only be called by a class, and the instance variable cannot be accessed in a class method.

4. Messaging mechanisms

General format for sending messages in OC: [receiver message];

[Instance object instance method];

[Class-Class method];

5. Attribute declarations

The syntax for declaring a property is: @property (parameter 1, parameter 2) type name;

Example: @property (nonatomic, retain) Circle *circle;

The parameters are divided into three main categories:

Read-Write properties: (readwrite/readonly)

Setter semantics: (assign/retain/copy/strong/weak)

Atomicity: (atomic/nonatomic)

Each parameter has the following meanings:

ReadWrite: Producing Setter/getter method;

ReadOnly: Only the Getter method is generated;

Assign: The default type, the setter method in the instance variable directly assigned value, do not perform retain operation, the main operation of the basic data type.

Retain: Shallow copy, copy the memory address of the object, so that the target object pointer and source object point to the same piece of memory space.

Copy: Deep copy, copy the specific contents of the object to a newly opened space.

New properties in Strong:arc, similar to retain, when multiple pointers point to the same piece of memory, the memory is not freed as long as there is a pointer to that memory.

New properties in Weak:arc, similar to assign, when multiple pointers point to the same piece of memory, the memory is freed as long as one does not point to that memory.

Nonatomic: Disables multi-threading and improves performance.

Atomic: When using multi-threading, to prevent two threads from waiting for each other to cause deadlocks, use atomic, but consume a certain amount of resources.

Introduction to classes and objects in Objective-c

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.