IOS interview question Summary (2)

Source: Internet
Author: User

 
 

47. What is kvc? What is kvo? What are the features?

48. What is the responder chain?

49. How do processes communicate with each other on unix?

UNIX mainly supports three communication modes:

1. Basic Communication: Mainly used to coordinate synchronization and mutual exclusion between processes

(1) Lock File Communication

Both parties can find specific types of files (called lock files) in a specific directory to achieve mutual exclusion between processes when accessing critical resources. For example, process p1 accesses a critical resource, first, check whether there is a specific type of file. If yes, wait for a while before searching for the lock file.

(2) record lock files

2. Pipeline communication: Suitable for Massive Data Transmission

3. IPC: Suitable for Mass Data Transmission

 

50. Is the timer interval accurate? Why? How to implement a precise timer?

 

51. What design model does UIscrollVew use? Can I find something similar in the foundation database? (There are many answers. I don't know which one is correct ~~)

Template mode. all datasource and delegate interfaces are typical applications in Template mode,

Composition mode. All containerviews use this mode.

The observer mode is observer, and all UIResponder uses this mode.

 

52. If you want to develop a software similar to eclipse, the plug-in structure is supported. And open to third-party development. How do you design it? (General idea)

 

53. main ()

{

Int a [5] = {1, 2, 3, 4, 5 };

Int * ptr = (int *) (& a + 1 );

Printf ("% d, % d", * (a + 1), * (ptr-1 ));

}

Answer: 2,5

* (A + 1) is a [1], * (ptr-1) is a [4], the execution result is 2.5 bytes, & a + 1 is not the first address + 1, the system will consider that the offset of an array a is the size of an array offset (in this example, it is five int, int * ptr = (int *) (& a + 1); then, ptr is actually & (a [5]), that is, a + 5 records for the following reasons:

& A is an array pointer, whose type is int (*) [5]; cursor and pointer plus 1 must add a certain value according to the pointer type, different types of pointers + 1 increase with different sizes. A is an int array pointer of 5, so 5 * sizeof (int) must be added. Therefore, ptr is actually a [5], but prt and (& a + 1) type is different (this is important), so the prt-1 will only subtract sizeof (int *), a, & a address is the same, but the meaning is different, a is the first address of the array, that is, the address of a [0]. & a is the first address of the object (array), and a + 1 is the address of the next element of the array, that is, a [1], & a + 1 is the address of the next object, that is, a [5].

 

54. The following is a 32-bit C ++ program in Windows NT. Please calculate the sizeof value into voidFunc (char str [100]) using {inclusizeof (str) =? Response} invalid void * p = malloc (100); inclusizeof (p) =?

Answer: This question is very common. When the array name in the Func (char str [100]) function is used as the function parameter, the array name in the function loses its meaning, it is just a pointer. Without its meaning, it also loses its constant feature. It can be used for auto-increment, auto-subtraction, and other operations and can be modified. On Windows NT 32-bit platform, the pointer length (memory usage) is 4 bytes, so sizeof (str) and sizeof (p) are 4.

 

55.-(void) * getNSString (const NSString * inputString)

{InputString = @ "This is a main test \ n ";

Return ;}

-Main (void)

{NSString * a = @ "Main ";

NSString * aString = [NSStringstringWithString: @ "% @", getNSString (a)];

NSLog (@ "% @ \ n", aString );}

Finally, the output string: NULL. After the output function returns, the memory has been released.

 

56. List the synchronization mechanisms of several processes and compare their advantages and disadvantages.

Answer: The atomic operation semaphores mechanism is used to spin the lock pipe process.

Communication between processes

Answer: shared storage system message passing system pipeline: Based on the file system

Cause of process deadlock

Answer: the order of resource competition and process promotion is illegal.

Four required deadlocks

Answer: mutex, request persistence, non-deprivation, loop

Deadlock handling

Answer: ostrich strategy, prevention policy, avoidance policy, detection and deadlock Removal

 

57. Differences between 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: Stacking 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. Therefore, the space available from the stack is small. Heap: the heap is a data structure extended to the high address and is 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.

 

58. What are key-value and Key Path?

The nature of the model is specified by a simple key (usually a string. The view and controller use keys to find the corresponding attribute values. In a given object, all values of the same attribute have the same data type. Key-value encoding technology is used for such a search-it is a mechanism for indirect access to object attributes.

A key path is a string consisting of keys separated by vertices. It is used to specify a sequence of object properties connected together. The distinct nature of the first key is determined by the previous nature, and the value of each key is also relative to the previous nature. The Key Path allows you to specify the properties of related objects in a way independent of model embedding. Through the Key Path, you can specify a path in any depth in the object graph to point it to a specific attribute of the related object.

 

59. 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, since cpp is only cpp limit 2) You can use it directly in the mm file by mixing cpp, so obj-c Mixed cpp is not a problem. (3) in cpp, mixing obj-c 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 object obj-c cannot appear in the header file, 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 obj-c with cpp is to use interfaces, instead, you cannot directly use the implementation 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.

 

60. Target-Action Mechanism

The target is the receiver of the action message. A control, or more commonly its unit, maintains the target of its action message in the form of a socket variable (see "socket variable" section) hidden.

An action is a message sent from the control to the target, or from the perspective of the target, it is a method implemented by the target to respond to the action.

Programs need some mechanisms to translate events and commands. This mechanism is the target-action mechanism.

 

61. cocoatouch framework

The basic Cocoa Touch framework of the iPhone OS Application Reuse the mature mode of many Mac systems, but it focuses more on Touch interfaces and optimization. UIKit provides you with a basic tool to implement graphics and event drivers on the iPhone OS. It is built on the same Foundation framework as Mac OS X, including file processing, network, string operations.

CocoaTouch has a special design consistent with the iPhone user interface. With UIKit, you can use the unique graphical interface controls, buttons, and full-screen views on the iPhone OS. You can also use the accelerator and multi-touch gestures to control your applications.

In addition to UIKit, Cocoa Touch includes all the frameworks required to create world-class iPhone applications, from 3D graphics to professional sound effects, and even provides device access APIs to control cameras, or use GPS to find the current location. Cocoa Touch includes a powerful Objective-C framework that requires only a few lines of code to complete all tasks, and provides basic C language APIs to directly access the system. These frameworks include:

CoreAnimation

With CoreAnimation, you can create a rich user experience by using a simple programming model that combines independent layers.

CoreAudio

CoreAudio is a specialized technology for playing, processing, and recording audio, allowing you to easily add powerful audio features to your applications.

CoreData

Provides an object-oriented data management solution that is easy to use and understand, and can even process any application or big or small data models.

Function list: Framework Classification

The following is a small part of the available frameworks in CocoaTouch:

• Audio and Video

• Core Audio

• OpenAL

• Media Library

• AV Foundation

• Data Management

• Core Data

• SQLite

• Graphics and animation

• Core Animation

• OpenGL ES

• Quartz 2D

• Network/li>

• Bonjour

• WebKit

• BSD Sockets

• User applications

• Address Book

• Core Location

• Map Kit

• Store Kit

 

62. objc Memory Management

If you create an object by allocating and initializing it (for example, [[MyClass alloc] init]), you have this object for the consumer and need to release it. This rule also applies when using NSObject's convenient method new.

If you copy an object and you have the Copied object, you must release the object.

If you keep an object, you have some of it, and you need to release it when it is no longer in use.

If you receive an object from another object, you do not own the object or release it (this rule has a few exceptions, see the reference documentation for explicit instructions ).

 

63. What is the automatic release pool and how it works?

When you send an autorelease message to an object, Cocoa puts a reference to the object into the latest Auto Release pool. It is still a legitimate object, so other objects in the scope defined by the automatic release pool can send messages to it. When the program is executed at the end of the scope, the automatically released pool will be released, and all objects in the pool will be released.

1. ojc-c manages memory in a "referringcounting" (reference count) mode. When an object starts to allocate memory (alloc), the reference count is one, in the future, when there is a copy or retain, the reference count will be added. When the release and autorelease parameters are met, the reference count will be reduced by one. If the Count of this object is changed to 0, will be destroyed by the system. limit 2. the nyothoreleasepool is used to manage reference counts. lifecycle 3. there is no difference between autorelease and release, except that the time when the reference count is reduced by one is different. autorelease will reduce the reference count by one when the object is actually used.

 

64. What is the class factory method?

The implementation of the class factory method is to provide convenience to customers. They combine allocation and initialization in one step, return the created object, and release it automatically. These methods are in the form of + (type) className... (className does not include any prefix ).

The factory method may not only be used for convenience. They can not only combine the allocation and initialization, but also provide the allocation information for the image during the initialization process.

Another purpose of the class factory method is to make the class (such as NSWorkspace) provide a single-piece instance. Although the init... you can confirm that only one instance exists for a category during each program running, but it needs to assign a "generated" instance first and then release the instance. The memory factory method can avoid blind memory allocation for objects that may not be used.

 

65. What is a single-piece instance?

Some classes in the Foundation and Application Kit frameworks only allow the creation of single-piece objects, that is, the unique instances of these classes in the current process. For example, the NSFileManager and NSWorkspace classes are used to instantiate single-piece Objects Based on processes. When you request instances of these classes, they will pass you a single instance reference. If the instance does not exist, the instance is allocated and initialized first. A single-piece object acts as a control center and is responsible for guiding or coordinating various services. If a class has only one instance (for example, javasnsworkspace) in concept, a single-piece instance should be generated instead of multiple instances. If one day may have multiple instances, you can use the single-piece instance mechanism instead of the factory method or function.

 

66. dynamic binding-determine the method to be called at runtime

Dynamic binding also delays the confirmation of the call Method to the runtime. During compilation, the method call is not bound with the Code. The called code is confirmed only after the message is sent out. Through dynamic type and dynamic binding technology, your code can get different results every time you execute it. The runtime factor is used to determine the receiver and method of the message. The message distribution mechanism at runtime provides support for dynamic binding. When you send a message to an object with a dynamic type, the runtime environment system locates the class of the object through the isa pointer of the receiver and determines the called method starting from this, methods and messages are dynamically bound. Additionally, you can automatically obtain the benefits of dynamic binding without having to do any work in Objective-C code. Each time you send a message,

When the receiver of a message is an object of a dynamic type, dynamic binding takes place routinely and transparently.

 

67. Advantages and Disadvantages of obj-c

Advantages of objc: Objective 1) Cateogies Objective 2) Posing objective 3) Dynamic Identification objective 4) indicator calculation Objective 5) elastic message transfer Objective 6) not an overly complex C derivative language Objective 7) Objective-C and C ++ mixed programming Objective

Disadvantage: Operator 1) does not support naming null period 2) does not support Operator Overloading

3) Multi-inheritance is not supported.

4) use the dynamic runtime type. All methods are called by functions, so many compilation optimization methods are not used. (Such as inline functions), poor performance.

 

68. What are the functions of readwrite, readonly, assign, retain, copy, and nonatomic attributes?

@ Property is an attribute access statement. The extension number supports the following attributes: setter 1, getter = getterName, setter = setterName, and setter and getter method names: setter 2, readwrite, readonly, which is used to assign values directly to access level limit 2, assign, and setter without any retain operations. To solve the problem of original type and cycle reference issue limit 3, retain, the setter method performs the release old value on the parameter and retain the new value. All implementations are in this Order (related information is available on CC). 4, copy, and setter Methods perform the Copy operation, like the retain processing process, the old value is release first, and then the new object is copied. retainCount is 1. This is a mechanism introduced to reduce context dependencies. Ipv5, nonatomic, non-atomic access, without synchronization, multi-thread concurrent access will improve performance. Note: If this attribute is not added, both access methods are atomic transaction access by default. The lock is added to the instance level of the object to which it belongs (I understand this ...).

 

69. When was didReceiveMemoryWarning of ViewController called? (87)

70. How do you understand and understand ARC?

 

71. In ObjC, is the method opposite to alloc semantics dealloc or release? The method opposite to the retain syntax is dealloc or release. Why? The method to be paired with alloc is dealloc or release. Why?

Alloc is opposite to dealloc. alloc is the creation variable, and dealloc is the release variable. Retain corresponds to release, and retain retains an object. After the call, the variable count is increased by 1. It may not be obvious. Here is an example:

-(Void) setName: (NSString *) name {

[Name retain];

[Myname release];

Myname = name ;}

Let's explain: imagine that when a user calls this function, he pays attention to memory management, so he carefully writes the following code:

NSString * newname = [[NSString alloc] initWithString: @ "John"];

[AClasssetName: newname];

[Newnamerelease];

Let's take a look at how the newname count changes. First, it is alloc, count = 1; then, in setName, It is retain, count = 2; finally, the user releases newname, count = 1, and myname points to newname. This also explains why you need to call [myname release]. We need to release old variables when assigning new values to myname. After retain, the direct dealloc object counter is not released. Alloc needs to be paired with release, because after the alloc function is called, the variable count is increased by 1. Therefore, after calling alloc, you must call the corresponding release. In addition, after a variable is release, its value is still valid, so it is best to followed by var = nil.

 

72. In the method of an object:

Self. name

And

Name = "object"

What is the difference?

 

73. What is the problem with this code:

@ Implementation Person

-(Void) setAge :( int) newAge {

Self. age = newAge;

}

@ End

 

74. What is retaincount?

 

75. What is the retain count of the person object after each line of code below is executed?

Person * person = [[Person alloc] init];

[Person retain];

[Person release];

[Person release];

 

76. Why do many built-in classes such as the delegate attribute of UITableViewController use assign instead of retain?

 

77. When do I use copy, assign, and retain when defining attributes?

Assign is used for simple data types, such as NSInteger, double, bool, retain, and copy user objects. copy is used when a points to an object and B wants to point to the same object, if assign and a are used, if they are released, then calling B will crash. If the copy method is used, a and B have their own memory, which can solve this problem. Retain will increase the counter by one, which can also solve the assign problem. In addition, atomic and nonatomic are used to determine whether the getter and setter generated by the compiler are atomic operations. In a multi-threaded environment, atomic operations are necessary; otherwise, errors may occur. With atomic added, the setter function will become as follows:

If (property! = NewValue) {[propertyrelease]; property = [newValueretain];}

 

78. When will the autorelease object be release?

A: autorelease actually only delays the call to release. For each Autorelease, the system only places the Object in the current Autoreleasepool. When the pool is released, all objects in the pool will be called Release. For each Runloop, the system will implicitly create an Autoreleasepool, so that all releasepools will constitute a stack structure like CallStack. At the end of each Runloop, the Autoreleasepool at the top of the current stack will be destroyed, so that every Object (the Object of autorelease) in this pool will be release. So what is a Runloop? A ui event, Timer call and delegate call, will all be a new Runloop. So what is a Runloop? A ui event, Timer call and delegate call, will all be a new Runloop.

 

79. How can I modify this code?

For (int I = 0; I <someLargeNumber; I ++)

{

NSString * string = @ "Abc ";

String = [string lowercaseString];

String = [stringstringByAppendingString: @ "xyz"];

NSLog (@ "% @", string );

}

 

80. What is the relationship between autorelease and gc? Is there garbage collection (gc) for IPhone OS )?

 

81. What is protocal in Objective C?

The protocol in @ protocal obj is like the interface in java.

 

82. What is selector in Objective C?

You can understand that @ selector () is the number of the class method. Its behavior can basically be equivalent to the function pointer in C language, but in C language, the function name can be directly assigned to a function pointer, while the Objective-C class cannot directly apply the function pointer. In this way, only one @ selector syntax can be used to obtain the function. the result is a SEL type. This type is essentially the number (function address) of the class method ).

 

83. What is category in Objective C?

Objective-C provides a very flexible Class extension mechanism-Category ). A category is used to add a method (Methods) to an existing class ). You only need to know the public interface of this class, and do not need to know the source code of the class. You must note that the category cannot add Instance Variables for an existing class ).

Subclassing is another common method for class extension. Compared with subclass, the main advantage of category is that existing classes in the system can use the extended category function without modification.

The second advantage of category is that it implements localized encapsulation of functions. The category definition can be placed in an existing class (Class A) definition file (. h ). This means that this category is only visible externally when class A is referenced. If another class (Class B) does not need to use the function of Class A (the. h file of Class A is not included), the class of the attached Class A will not be seen. This category definition method is widely used in iOS SDK for encapsulation.

 

84. What is Notification? When is delegate used and Notification used?

In observer mode, the controller adds its own notification to defanotifnotificationcenter. Other classes can register this notification and receive the notification, these classes can perform their own operations when receiving the notification (multiple observers send the notification to the observer in a random order by default, in addition, each observer must wait until the current operations of an observer are completed before it is his or her turn to operate. You can use icationicationqueue to arrange the reaction sequence of the observer, or you can set the reflection time in the Add observer, to cancel observation, you must log out of viewDidUnload and dealloc ).

Delegate is for the one-to-one relationship, and the reciever can return the value to the sender. The notification can be for one-to-one/pull/none, and the reciever cannot return the value to the sender. therefore, delegate is used by sender to receive a function feedback value from reciever,

Notification is used to notify an event of multiple objects.

 

85. What are KVC and KVO? What is the difference between Notification and KVO? How is KVO implemented in ObjC?

 

86. When is the loadView, viewDidLoad, and viewDidUnload of ViewController called? What should I do in the Custom ViewController functions?

A: viewDidLoad is called when the view is initialized from the nib file, while loadView is called when the controller's view is nil. This method is called when the view is programmed. By default, the view controller registers the memory warning notification. When any view of the view controller is useless, viewDidUnload is called, here, we implement retain's view release. If it is retain's IBOutlet view attribute, we should not release it here, and IBOutlet will be responsible for release.

 

87. When was didReceiveMemoryWarning of ViewController called? In the UIViewController class, what is the default didReceiveMemoryWarning operation?

A: [superdidReceiveMemoryWarning] is called by default.

 

88. In UITableViewController, when UITableViewCell is created, what is the use of reuseIdentifier in initWithSytle: resuseIdentifier? The Reuse Principle of UITableViewCell is briefly described.

Added elements of the multiplexing queue: the cell is added to the multiplexing queue only when the cell is sliding out of the interface. Each time a cell is created, the program first calls the dequeueReusableCellWithIdentifier: cellType method to find the cell with the identifier "cellType" in the multiplexing queue. If no cell is found, nil is returned, the program then calls [[[UITableViewCell alloc] initWithStyle: style reuseIdentifier: cellType] autorelease] to create a cell with the "cellType" identifier.

 

89. What is the difference between UIView and CALayer?

The biggest difference between the two is that layers are not directly rendered to the screen.

 

90. Taking the UIView class animateWithDuration: animations: as an example, we will briefly describe the principle of UIView animation.

 

 

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.