Object C learning records

Source: Internet
Author: User

In @ object-C, public, protected, and private are used to control the access permissions of inherited classes. They do not play a role in accessing class members. In C ++ and Java, these three descriptors can also be used to control the access permissions of member variables.

@ Strong type and weak type, strong type variables use class names, and weak type variables use ID instead. Weak types provide more flexibility and dynamic features during runtime, you need to go home on the mechanism to ensure that this is also different from static languages.

@ In object-C, the object reference is called a pointer. Remember to add * to the variable declaring a strong object. The weak type ID itself is a pointer. Two types of methods can be declared: the corpse method and the class method. The entity method is the method that can be executed in an instance of a specific class, and the class method does not need to create an instance, of course, it can also be used after the instance is created.

@ A very strange method appeared. I was so depressed that day, and now I understand it. In general, the declaration of a function includes the return type, the function name and the input parameter type and name. Object-C is similar, but in object-C, function names and parameter types and names may be written separately. For example, the function insertobject: atindex:

-(Void) insertobject :( ID) onobject atindex :( nsuinteger) Index

The function type identifier (class method or instance method) is-, that is, the instance method. The return type is void, and the function name is also called the method signature keyword. Here the insertobject and atindex parts are included, the parameter type is ID and nsuinteger, and the parameter name is anobject and index. Similar structure, but different forms of expression, need to complete the transformation of thinking.

The actual name of the above method is: insertobject: atindex:, which is the link of all signature keywords. The colon indicates the current parameter.

When a method is called, it communicates with the object. The communication content is the parameter information required by the method signature and method. The method call format is as follows:

[Myarray insertobject: anobject atindex: 0]. In brackets, the leftmost object is the object that receives the information. The information includes the parameter on the right, which indicates that the message is sent. insertobject: atindex: go to the object named myarray.

To prevent multiple local variables from saving temporary results, object-C allows you to nest information. The returned values from each nested information can be used as parameters, targets, or other information, you can use information to replace any variables used in the previous example. Therefore, if an object named myappobject is used to access the array object and embed the object into the array, you can write the preceding example as follows:

[[Myappobject thearray] insertobject: [myappobject objecttoinsert] atindex: 0]

Object-C also provides a period to call the accessor method. The accessor obtains and sets the state of an object. The typical format is-(type) propertyname and-(void) setpropertyname :( type ). Using the period syntax, you can rewrite the preceding example:

[[Myappobject. thearray] insertobject: [myappobject objecttoinsert] atindex: 0]

You can also use the period syntax to assign values. The so-called accessor method naturally consists of two parts: storage and retrieval, as shown below:

Myappobject. thearray = anewarray;

Have you found that the language is becoming more and more natural? assignment can also be written:

[Myappobject setthearray: anewarray]

When sending a message, you can send the message to a class instance or class itself. You must specify the instance method or class method as the corresponding method.

The class method includes creating a class instance and sharing information of the class. The only difference between the class method declaration and the class method declaration is the difference of the "-+" number.

Use the class method as the factory method of the class. The following method is the nsarray class method-inherited from nsmutablearray-used to assign and initialize a new instance of the class and return itCodeCentral area.

Msmutablearray * myarray = NULL;

// Create a new array and assign it to the variable myarray

Myarray = [nsmutablearray array];

. H files are interface files, including interface and end, And. M files are implementation files, including implementation and end. The compiler uses this information to locate the closed method of the corresponding class. The method declaration must match the description in the excuse file without containing the content of the code block.

@ Property (readonly) uiview * rootview;

@ Synthesize flag, nameobject, rootview

String:

Nsstring provides object encapsulation strings, which can have all the disadvantages required. It creates memory management for strings of any length and supports multiple formatting tool sets, to quickly create an nsstring for Object-C, you must add the @ symbol before the normal C string.

For example

 
Nsstring * mystring = @ "my string \ n ";
 
Nsstring * anotherstring = [nsstring stringwithformat: @ "% d % @", 1, @ "string"];
 nsstring * fromcstring = [nsstring stringwithcstring: "a c string" encoding: nsasciistringencoding]; 
protocol
the protocol here is like an excuse, it can be implemented by any class, and other objects can be reliably implemented through the top excuse. The implementation method can be used to demonstrate the agreement.
the protocol is similar to an excuse, but the Protocol does not have a parent class and does not define corpse variables.
the application obtains the view through delegation. The implementation of delegation comes from the Protocol requirements. The Declaration contains the view.

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.