IOS interview question 02

Source: Internet
Author: User

What is the difference between shallow replication and deep replication? // Shallow copy and deep copy
Answer:
Copy: only copies the pointer to the object instead of the referenced object. // Access this object through the object pointer
Mutablecopy: Copies the referenced object itself.
This means that there is an object A. After copying a copy and getting the_copy object, for the shortest copy, A and a_copy point to the same memory resource, and the copy is just a pointer, object Resource
There is only one copy. If we perform the modification operation on a_copy, we will find that the object referenced by A is also modified, which violates the idea of copying. Deep replication is easy to understand, and memory exists
Two independent objects. // When a is modified, a copy remains unchanged.

A metaphor: 1. A copy is: you are down. Your mother shouted that you could not find anyone when you went home for dinner. She was very sad. 2. Deep copy: You cloned yourself: you hung up, your brother was still there, and your mother called you back for dinner to find someone. So, for children, keep yourself safe and make a deep copy. Remember to manage the memory.

What is the role of a category? What are the differences between inheritance and category implementation?
Answer: you can add a new method to the category without learning or changing the original code. You can only add the method, but cannot delete the modification. // Category: class and type
In addition, if the category conflicts with the method in the original class, the category overwrites the original method because the category has a higher priority. // Class and Class priority
Category has three main functions:
(1) Distribute the implementation of classes to multiple different files or different frameworks.
(2) create a forward reference to a private method.
(3) add informal protocols to objects.
Inheritance can be added, modified, or deleted, and attributes can be added.
// Informal Protocol: Implemented by category, and informal protocol is a class of nsobject, so that any class object can be used as a delegate object, it can list all methods that can be executed by an object, so as to implement delegation. We can use selector to determine whether this method is available in the informal protocol.
Formal Protocol: it is a list of named methods. Unlike informal protocols, it requires the protocol to be displayed, the Protocol method is to list the protocol name in the @ interface declaration of the class. In this case, the class implementing the Protocol should abide by the Protocol and promise to implement all methods in the Protocol.

3. Differences between category and class extension.
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.
4. What is the difference between the Protocol in OC and the interface concept in Java?
Answer: The agent in OC has two meanings: formal and informal protocol. The former is the same as the Java interface.
The methods in informal protocol belong to the scope of design pattern Consideration and are not required. However, if there is an implementation, the attributes of the class will be changed.
In fact, I have read about formal protocols, types, and informal protocols a long time ago and have written them in the course of study.
"The concept of informal protocols is actually another expression of categories." Here are some methods you may want to implement. You can use them to better complete the work ".
This means that these are optional. For example, if we want a better method, we will declare such a category to implement it. Then you can directly use these better methods later.
In this case, we always think the category is a bit like an optional protocol. "
Now we can see that protocal has begun to unify and standardize the two, because the document says "informal protocols use interface modifier",
Now we can see two modifiers in the Protocol: "must be implemented (@ requied)" and "Optional implementation (@ optional )".
5. What are KVO and KVC?
Answer: KVC: Key-value encoding is an indirect access object attribute that uses a string to identify the attribute, instead of calling an access method, or directly accessing the attribute through instance variables. // KVC uses an ISA-swizzling technology. Isa-swizzling is a type-based hybrid pointer mechanism. KVC mainly uses Isa-swizzling to implement internal search and positioning. ISA pointer, which is a kind of, refers to the class of the object that maintains the sub-table. The sub-Table actually contains pointers to methods in the implementation class, and other data.

 

KVO (key-value observing): the key-value observation mechanism. It provides a method to observe the changes of a certain attribute, greatly simplifying the code.
Once used, it monitors the status of button click changes.
For example, a Custom button. // KVO provides Automatic Message notification when the attribute changes. A Message notification is sent every time the attribute is changed. This is because when this solution has been clearly defined and is supported at the framework level, it can be easily used. Developers do not need to design their own observation models and can use them directly in the project. The Kov architecture allows multiple observers to observe the same attribute and related values.

[CPP]
[Self addobserver: Self forkeypath: @ "highlighted" Options: 0 context: Nil];
 
 
# Pragma mark KVO
 
-(Void) observevalueforkeypath :( nsstring *) keypath ofobject :( ID) object change :( nsdictionary *) change context :( void *) Context
{
If ([keypath isw.tostring: @ "highlighted"]) {
[Self setneedsdisplay];
}
}
In theory, the system changes the value obtained based on keypath, which is the same as the KVC mechanism.
How to find the value through the key of the KVC mechanism:
"When calling an object through KVC, for example, [self valueforkey: @" somekey "], the program will automatically try to parse the call in several different ways. First, check whether the object has the somekey method. If it is not found, it will continue to find whether the object has the somekey instance variable (Ivar). If it has not been found, the program will continue to try to call-(ID) valueforundefinedkey: This method. If this method is still not implemented, the program will throw an nsundefinedkeyexception error.
 
(Cocoachina.com Note: When the key-value coding search method is used, it not only searches for the somekey method, but also the getsomekey method. A get is added to the front, or _ somekey and _ getsomekey. In addition, when searching for instance variables, we will not only look for the somekey variable, but also find whether the _ somekey variable exists .)
 
Design valueforundefinedkey: when you use the-(ID) valueforkey method to request a value from an object, the object can have a final chance to respond to the request before an error occurs. There are many benefits to doing so. The following two examples illustrate the benefits of doing so. "
Come to cocoa, this statement should be quite reasonable.
Because we know that there is a highlighted instance variable in the button, we just need to add a related keypath,
You can follow the KVC search logic to understand the logic.
6. What is the role of proxy?
Answer: The proxy aims to change or transmit the control chain. Allows a class to notify other classes at certain times without obtaining pointers to those classes. This reduces the complexity of the framework.

7. The types can be modified in OC and cannot be modified.
Answer: It is the same as dynamic addition and modification.
For example, nsarray and nsmutablearray. The former memory control after Initialization is fixed and immutable, and the latter can be added, and new memory space can be dynamically applied.
8. What do we mean when OC is a 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.
The runtime mechanism allows us to determine the class of an object and call the specified method of this Class Object until the runtime.
Polymorphism: the ability of different objects to respond to the same message in their own way is called polymorphism.
// All objects use the same method to respond to the same message in their own way.
Therefore, it can be said that the runtime mechanism is the basis of polymorphism.
9. What are the differences between notifications and protocols?
Answer: The Protocol has a control link (has-a). No notification is sent. // Notification: one message can be one-to-multiple, and one message can be sent to multiple message recipients, but the message is not processed.
Control chain: the relationship between a single ownership and a controllable one.
10. Polymorphism
Answer: polymorphism. Subclass pointers can be assigned to the parent class.
Objects can not only exist as their own types, but also exist as their parent classes. Polymorphism is a technology that allows you to set a parent object to be equal to one or more of its sub-objects. polymorphism allows you to reference objects of different classes using pointers of the same class (base class) type, and perform the same operation in different ways according to different referenced objects.

11. Talk about the response chain
Answer: Event Response chain. Including click events and screen refresh events. Spread from top to bottom in the view stack or from top to bottom.
12. What is the difference between frame and bounds?
Answer: frame refers to the position and size of the view in the parent view coordinate system. (The reference point is the father's coordinate system) // frame: frame, Structure
Bounds indicates the position and size of the view in its own coordinate system. (The reference point is its own coordinate system) // bounds: Bounds
14. nsoperation queue
The operation and operation queue can basically be seen as the concept of threads and thread pools in Java. It is used to handle IOS multi-thread development issues.
Some materials on the Internet mentioned that although it is a queue, it is not a concept with a queue. The put operations are not strictly advanced.
There is another question here: for a queue, the concept of first-in-first-out is that afunc is added to the queue, and bfunc is followed into the queue. afunc is required to execute it first,
However, bfunc is started and executed only after afunc is fully operated. Therefore, the concept of queue is a little different from that of multi-thread processing.
However, you can refer to the Bank's ticketing and calling system.
Therefore, for a to get votes in the queue before B, but B is the first to complete the operation, we can also perceive this as a queue.
However, I saw an article about this queue topic, one of which mentioned
"Because the time interval between two operations is very close, it is not certain who starts the thread in the thread pool ."
In an instant, I think this queue name is a little dumb. It's not as good as pool ~
To sum it up, we know that he can be of great use in helping with multi-thread programming.
15. are two tableview controllers embedded in one view controller?
Answer: A View control only provides one view. Theoretically, a tableviewcontroller cannot be used,
Only one tableview can be embedded. It is a macro view controller, so we can regard it as a view controller, which can control multiple view controllers, such as tabbarcontroller.

16. Can a tableview be associated with two different data sources? What do you do?
Answer: First of all, from the code, how to associate the data source is actually implemented in the proxy method of the data source Association.
 

-(Uitableviewcell *) cellforrowatindexpath :( nsindexpath *) indexpath
{
If (indexpath. Section = 0)
{
}
If (indexpath. Section = 1)
{
}
}
17. What is the ID type?
Variables of the ID type can store any data type objects. In terms of internal processing, this type is defined as a pointer to an object, which is actually a pointer to the instance variable of this object.
Example: ID number
Declare number as an ID variable. Declarative methods make them return values of the ID type as follows:
-(ID) newobject; (INT) type;
This program declares an instance method named newobject, which has a single integer parameter named type and a return value of ID type. It should be noted that, for the return value and parameter type declaration, ID is the default type.
The ID type is the data type of unusual Chinese drug stores in Objetive-C. It is the basis of polymorphism and dynamic binding.

 


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.