"OC strengthening" OC programming language strengthening knowledge points simple induction--Understanding the use of OC lift capacity

Source: Internet
Author: User

(1) #import <Foundation/Foundation.h> The Foundation.h is the framework master header file, which is equivalent to having copied all the header files under the foundation framework and needs to import the header file before the NSLog can be used properly.


(2) The frame is stored in/applications/xcode.app/contents/developer/platforms/iphoneos.platform/developer/sdks/iphoneos.sdk/ System/library


(3) An upgraded version of #import是 # include, which prevents duplicate imports.


(4) NSLog already contains auto wrap.


(5) The length of the nsstring in OC is calculated as the number of words rather than characters, such as @ "123 haha", the length is 5. The length value in the C language is 9 because a Chinese character is 3 characters long.


(6) The object method is preceded by-, the class method is preceded by +. Class methods do not have access to member variables (instance variables), and if the method does not require access to the instance variables, it is efficient to use class methods. Class methods can only be called with a class, and object methods can only be called with objects. The class method cannot call the other method, and the object method can call the class method. Class methods can call class methods. Class methods and object methods can have the same name.


(7) An anonymous object is available when the object needs to be called only once. An anonymous object can be passed as an argument.


(8) member variable one of the benefits of starting with an underscore: Enter the underline below to get a hint.


(9) Self represents a class in a class method and represents an object in an object method.


(10) If a method with the same name as the parent class exists in the subclass, it is called overriding the method. However, a subclass cannot have the same member variable as the parent class.


(one) Super is generally the thing that tells the program to call the parent class explicitly. Usage scenarios: It is generally necessary to rewrite (add enhanced) The parent class method in the subclass, but all the original parent code can be used, at this time [super method name], the parent class of the code to get over, and then add the other new code, the subclass of the method of rewriting.


The description method is used to output all member variables of an object, and of course it can be customized.


(13) There is no polymorphism without inheritance. Polymorphism is the object that points to the child class with a pointer to the parent class. If the subclass overrides the method of the parent class, it involves dynamic binding as the title, and if the subclass has an override, then although the parent pointer points to the subclass object, the program will determine the actual type of the object (that is, the object pointer declared with the parent class) at run time, so the method of the subclass is called. Only methods of the parent class can be called unless the child class has not overridden the method.


(14) After 13, using the polymorphic declaration, if the parent class does not have a subclass of the method, an error will be received. This is not supported by the OC language, but is not supported by Xcode. If we are going to continue to use it, we need to cast, convert the object declared by the parent class to the object declared by the child class, and then call the method.


(15) Dot syntax is created by OC in order to cater to Java class programmers.


(16) @protected and @public member variables of the parent class can be accessed in subclasses, but @private member variables cannot be accessed.


(17) The nature of a class is a class. When creating an object, the program first loads the code into the code area, then creates the class object in the heap, the sel of the class object points to the method in the code area, and then creates an object with the class object, which Isa points to the class object and holds the object pointer in the stack, pointing to the object.


class xxx=[classes); This method is the class object that gets the class, and XXX is the class object.


(+) person *p1=[person new]; here P1 and self are object pointers, and the addresses they point to are the addresses of the ISA.


(20) Getter and setter methods can be generated automatically through @property and @synthesize, but if you want to control the rationality of input data, that is, setter assignment is reasonable, you can override setter method. And when you manually set the setter and getter methods at the same time, the member variables are not automatically generated for you. However, in general, we do not manually modify the getter method, so the two methods are not generally overridden at the same time.


(21) After accepting an object with a universal pointer ID, you do not need to cast the ID type to invoke the method of the object.


(22) When overriding the Init method, you need to call Self=[super init]; it is a fixed notation. And you should add a second judgment if (Self!=nil) {}, that is, only the parent class initializes the normal non-undo nil to continue execution. Memorize it. The optimization notation is if (self=[super init]) {}. Generally overriding Init, it seems to give certain properties a fixed value.


(23) Classes cannot be #import imported in. h files. But you can take advantage of @class person, instead (the statement simply tells the compiler that the person is a class, so it doesn't get an error), and the latter is more efficient and is used when developing large projects. Sometimes in. h files, use @class * * *, #import "***.h" in. m files, because the method of that class cannot be used in. m if it is not imported.


(24) So-called memory management: The basic data types are stored in the stack, the end of the scope is automatically invalidated. The object pointer is also stored in the stack, but the object pointer points to an object that exists in the heap, and if no memory is reclaimed before the scope ends, an object that is not reclaimed in the heap will continue to occupy memory if the object pointer is invalidated.


(25) When an object is recycled, the Dealloc method of the class is called, and we can override this Dealloc method. When overridden, it is customary to add [super Dealloc] to the final statement.


(26) When the pointer to the object reference count is 0 o'clock will be destroyed, then this pointer is a wild pointer, because it points to a nonexistent memory space.


(27) Generally if there is a new pointer to an object, you should use retain as the object reference count plus 1, otherwise direct point, there will be 2 pointers to the object, but its reference count only 1,release once error.


(28) If the code is rigorous, the corresponding object pointer is set to nil null after the object pointer is release.


(29) Use @property time () Inside: If the OC object type is used Retain/copy, the basic data type is used assign. is to use memory management rules.


(30) The system comes with the method: If not contain alloc, copy and so on, then their returned objects have been autorelease. We can quickly create a Autorelease object when we create the object so that others don't have to think about release when they call our object, creating the best use of self without the class name.


ARC rule: Destroy this object as long as there is no strong pointer pointing to it. By default, all pointers are strong pointers, and arc automatically removes weak pointers, despite weak pointers, if no strong pointers are pointed.


OC Object type applies to strong pointers, equivalent to retain. The base data type is suitable for weak pointers, which is equivalent to assign, so used in @property (): With strong or assign.


(33) If the code for the previous project needs to be converted to arc, use the following method. If the imported third-party library has non-arc code, it can be set to retain non-arc.



(34) If you want to loop a reference, one variable is set to strong and the other is set to weak, otherwise an error occurs. At non-arc, one is set to retain and one is set to assign.


(35) Apple's official recommendation is to use block encapsulation code, which is labeled ^, which is similar to defining variables, called calls and function calls. can have parameters and return values, and so on. In general, you cannot modify a local variable inside a block, but if you add the __block keyword to a local variable, it can be modified in the block.

#import <foundation/foundation.h>int Main (int argc, const char * argv[]) {    @autoreleasepool {        int (^block1 ) (Int,int) =^ (int a,int b) {            return a+b;        };        NSLog (@ "%i", Block1 (5,6));    }    return 0;}

(35) class has a base class, the same as the agreement, there is a base protocol, that is, the protocol to abide by the agreement.


(36) It is possible to require this variable to conform to a protocol when defining a variable, Format: class name < protocol name > * variable name, but when the class name is an ID, there is no need to precede the variable name *. Similarly, when @property declares a property, it can also require that the attribute conform to a protocol, format: @property (Nonatomic,strong) class name < protocol name > * variable name, and the same time when the ID is removed.

"OC strengthening" OC programming language strengthening knowledge points simple induction--Understanding the use of OC lift capacity

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.