Objective-C learning notes-Part 1-object, type, message

Source: Internet
Author: User

The content of this series corresponds to the content described in the official objective-C language, which is divided into the following parts:

    1. Object, type, message
    2. Define a class
    3. Allocate and initialize memory for objects
    4. Protocol
    5. Attribute Declaration
    6. Classification and Expansion
    7. References
    8. Quick Enumeration
    9. Implement Static Behavior
    10. Selector
    11. Error Handling
    12. Thread

In general, objective-C has many commonalities with mainstream object-oriented languages such as C # And java. The core of objective-C is a dynamic runtime system built on C, which helpsProgramRealize tasks such as dynamic type estimation and garbage collection. Similar to ironpython, a static language is used for dynamic runtime.

The first part of the note is as follows:

An object-oriented language
An object library
A set of development tools
One Runtime

In the instance object, variables are private. If you need to access them, you need to provide a dedicated access method.

The object ID is an independent data type ID.

Either the instance object or the class itself may have an ID

Id anobject;

In the object-oriented method return, Id replaces int with the default return value type. In strict C results, Int Is still used.

Nil ID is 0

ID, nil, and Other OC types are defined in the objc/objc. h file.

The ISA pointer Pointer Points to an instance of the class of an object.

ID is only valid for the runtime object, and its value is "dynamic". Therefore, it cannot obtain more information during compilation.

The dynamic type system is called "dynamic binding" in OC.

Some data structures are stored in the ISA variable during runtime. Therefore, you can execute operations such as whether an object implements a method or obtains its parent class.

Class is a special object that provides the implementation of static methods.

About memory management:

OC provides two mechanisms for memory management:

1. Reference count
2. Garbage Collection

Object message:

To let an object execute a function, the method is to pass a "message" to it. format:
[Receiver message];

Because the method name is usually used to select the implementation of a method, the method name in the message is generally called "selector"

Example:
[Myrectangle setorig.pdf: 20.0 Y: 50.0];
In this example, the selector is setoriginx INX: Y: note that it contains a colon, but does not specify the return value.

In oC, the parameter name cannot be omitted, the order cannot be changed, and the name parameter cannot be used as in Python.

The required parameter is a colon, and the optional parameter is a comma.

You can send a message to nil without reporting an error. This is why the message is used instead of "method call", because nil can choose not to accept the message, this is not to say that you don't have to worry about calling a method on an empty object and causing an error?

The three paragraphs below are important:
If the method returns any pointer type, any integer scalar of size less than or equal to sizeof (void *), a float, a double, a long double, or a long, then a message sent to nil returns 0.
If the method returns a struct, as defined by the Mac OS X Abi function call Guide to be returned in registers, then a message sent to nil returns 0.0 for every field in the struct. other struct data types will not be filled with zeros.
If the method returns anything other than the aforementioned value types, the return value of a message sent to nil is undefined.

The method is used to obtain the instance variable access permission of the message receiver.

Polymorphism

Because the method belongs to an object, the message result depends on the implementation of the method of the current object.

Dynamic binding

The biggest difference between a method call and a message mode is that in a method call, the relationship between the method and its call object is fixed, but not in message transmission, it needs to decide which method to call at runtime.

When an object receives a message, the message will scan the method table of the object, find the matching method, and call it, and pass a pointer to the receiver's sock instance variable to it (this method)

Messages Sent can even be dynamic.

Dynamic Parsing
It allows you to implement a class or instance method at runtime.

"." OPERATOR:
OC provides the "." operator to access accessor Methords:
Myinstance. value = 10; at this time, it is not really "accessing an attribute", but a syntactic sugar. The actual function is similar to [myinstance setvalue: 10];

For example, you need to access the its own instance variable of an object:
Self. Age = 10;

How to understand an object "its own attributes "?
That is, it does not have the attributes discussed in the class to which it belongs. Because there is no accessor method for this attribute, you must use the self. xxx method to access it.

Graphic * graphic = [[graphic alloc] init];
How to understand this sectionCode?
Whether it is equivalent to calling a method alloc on graphic class to generate an instance, then calling the init method of this instance, and finally storing the reference in a pointer.

Do not use "." to access non-attribute Methods

Do not use "." To access the read-only attribute. Therefore, you may incorrectly assign a value to it and cause a runtime error.

About classes
The constructor of each class is equivalent to a static method. It is responsible for generating instances of this class. The instance methods are shared, but they have their own instance variables.

Cocoa provides more than 250 classes, some of which can be used directly, and some of which need to be compiled as needed for necessary extension.

Writing a basic class is a meticulous tool. Instead of writing a basic class, you should use nsobject

Abstract class does not force you to instantiate it, but you should not.

You can use static variables to implement the singleton mode.

The initialization method is provided to execute an instance of the type during initialization. To ensure that it is executed only once, you need to write it like this:
+ (Void) initialize
{
If (Self = [thisclass class]) {
// Perform initialization here.
...
}
}
In this case, it is hard to say that if a method is called on an object, the method of the object and the method of the same name of its parent class will be called in turn? To be confirmed.

The following is true:
[Object Class]! = Object_getclass (object )! = * (Class *) object)

Test object type:
If ([objecta class] = [objectb class]) {//...

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.