[to] those famous or non-famous iOS face questions (bottom)

Source: Internet
Author: User

1. Runtime

Objective-c is the language of the face runtime (runtime oriented language), which means that it will defer to the runtime the logic to be executed when compiling and linking as much as possible. This gives you a lot of flexibility, you can redirect the message to the right object as needed, you can even swap the implementation of the method, and so on.

runtime abbreviation. is the system at the time of the operation of some mechanism, the most important is the message mechanism. The function call of OC becomes a message sent. belongs to the dynamic call procedure. It is not possible at compile time to decide which function to actually call (it turns out that OC can call any function during the compile phase, even if the function is not implemented, as long as it is declared without an error.) C language will be in the compilation phase error). The corresponding function will be called based on the name of the function only when it is actually running.

Take the following code as an example:

1 [obj makeText];

Where obj is an object and Maketext is a function name. For such a simple invocation. Runtime at compile time will convert the above code into

1 objc_msgSend(obj,@selector(makeText));

First, the compiler converts the code [obj Maketext] into objc_msgsend (obj, @selector (Maketext)), in the Objc_msgsend function. The class of obj is first found through the ISA pointer of obj. In class, first go to the cache through the SEL to find the corresponding function method (guess the Cache method list is the SEL key through the hash table to store, this can improve the function lookup speed), if not found in the cache. Again to find in the methodlist, if not found in MethodList, then take superclass to find. If found, the method is added to the cache to facilitate the next lookup, and the function pointer in method jumps to the corresponding function to execute.

What is Objective-c Runtime?

Objective-c's runtime is a runtime library, which is a library written primarily using C and sinks, adding the ability to face objects to C and creating objective-c. This means that it is loaded in class information, all methods are distributed, method forwarded, and so on. OBJECTIVE-C runtime creates all the required structures, making OBJECTIVE-C's face object programming possible.

Method swizzling Principle

Calling a method in Objective-c is actually sending a message to an object, and the only way to find the message is by selector's name. By using the dynamic characteristics of objective-c, the corresponding method of selector can be realized at runtime to achieve the purpose of linking to the method. Each class has a list of methods that hold the mapping of selector's name and method implementation. IMP is a bit like a function pointer, pointing to a concrete method implementation.

We can use Method_exchangeimplementations to exchange the IMP in 2 ways,

We can use Class_replacemethod to modify the class,

We can use Method_setimplementation to directly set up a method of imp, ...

In the final analysis, all are the selector imp.

2. The GCD implements 1, 2 parallel and 3 serial and 45 serial, and 4,5 is parallel. That is, 3 depends on the execution, 45 depends on the execution of 3.

Relationship

How queue groups are

123456789101112131415161718192021222324252627 - (void) methodone{dispatch_group_t group = dispatch_group_create();dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{    NSLog(@"%d",1);}); dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{    NSLog(@"%d",2);});dispatch_group_notify(group, dispatch_get_main_queue(), ^{    NSLog(@"3");    dispatch_group_t group1 = dispatch_group_create();     dispatch_group_async(group1, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        NSLog(@"%d",4);    });    dispatch_group_async(group1, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        NSLog(@"%d",5);    });});}

Serial queue: Tasks in the queue are only executed sequentially

1 dispatch_queue_t q = dispatch_queue_create(“....”, dispatch_queue_serial);

Parallel queues: Tasks in a queue are typically executed concurrently.

1 dispatch_queue_t q = dispatch_queue_create("......", dispatch_queue_concurrent);

Global queue: Is the system development, directly to use it can be, similar to a parallel queue, but when debugging, unable to confirm the operation of the queue.

1 dispatch_queue_t q = dispatch_get_global_queue(dispatch_queue_priority_default, 0);

Main queue: Each application development program corresponds to a single home row, direct get, and in multithreaded development, the UI is updated with the master queue.

1 dispatch_queue_t q = dispatch_get_main_queue();

The primary queue is a serial queue that comes with the GCD and executes in the main thread. Asynchronous global concurrent queues open new threads and execute concurrently.

synchronous tasks in parallel queues are executed sequentially, only asynchronously.

Serial queues open asynchronous tasks, which are sequential.

The nested synchronization task causes a deadlock when the serial queue turns on an asynchronous task.

3. The change of the Copy,strong value of the depth copy and the attribute

Shallow copy: Copies only pointers to objects, not the reference object itself. For shallow copy, A and a_copy point to the same memory resource, the copy is not a pointer, the object itself is only one copy of the resource, if we do a modification to a_copy, then found that a reference to the object has also been modified. Deep replication is a good understanding that there are two independent objects in memory.

Not all objects in Objective-c support copy,mutablecopy, classes that obey the Nscopying protocol can send copy messages, and classes that follow the Nsmutablecopying protocol can send mutablecopy messages.

1234 [immutableObject copy] // 浅拷贝[immutableObject mutableCopy] //深拷贝[mutableObject copy] //深拷贝[mutableObject mutableCopy] //深拷贝

property to copy, specifying that the value of this property cannot be changed, preventing variable strings from changing their own values without affecting the values of object properties such as Nsstring,nsarray,nsdictionary. Strong the reference to this property changes as it changes. Copy is a copy of the content, and strong is a pointer copy.

The thread on which the 4.NSTimer will run after it is created.

Created with Scheduledtimerwithtimeinterval, which thread is run on which thread the Runloop is created and which thread it will be added to.

The timer that was created by itself, and the thread to which the thread was added to the Runloop.

5. Kvo,nsnotification,delegate and Block differences

KVO is the Observer pattern implemented by the cocoa framework, which is generally used in conjunction with KVC, and can be monitored by KVO for changes in a value, such as the height of the view. is a one-to-many relationship, and a change in value informs all observers.

Nsnotification is a notification and a one-to-many usage scenario. In some cases, KVO and nsnotification are the same, and are informed by changes in status. Nsnotification's characteristics, is the need to be observed to notify the first initiative, and then the observer registered monitoring and then to respond, more than KVO to send a notification step, but its advantage is not limited to the monitoring of property changes, but also to a variety of state changes to monitor, wide range of monitoring, Use is also more flexible.

Delegate is the agent, is I do not want to do things to others to do. For example, the dog needs to eat, through the delegate inform the owner, the host will give him to cook, Sheng fan, pour water, these operations, these dogs do not need to care, only need to call Delegate (agent) on it, by other classes to complete the required operations. So delegate is a pair of relationships.

Block is another form of delegate, a form of functional programming. With delegate, the use of scenes is more flexible than delegate, and the implementation of proxies is more intuitive.

KVO General usage scenario is data, demand is data change, such as stock price change, we generally use KVO (Observer mode). Delegate the general use of the scene is the behavior, the need for someone else to do something for me, such as buying and selling stocks, we generally use delegate. Notification is generally a global notification, such as good news out, notify everyone to buy. Delegate is a strong association, that is, the Commission and the agent know each other, you entrust others to buy stock you need to know the broker, the broker do not know their customers. Notification is a weak association, good news out, you do not need to know who is the hair can also react accordingly, with the barber message people do not need to know that the receiving person can also send messages.

6. How to get a timer to call a class method

A timer can only invoke instance methods, but it is possible to invoke a static method inside the instance method.

Use the timer to be aware that the timer must be added to the Runloop, and select the model to run. The Scheduledtimerwithtimeinterval method creates a timer and joins the Runloop so that it can be used directly.

If the timer repeats select Yes to indicate that the timer will be executed repeatedly, be sure to call the timer's invalid at the appropriate time. cannot be called in Dealloc, because once set to repeats is yes, the timer will hold self, causing Dealloc to never be called, and the class will never be released. For example, it can be called in viewdiddisappear, so that when the class needs to be recycled, it can enter dealloc normally.

7. Does calling a static method of a class require no release?

Static methods, which are class methods, do not need, class method objects are placed in Autorelease

8. Static effect?

(1) The function body static variable scope is the function body, different from the auto variable, the memory of the variable is allocated only once, so its value at the next call will still maintain the last value;

(2) The static global variable within the module can be accessed by functions used within the module, but not by other functions outside the module;

(3) The static function within the module can only be called by other functions within this module, and the use of this function is limited to the declaration

within its module;

(4) A static member variable in a class is owned by the entire class and has only one copy of all objects of the class;

(5) The static member function in a class is owned by the entire class, which does not receive the this pointer, and therefore can only access static member variables of the class.

9. Load and initialize methods for NSObject

Common features of load and initialize:

The system will call at most once without considering the developer's active use

If both the parent and child classes are called, the invocation of the parent class must precede the child class

To create the right run environment in advance of the application run

Do not rely heavily on these two methods when you use them, unless you really need to

The difference between load and initialize:

    • Load method

The calling time is early and the operating environment is uncertain. Specifically, it is usually loaded on iOS when the app is launched, but when the load is called, there is no guarantee that all classes will be loaded and available, and that you are responsible for auto release when necessary. For the two libraries that have dependencies, the load of the dependent classes is called first. But within a library, the order of calls is indeterminate.

For a class, no implementation of the Load method is called, and inheritance of NSObject is not considered.

The load method of a class does not specify [super Load], the parent class receives the call, and precedes the subclass.

The category load will also receive calls, but in order after the main class's load call.

Initialize calls are not triggered directly.

    • Key points of Initialize method

The natural invocation of initialize is the first time the current class is actively used.

When the Initialize method receives the call, the operating environment is basically sound.

Initialize is guaranteed to be thread-safe during operation.

Unlike load, even if a subclass does not implement the Initialize method, it inherits the implementation of the parent class and invokes it again. Note that before this, the method of the parent class has been executed once, and no super call is required.

Due to these characteristics of initialize, the application is slightly more extensive than load. Can be used to do some initialization work, or a singleton mode of implementation of the scheme.

10. Can I add an instance variable to a compiled class? Can I add an instance variable to a class that is created at run time? Why?

The instance variable cannot be added to the compiled class;

The ability to add instance variables to classes created at run time;

Since the compiled class is already registered in runtime, the memory size of the list and Instance_size instance variables of the Objc_ivar_list instance variable in the class structure has been determined, and the runtime calls Class_setivarlayout or CL Ass_setweakivarlayout to handle strong weak references. Therefore, you cannot add an instance variable to a class that exists;

The runtime creates a class that can add an instance variable, calling the Class_addivar function. But after calling Objc_allocateclasspair, the reason is Objc_registerclasspair before.

[to] those famous or non-famous iOS face questions (bottom)

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.