Objective-C Basics

Source: Internet
Author: User
# Import <Foundation/Foundation. h> int main (INT argc, char * argv []) {@ autoreleasepool {nslog (@ "Hello world! ");} Return 0;} the class and method in C ++ have determined the objective -- V Class-> message during the compilation period. If no exception is thrown, for example: [OBJ method: argument]; ===" [car fly]; advantages and disadvantages: C ++ supports multi-inheritance, function calling is very fast, and the compilation period is determined, disadvantages: dynamic binding of objective-C is not supported. Any data can be transmitted to the class without checking the types one by one. Nil does not crash the interface section and clearly defines the class name, instance variable and method. Use the keyword @ interface as the beginning of the section and @ end as the end section. @ Interface myobject: nsobject {int membervar1; // actual change ID membervar2;} + (return_type) class_method; // type method-(return_type) instance_method1; // actual method-(return_type) instance_method2: (INT) P1;-(return_type) instance_method3: (INT) P1 andpar: (INT) P2; @ end the +/-sign in front of the method indicates the type of the method: the plus sign (+) indicates the class method (classmethod), which can be called without the entity, similar to the static member function of C ++ ). Minus (-) is the general entity method ). Parameter transfer: Objective-C uses (:) to pass Parameters. Advantage: it makes it unnecessary for a parameter to be plural at the end of a method call, improving the readability of the Code: For example:-(void) setcolortored: (float) Red Green: (float) Green Blue :( float) blue;/* declaration method */[mycolor setcolortored: 1.0 Green: 0.8 Blue: 0.2]; /* Call Method */The full name of this method is setcolortored: Green: Blue :. Each colon is followed by a float parameter, which represents red, green, and blue. Implementation implementation section describes the actual running code of the method. Start with the keyword @ implementation and end with @ end. @ Implementation myobject + (return_type) class_method {.... // method Implementation}-(return_type) instance_method1 {....} -(return_type) instance_method2: (INT) P1 {....} -(return_type) instance_method3: (INT) P1 andpar: (INT) P2 {....} @ end: to create an object in objective-C, you need to use two messages: alloc and init. Alloc is used to allocate memory space, and init is used to initialize objects. Both init and alloc are class methods defined in nsobject. When the object receives the two messages and responds correctly, the new object is considered ready. The following is an example of myobject * My = [[myobject alloc] init] // 1.0 2.0 myobject * My = [myobject new] // The init method can be rewritten for custom initialization. -(ID) Init {If (Self = [Super init]) {// The method of the parent class must be called // do someting here ....} return self;} (Note: all methods that must be implemented by interfaces in Java, which can be selectively implemented after 2.0 in O -- c) dynamic type: the following three declarations provide a more explicit type information. These three declarations are equivalent at runtime, but the additional type information allows the compiler to check the variable type during compilation and give a warning when the type does not match. -Setmyvalue :( ID) Foo; this Declaration indicates that "foo" can be an instance of any class. -Setmyvalue :( id <aprotocol>) Foo; this Declaration indicates that "foo" can be an instance of any class, but it must comply with the "aprotocol" protocol. -Setmyvalue :( nsnumber *) Foo; this statement indicates that "foo" must be an instance of "nsnumber. /** Type consistency, but dynamic language does not have this problem at all. [Edit] forwarding objective-C allows a message to be sent to an object regardless of whether it can respond. In addition to responding to or discarding a message, an object can also forward the message to an object that can respond to the message. Forwarding can be used to simplify a specific design mode, such as observer mode or proxy mode. The objective-C Runtime defines a pair of methods in the object: The forwarding method:-(retval_t) Forward :( SEL) SEL :( arglist_t) ARGs; // with GCC-(ID) forward :( SEL) SEL :( marg_list) ARGs; // with next/Apple systems response method:-(retval_t) performv :( SEL) SEL :( arglist_t) ARGs; // with GCC-(ID) performv :( SEL) SEL :( marg_list) ARGs; // with next/Apple systems only needs to overwrite the above methods to define the forwarding behavior of objects to be forwarded. You do not need to override the response method performv: Because this method only sends messages to the response object and Passes parameters. The SEL type is the message type in objective-C. Not very familiar: */Dynamic Features: supports overwriting and adding new methods of dynamic class methods at runtime, and does not need to re-compile objective-C ++ as an extension of C ++, similar to objective-C, the extension of C has the following restrictions due to the absence of special efforts in integrating the features of C ++ and objective-C: c ++ classes cannot inherit from objective-C classes, and vice versa. The objective-C definition cannot define the C ++ namespace. The member variables of the objective-C class cannot include C ++ class objects that do not contain Default constructors and/or virtual methods, however, the use of C ++ class pointers does not have such restrictions (you can initialize it in the-init method ). The "pass value" feature of C ++ cannot be used on objective-C objects, but can only pass its pointer. The objective-c Declaration cannot exist in the C ++ template declaration, and vice versa. However, the objective-C type can be used in parameters of the C ++ template. Objective-C and C ++ have different error handling statements, and their respective statements can only handle their own errors. If the objective-C error causes the C ++ object to exit, the C ++ destructor will not be called. The new 64-bit runtime solves this problem. The release of objective-C 2.0 adds "Modern garbage collection and syntax improvement [3], runtime Performance Improvement [4] garbage collection objective-C 2.0 provides an optional garbage collector. In backward compatible mode, the reference count operations such as "retain" and "release" are changed to "no operation" when the objective-C runs. When garbage collection is enabled, all objects are the working objects of the collector. The common C pointer can be modified with "_ strong", marking that the object pointed to by the pointer is still in use. The pointer marked as "_ weak" is not included in the collector's count and rewritten as "nil" when the object is recycled ". The objective-C 2.0 implementation on IOS does not contain the garbage collector. The garbage collector runs in a low-priority background thread and can be paused during user action to maintain a good user experience. [5] attribute: the attribute is always public and aims to provide internal variables for external class access (or read-only) classes. The attribute can be declared as "readonly", that is, read-only. You can also provide storage methods including "Assign ", "copy" or "retain" (simple assignment, copying, or adding 1 reference count ). The default attribute is atomic, that is, the lock is applied during access to avoid multiple threads accessing the same object at the same time. You can also declare the attribute as "nonatomic" (non-atomic) to avoid locks. Compared to using nsenumerator objects or listing in turn, objective-C 2.0 provides a fast enumeration syntax. In objective-C 2.0, the following loop functions are equal, but the performance characteristics are different. // Use nsenumeratornsenumerator * enumerator = [thepeople objectenumerator]; person * P; while (P = [enumerator nextobject])! = Nil) {nslog (@ "% @ is % I years old. ", [p name], [P age]);} // use enumeration for (INT I = 0; I <[thepeople count]; I ++) {person * P = [thepeople objectatindex: I]; nslog (@ "% @ is % I years old. ", [p name], [P age]);} // use the quick enumeration for (person * P in thepeople) {nslog (@" % @ is % I years old. ", [p name], [P age]);} quick enumeration can generate more effective code than standard enumeration, the enumerated method is replaced by the pointer arithmetic operation provided by the nsfastenumeration protocol. [6]

 

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.