[Interview] interview questions for iOS development (1)

Source: Internet
Author: User

[Interview] interview questions for iOS development (1)

  1.# What is the difference between import and # include? What is the difference between @ class and # import <> and # import?
A: # import is the keyword used to import the Objective-C header file. # include is the keyword used to import the C/C ++ header file. The # import header file is automatically imported only once and will not be imported repeatedly, it is equivalent to # include and # pragma once; @ class tells the compiler about the declaration of a class. Only when executed can we view the implementation file of the class, which can solve the mutual inclusion of header files; # import <> is used to include the system header file, and # import "is used to include the user header file.


  2.What are the functions of attributes readwrite, readonly, assign, retain, copy, and nonatomic?
1. readwrite is a readable and writable feature. When the getter method and setter method need to be generated
2. readonly is a read-only feature that only generates the getter method and does not generate the setter method. You do not want to change the attribute outside the class.
3. assign is a value assignment feature. The setter method assigns input parameters to instance variables. Only variables are set;
4. retain indicates that the property is held. The setter method will keep the input parameter first, and then assign a value. The retaincount of the input parameter will be + 1;
5. copy indicates the value assignment feature. The setter method copies the input object. When a new variable is required.
6. nonatomic non-atomic operations determine whether the setter getter generated by the compiler is an atomic operation. atomic indicates multi-thread security. nonatomic

 

3.Can Object-c classes be inherited in multiple ways? Can multiple interfaces be implemented? What is Category? Is the method of rewriting a class well inherited or well classified? Why?

A: The class of Object-c cannot be inherited multiple times. Multiple Interfaces can be implemented to implement multiple inheritance of C ++. Category is a class, generally, classification is good, and the class method is rewritten with Category, which is only valid for this Category and does not affect the relationship between other classes and the original classes.

 

4. Write a setter Method for @ property (nonatomic, retain) NSString * name, and write a setter Method for completion. @ Property (nonatomic, copy) NSString * name
-(Void) setName :( NSString *) str
{
[Str retain];
[Name release];
Name = str;
}
-(Void) setName :( NSString *) str
{
Id t = [str copy];
[Name release];
Name = t;
}


5. For the statement NSString * obj = [[NSData alloc] init]; what types of objects are used for obj during compilation and runtime?
It is an NSString type during compilation, and an object of the NSData type during runtime.


6. What are the common object-c data types? What is the difference between them and C's basic data types? Such as NSInteger and int
The Data Types of object-c include NSString, NSNumber, NSArray, NSMutableArray, and NSData. These are all classes. After the object is created, the basic data type of c language is int, only a certain byte of memory space is used to store numeric values. NSInteger is a basic data type, not a subclass of NSNumber, and of course not a subclass of NSObject. NSInteger is the alias of the basic data type Int or Long (NSInteger defines typedef long NSInteger), the difference is that, NSInteger determines whether it is an int or Long based on whether the system is 32-bit or 64-bit.


7. What are the features of the object declared by id?
The object declared by Id has the runtime feature, that is, it can point to any type of objcetive-c object;


8. How does Objective-C manage the memory? What are your opinions and solutions?
Objective-C memory management mainly includes three methods: ARC (Automatic Memory count), Manual memory count, and memory pool.
1. (Garbage Collection) Automatic Memory count: This method is similar to java in the execution process of your program. There is always a high person behind the scenes to help you clean up the garbage accurately, you don't have to consider when it starts to work, how to work. You only need to understand that I have applied for a piece of memory space. When I no longer use the memory and it becomes garbage, I will completely forget it, the tall guy will help me clean up the garbage. Unfortunately, that person needs to consume a certain amount of resources. In the carrying devices, the resources are very popular, so the iPhone does not support this function. Therefore, "Garbage Collection" is not the scope of this Getting Started Guide. If you are interested in the internal mechanism of "Garbage Collection", you can refer to other materials, but to be honest, "Garbage Collection" is not suitable for beginners.
Solution: If you create an instance in alloc-initial mode, the reference count is + 1 after creation, and the reference count is + 1 every retain. Then, you can set the corresponding number of release in the program.
2. (Reference Counted) manual memory count: that is to say, after a memory segment is applied, there is a variable used to save the number of times the memory is used. We call it a counter for the time being, when the counter changes to 0, the memory is released. For example, after A memory segment in program A is successfully applied for, the counter changes from 0 to 1 (we call this process alloc ), then program B needs to use this memory, and the counter is changed from 1 to 2 (we call this process retain ). Then, when program A no longer needs the memory, program A will reduce the counter by 1 (we call this process release); and program B no longer needs the memory, then, reduce the counter by 1 (this process is still release ). When the system (that is, the Foundation) finds that the counter has changed to 0, it will call the memory recycle program to recycle the memory (we call this process dealloc ). By the way, if there is no Foundation, you need to manually maintain the counter, release the memory, and so on.
Solution: It is generally created by a static method of the class. The function name does not contain alloc or init words, such as [NSString string] and [NSArray arrayWithObject:]. reference count + 0 after creation, released after function exit stack, that is, equivalent to a local variable on the stack. of course, you can also extend the lifetime of an object through retain.
3. Memory Pool: You can create and release a memory pool to control the timing of memory application and recovery.
Solution: autorelease is added to the system memory pool, and the memory pool can be nested. Each memory pool requires a creation and release pair, just as it is written in the main function. it's easy to use, for example, [[NSString alloc] initialWithFormat: @ "Hey you! "] Autorelease]: adds an NSString object to the inmost system memory pool. When we release this memory pool, all objects will be released.


9. What is the difference between atomic (atomic) and non-atomic (non-atomic) attributes?
1. atomic provides multi-thread security. Is to prevent reading by another thread when the write is not completed, resulting in data errors
2. non-atomic: in the environment where you manage the memory, the parsed accessors retain and automatically release the returned values. If nonatomic is specified, the accessors simply return this value.


10. Check the following program. What will the first NSLog output? What is the retainCount of str? What about the second and third? Why?
========================================================== ====================
NSMutableArray * ary = [[NSMutableArray array] retain];
NSString * str = [NSString stringWithFormat: @ "test"];
[Str retain];
[AryaddObject: str];
NSLog (@ "% @ % d", str, [str retainCount]);
[Strretain];
[Strrelease];
[Strrelease];
NSLog (@ "% @ % d", str, [str retainCount]);
[AryremoveAllObjects];
NSLog (@ "% @ % d", str, [str retainCount]);
========================================================== ====================
Create retainCount of str + 1, retain + 1, add array automatically + 1 3
Retain + 1, release-1, release-1 2
Delete all objects in the array. All objects in the array are automatically-1


11. What are the principles of memory management? According to the default rule. objects generated by those keywords
Need to be manually released? How can we effectively avoid Memory leakage when combined with property?
Who applies and who releases
Complies with the use principles of Cocoa Touch;
Memory Management mainly needs to avoid "premature release" and "Memory leakage". For "premature release", note that when setting the @ property feature, you must use the feature keyword. For "Memory leakage ", be sure to apply for release and be careful.
The keywords alloc or new generated objects need to be manually released;
Set the correct property and release the retain in the appropriate place,


12. How to test the performance of iOS devices?
Profile-> Instruments-> Time Profiler


13. What is the thread creation method in Object C? What is the method for executing code in the main thread? If you want to delay code execution, what is the method?
There are three ways to create a thread: Use NSThread to create a thread, use GCD dispatch, use NSOperation as a subclass, and then add it to NSOperationQueue; execute code in the main thread by running mselecw.mainthread, if you want to run code in a delayed manner, you can use javasmselector: onThread: withObject: waitUntilDone:


14. describes how to implement the MVC development mode in the iOS SDK.

MVC is Model-VIew-Controller, which is Model-View-Controller. MVC divides the software system into three parts: Model, VIew, and Controller. In cocoa, every object in your program will obviously belong to only one of these three parts, but not the other two. Model Data model. view displays the data. viewcontroller obtains the model to view and serves as a bridge between model and view. MVC can help ensure maximum reusability of the program. Each MVC element operates independently of each other. By separating these elements, it can be used to build maintenance programs that can be updated independently to improve code reusability.



15 what is the difference between shallow replication and deep replication?
Answer: copy the pointer to the object instead of the referenced object.
Deep replication: Copies the referenced object itself.
That is to say, I have 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.


16. What is the role of 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.
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.
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.


17. Differences between categories and class extensions.
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.


18. 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.
In many cases, program code can be simplified. The apple documentation provides a good example.
Kvo: the key-value observation mechanism. It provides a method to observe the changes of a certain attribute, greatly simplifying the code.
You can see that one of the places you have used is monitoring the status of button click changes.
For example, a Custom button
[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.


19. 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.
In addition, the proxy can be understood as a similar callback listening mechanism in java.


20. The types can be modified in oc and cannot be modified.
Answer: you can modify unchangeable collection classes. In my personal understanding, dynamic addition and modification are 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.

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.