1. What is the difference between # import and # include? What does @ Class mean?
@ Class is generally used when the header file needs to declare an instance variable of this class. In the M file, you still need to use # Import
The advantage of # import over # include is that it does not cause Repeated inclusion.
2. How and how to manage the memory of Object-C?
1. when you create an object using the new, alloc, and copy methods, the reserved counter value of this object is 1. when you no longer use this object, you are responsible for sending a release or autorelease message to this object. in this way, the object will be destroyed at the end of its service life.
2. when you obtain an object using any other method, assume that the reserved counter value of this object is 1 and has been set to Auto Release, you do not need to perform any operations to ensure that the object is cleared. if you want to own this object for a period of time, you need to keep it and ensure that it is released when the operation is complete.
3. If you retain an object, You Need To (eventually) release it or automatically release it. You must ensure that the retain method and the release method are used equally.
3. Is there a private Method for Object-C? What about private variables?
There are only two methods in the objective-C-class, static method and instance method. this does not seem to be a complete object-oriented approach. According to the OO principle, an object only exposes useful things. if there is no private method, some small-scale code reuse will not be so easy. name a private method in the class
@ Interface controller: nsobject {nsstring * something ;}
+ (Void) thisisastaticmethod;
-(Void) thisaninstancemethod;
@ End
@ Interface controller (private )-
(Void) thisaprivatemethod;
@ End
@ Private can be used to modify private variables
In objective-C, all instance variables are private by default, and all instance methods are public by default.
4. Is object-C inherited? If no, what should I replace it? All classes in cocoa are nsobject subclasses.
Multi-inheritance is implemented by the Protocol delegate proxy.
You don't have to consider the cumbersome multi-inheritance, virtual base class concept.
The polymorphism of OOD is implemented by delegation in obj-C.
5. What are the set methods and meanings of memory management autorelease, retain, copy, and assign?
1. You initialize (alloc/init) the object, and you need to release (release) it. For example:
Nsmutablearray aarray = [[nsarray alloc] init]; [aarray release];
2. You need to release retain or copy. For example:
[Aarray retain] And then [aarray release];
3. the objects to be passed (assign), retain and release you need to consider. For example:
Obj2 = [[obj1 somemethod] autorelease];
When object 2 receives an automatically released value of object 1 or transmits a basic data type (nsinteger, nsstring): You may want to retain object 2, to prevent it from being automatically released before being used. However, after retain, it is necessary to release it when appropriate.
Reference counting
Retain value = index count (reference counting)
The nsarray object will retain (retain value plus one) any object in the array. When nsarray is detached (dealloc), all objects in the array are released once (the retain value is reduced by one ). Not only is it nsarray, but any collection class performs similar operations. For example, nsdictionary or even uinavigationcontroller.
The index count of the objects created by alloc/init is 1. You do not need to retain it again.
[Nsarray array], [nsdate date], and other "methods" create an object with an index count of 1, but it is also an automatically released object. So it is a local temporary object, so it doesn't matter. If it is a variable (Ivar) intended to be used in the whole class, you must retain it.
The "Auto Release" method is executed for the return values of the default class methods. (* Nsarray in the preceding example)
In the unload method "dealloc" in the class, release all NS objects that are not balanced. (* All records that are not autorelisted and whose retain value is 1)
6. What is the difference between shallow copy and deep copy?
Simply put, in the case of a pointer, the shallow copy only adds a pointer pointing to the existing memory, and the deep copy adds a pointer and applies for a new memory, point the added pointer to the new memory. When deep copy is used, the same memory will not be released repeatedly during the shortest copy.
7. How to mix C and obj-C
1) when the obj-C compiler processes files with a suffix of m, it can recognize the code of obj-C and C. When processing mm files, it can recognize obj-C, C, c ++ code, but the CPP file must only use C/C ++ code, and the obj-C code cannot appear in the header file of the CPP File Include, because CPP is only CPP
2) Mix CPP in the MM file and use it directly. Therefore, obj-C Mixed CPP is not a problem.
3) Mixing obj-C in CPP is actually what we want to write modules using obj-C.
If the module is implemented as a class, write the class definition according to the CPP class standard. The header file cannot contain obj-C, including # import cocoa. In the implementation file, that is, the implementation code of the class can use obj-C and can be imported, but the suffix is mm.
If the module is implemented by functions, the header file should declare the function in the C format. In the implementation file, the C ++ function can use obj-C internally, but the suffix is still mm or M.
Summary: as long as the CPP file and the CPP include file do not contain obj-C, you can use it. The key to mixing CPP with obj-C is to use interfaces, instead, we cannot directly use modern real-time code. In fact, CPP is a mix of the O files compiled by obj-C. This is actually the same, so it can be used. The obj-C compiler supports CPP.
8. Differences between class extension and class extension in objective-C.
Answer: The difference between category and extensions is that the latter can add attributes. In addition, the method added by the latter must be implemented.
Extensions can be considered as a private category.
9. What does objective-C mean by dynamic runtime language?
Answer: polymorphism. It mainly delays the determination of data types from compilation to runtime.
This problem actually involves two concepts: runtime and polymorphism.
Simply put, the runtime determines the class of an object and calls the specified method of the Class Object until it is run.
Polymorphism: the ability of different objects to respond to the same message in their own way is called polymorphism. This means that we assume that biological classes (LIFE) use the same method-eat;
Humans belong to creatures, and pigs also belong to creatures. After they all inherit life, they implement their own eat. However, we only need to call their own eat methods.
That is, different objects respond to the same message in their own way (response to the Eat selector ).
So it can be said that the runtime mechanism is the basis of polymorphism?
10. What is the difference between objective-C heap and stack?
Management Method: For stacks, it is automatically managed by the compiler without manual control. For heaps, the release work is controlled by programmers and memory leak is easily generated.
Application size:
STACK: in windows, a stack is a data structure extended to a low address and a continuous memory area. This statement indicates that the stack top address and the maximum stack capacity are pre-defined by the system. In Windows, the stack size is 2 MB (OR 1 MB, in short, it is a constant determined during compilation. If the requested space exceeds the remaining space of the stack, overflow will be prompted. Because of this, the space available from the stack is small.
Heap: the heap is a data structure extended to the high address and a non-sequential memory area. This is because the system uses the linked list to store the idle memory address, which is naturally discontinuous, And the traversal direction of the linked list is from the low address to the high address. The heap size is limited by the valid virtual memory in the computer system. It can be seen that the space obtained by the heap is flexible and large.
Fragmentation problem: for the heap, frequent New/delete operations will inevitably lead to memory space disconnections, resulting in a large number of fragments, reducing program efficiency. For the stack, this problem will not exist, because the stack is an advanced and outgoing queue, they are so one-to-one correspondence, it is impossible to have a memory block popped up from the middle of the stack.
Allocation Method: The heap is dynamically allocated without static allocation. There are two stack allocation methods: static allocation and dynamic allocation. Static allocation is completed by the compiler, such as local variable allocation. Dynamic Allocation is implemented by the alloca function, but the stack dynamic allocation is different from the heap dynamic allocation. Its Dynamic Allocation is released by the compiler without manual implementation.
Allocation Efficiency: the stack is the data structure provided by the machine system, and the computer will provide support for the stack at the underlying layer: allocate a dedicated register to store the stack address, the output stack of the Pressure Stack has dedicated Command Execution, which determines the high efficiency of the stack. The heap is provided by the C/C ++ function library, and its mechanism is very complicated.