OBJ-C first Knowledge

Source: Internet
Author: User

1, Obj-CCodeThe extension is ". m ".

 

2When obj-C needs to include header files, you can use # include but more commonly use # import (provided by the GCC compiler ), the latter ensures that the header file is only contained once (L is similar to # ifdef in C ).

 

3,Cocoa framework, Refer

Http://www.cocoachina.com/newbie/basic/2009/0611/130.html

 4. Hello objecitve-C

# Import <Foundation/Foundation. h>

Int main (INT argc, const char * arge [])

{

Nslog (@ "Hello obj-C !");

Return (0 );

}

 

Ns in the nslog () function: Cocoa adds the NS prefix to all functions, constants, and types. (Nslog (@ "% d is a % @", A, @ "num ");)

@ "Hello obj-C !" : The feature added by obj-C. The string in double quotation marks contains @, indicating that the referenced string should be processed as an nsstring element of cocoa.

 

5The true and false values of bool in C correspond to the values of yes and no () in cocoa. (Note: In obj-C, the non-zero value is yes, and 1 is not equal to yes)

 

6. ID type

In objective-C, the ID type is a unique data type. Conceptually, an object class similar to Java can be converted to any data type. In other words, variables of the ID type can store any data type object. In terms of internal processing, this type is defined as a pointer to an object, which is actually a pointer to the instance variable of this object. Id refers to any object that inherits the object (or nsobject) class. Note that ID is a pointer, so you do not need to add a star number when using ID. Http://book.51cto.com/art/201105/262238.htm

7. square brackets in obj-C:

Used to notify an object of what to do. In square brackets, the first item is the object, and the rest is the operation (call function) that you need to perform on the object ).

 

8. method declaration and call:

@ Interface circle: nsobject

{

Shapecolor fillcolor;

Shaperect bounds;

} // Create two instance variables

-(Void) setfillcolor: (shapecolor) fillcolor;

-(Void) setbounds: (shaperect) bounds;

-(Void) draw;

@ End // circle

 

 

@ Interface circle: nsobject --- defines the circle class, which is based on nsobject, that is, inheritance.

 

-(Void) draw; the short line above indicates that this is the method declaration in obj-C, which is a way to distinguish between the function prototype and method declaration; void indicates that the return value is null, draw is the method name.

 

-(Void) setfillcolor: (shapecolor) fillcolor;

-(Void) setbounds: (shaperect) bounds;

Both declared methods have a parameter (parameter type) parameter.

For example:

-(Void) setto: (INT) n over: (INT) d

{
Numerator = N;
Denominator = D;
}

[Afraction setto: 100 over: 200]; // call

Note: The method parameter name of objective-C is somewhat odd. The first parameter does not have a parameter name. If it is hard to say yes, it is the method name,

In general, when we see the colon, the parameter name is prior to the colon.

 

For example, a method without a parameter name is defined and called:

 

-(INT) set: (INT) N: (INT) D;

[Afraction set: 1: 3]; // call

Http://www.cnblogs.com/54007/archive/2011/02/10/1950801.html)

@ Interface is the declaration of the class, which is generally in the header file; @ implementation is the implementation of the class, generally in ". in the M file, the next step is the definition of each method. You do not have to define the method in the order of @ interface, or even define the method without the corresponding declaration in @ interface. The format parameter names of @ interface and @ implementation must be the same.

 

9. instantiate an object: Id ivars = [Class New];

 

10Each method call includes a hidden function named self, and the object itself. After using the self parameter, you can find and operate the object data (this pointer ).

 

11. Inheritance: E.g. @ interface circle: nsobject

OBJ-C does not support multi-inheritance. For example, @ interface circle: nsobject, printableobject cannot be recognized by the compiler. However, it can be inherited from other features, such as classification and protocols.

 

12. refactoring: Move and simplify code.

 

13The super in obj-C is equivalent to calling the parent class method, and fully utilizing the superclass code in the rewrite method.

 

14. Description:

-------------------------------------------------------

@ Interface tire: nsobject
@ End

@ Implementation tire

-(Nsstring *) Description
{
Return (@ "I Am a tire ");
}

 

Nslog (@ "% @", Tires );

 

The descriptong method returns the description of the object by default,
Nslog (@ "% @", objecta); this will automatically call the descriptong method of objecta to output the description of objecta,
After we override the descriptong method of a class,
Output nslog (@ "% @", objecta) again;, the descriptong method that we Overwrite will be called to achieve the description of the custom object !!!

 

15. accessor method): The method used to read or change the specific attributes of an object. For example, setter and getter methods (note: the setter method name is usually prefixed with set, E. g. setengine and getter are named based on the returned attribute name. g. engine. because get has a special meaning in cocoa, it means that this method will return a value through the pointer you pass in as a parameter ).

When operating on attributes of other objects, you should always use the access method provided by the object, and never directly change the values of attributes of other objects. For example, the main function should not directly invoke the instance variables of the category class. This is also an indirect embodiment.

 

16. Exit () function: Exit (1) indicates exit after an error occursProgram, Exit (0) indicates normal exit. Parameters 0 and 1 are returned to the operating system. The operating system determines whether the program is executed normally based on the parameters. (That is Defensive Programming)

 

17In obj-C, the interaction between all objects is achieved through pointers.

 

18In some cases, @ class can replace # import to reduce compilation times. e.g. Is Used in header files in composite classes...

 

19, Some useful data types (struct): nsange, nspoint, nssize, nsrect .....

 

20. nsstring Class Method

Create a string+ (ID) stringwithformat: (nsstring *) format ,......;

E.g. nsstring * height;

Height = [nsstring stringwithformat: @ "height is % d feet, % d inchs", 5, 11];

If a plus sign is added when a method is declared, this method is defined as a class method (Factory method ). This method is a Class Object (rather than an Instance Object of the class) and is usually used to create a new instance.

Size-(Unsigned INT) length;

E.g. Unsigned int length = [height length];

Comparison-(Bool) isequltostring: (nsstring *) asting; and The Compare Method

Check whether the string contains other strings: Hasprefix hassuffix

 

Nssting is an unchangeable string type, and its subclass nsmutablestring is a string type that can be repaired.

Nsmutablestring class method:

Create stringwithcapacity. Of course, stringwithformat can also be used.

Add characters: appendstring and appendformat

Delete characters: deletecharactersinrange and rangeofstring

21. nsarray class:

Create arraywithobjects

E.g. nsarray * array;

Array = [nsarray arraywithobjecrs: @ "one", @ "two", @ "three", nil]; (NIL is empty, table list ends)

[Array count] Number of arrays available

[Array objectatindex: I] obtains the content of the array (index traversal)

Split array componentsseparatedbystring

Merge array componentsjoinedbystrring

Variable array nsmutablearray class: addobject, removedobjectatindex

22. nsenumerator

Nsenumerator * enumerator;

Enumerator = [array objectenumerator]; // request the enumerator from the array

Id thingie;

While (thingie = [enumerator nextobject]) {

Nslog (@ "I found % @", thingie );

}

23. Quick enumeration:

For (nssting * string in array ){

Nslog (@ "I found % @", string );

}

24. nsdictionaryDictionary hash associated array: stores a value under a given key (usually nsstring) (which can be any type of object)

Of course, also has nsmutabledictionary

25. nsnumber: Cocoa is used to wrap classes of basic data types.

Nsvalue can wrap any value

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.