Summary of the latest iOS development common interview questions! (with answers)!

18b0253ee4ee48fbab1abd395dc135e9.jpeg1. What is the difference and connection between iOS threads and processes?

Processes and threads are the basic units of program operation experienced by the operating system, and the system uses this basic unit to realize the concurrency of the system to applications.

The main difference between processes and threads is that they are different operating system resource management methods. A process has an independent address space. After a process crashes, it will not affect other processes in the protected mode, and a thread is just a different execution path in a process. Threads have their own stacks and local variables, but there is no separate address space between threads. The death of a thread means the death of the entire process. Therefore, a multi-process program is more robust than a multi-threaded program, but it costs more when switching between processes. The resources are larger and the efficiency is worse. But for some concurrent operations that require simultaneous execution and share certain variables, only threads can be used, not processes.

As a developer, it is particularly important to have a learning atmosphere and an exchange circle. This is my iOS development exchange group: 130595548 , whether you are a novice or a big cow, welcome to settle in, let us progress together and develop together! (The group will provide some free learning books and materials collected by the group owner, as well as hundreds of interview questions and answer documents!)

2. How does iOS find the most suitable control to handle events?

Can I receive touch events?
Is the touch point on yourself?
Traverse the child controls from back to front, repeat the previous two steps.
If there are no eligible child controls, then you are best suited to deal with

3.What does the iOS static keyword do?

(1) The scope of the static variable in the function body is the function body. Unlike the auto variable, the memory of the variable is only allocated once.

Therefore, its value will remain the last value when it is called next time;

(2) The static global variables in the module can be accessed by functions used in the module, but cannot be accessed by other functions outside the module;

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

In its module;

(4) The static member variables in the class belong to the entire class, and there is only one copy of all objects of the class;

(5) The static member function in the class belongs to the entire class. This function does not receive the this pointer, so it can only access the static member variables of the class.

4. What are the functions and common properties of iOS UIEvent objects?

Every time an event is generated, a UIEvent object is generated

UIEvent: It is called an event object, which records the time and type of the event.
Common attributes:

  //Event type 
  //@property(nonatomic,readonly) UIEventType type; 
  //@property(nonatomic,readonly) UIEventSubtype subtype; 
  //Time of event generation 
  @property(nonatomic,readonly) NSTimeInterval timestamp; 
  UIEvent also provides the corresponding Method to get the touch object (UITouch) on a certain view

4. When are loadView, viewDidLoad, and viewDidUnload of ViewController called? What should be done in these functions when customizing ViewController?

viewDidLoad is called when the view is initialized from the nib file, and loadView is called when the controller's view is nil. This method is called when the view is implemented in programming. The view controller will register memory warning notification by default. When any view of the view controller is not used, viewDidUnload will be called. The release of the retained view is implemented here, if it is the retained IBOutlet view Do not release properties here, IBOutlet will be responsible for release.

5.Pros and cons of object-c?

Advantages of objc:

  1. Cateogies

  2. Posing

  3. 动态识别

  4. 指标计算

5)弹性讯息传递

  1. 不是一个过度复杂的 C 衍生语言

  2. Objective-C 与 C++ 可混合编程

缺点:

  1. 不支援命名空间

  2. 不支持运算符重载

3)不支持多重继承

4)使用动态运行时类型,所有的方法都是函数调用,所以很多编译时优化方法都用不到。(如内联函数等),性能低劣。

6.iOS引用与指针有什么区别?

1.引用必须被初始化,指针不必。
2.引用初始化以后不能被改变,指针可以改变所指的对象。
3.不存在指向空值的引用,但是存在指向空值的指针。

7.iOS堆和栈的区别 ?

管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制;对于堆来说,释放工作由程序员控制,容易产生memory leak。

申请大小:

栈:在Windows下,栈是向低地址扩展的数据结构,是一块连续的内存的区域。这句话的意思是栈顶的地址和栈的最大容量是系统预先规定好的,在 WINDOWS下,栈的大小是2M(也有的说是1M,总之是一个编译时就确定的常数),如果申请的空间超过栈的剩余空间时,将提示overflow。因此,能从栈获得的空间较小。

堆:堆是向高地址扩展的数据结构,是不连续的内存区域。这是由于系统是用链表来存储的空闲内存地址的,自然是不连续的,而链表的遍历方向是由低地址向高地址。堆的大小受限于计算机系统中有效的虚拟内存。由此可见,堆获得的空间比较灵活,也比较大。

碎片问题:对于堆来讲,频繁的new/delete势必会造成内存空间的不连续,从而造成大量的碎片,使程序效率降低。对于栈来讲,则不会存在这个问题,因为栈是先进后出的队列,他们是如此的一一对应,以至于永远都不可能有一个内存块从栈中间弹出

分配方式:堆都是动态分配的,没有静态分配的堆。栈有2种分配方式:静态分配和动态分配。静态分配是编译器完成的,比如局部变量的分配。动态分配由alloca函数进行分配,但是栈的动态分配和堆是不同的,他的动态分配是由编译器进行释放,无需我们手工实现。

分配效率:栈是机器系统提供的数据结构,计算机会在底层对栈提供支持:分配专门的寄存器存放栈的地址,压栈出栈都有专门的指令执行,这就决定了栈的效率比较高。堆则是C/C++函数库提供的,它的机制是很复杂的。

8.什么时候用delegate,什么时候用Notification?

delegate针对one-to-one关系,并且reciever可以返回值 给sender,notification 可以针对one-to-one/many/none,reciever无法返回值给sender.所以,delegate用于sender希望接受到 reciever的某个功能反馈值,notification用于通知多个object某个事件。

9.iOS UITouch对象的作用与常见属性?

当用户用一根手指触摸屏幕时,会创建一个与手指相关联的UITouch对象 一根手指对应一个UITouch对象

UITouch的作用:

保存着跟手指相关的信息,比如触摸的位置、时间、阶段
当手指移动时,系统会更新同一个UITouch对象,使之能够一直保存该手指在的触摸位置
当手指离开屏幕时,系统会销毁相应的UITouch对象
UITouch的常见属性

//触摸产生时所处的窗口@property(nonatomic,readonly,retain) UIWindow    *window;//触摸产生时所处的视图@property(nonatomic,readonly,retain) UIView      *view;//短时间内点按屏幕的次数,可以根据tapCount判断单击、双击或更多的点击@property(nonatomic,readonly) NSUInteger          tapCount;//记录了触摸事件产生或变化时的时间,单位是秒@property(nonatomic,readonly) NSTimeInterval      timestamp;//当前触摸事件所处的状态@property(nonatomic,readonly) UITouchPhase        phase;

UITouch的常见方法

   //返回值表示触摸在view上的位置
   //这里返回的位置是针对view的坐标系的(以view的左上角为原点(0, 0))
   //调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置
   - (CGPoint)locationInView:(UIView *)view;
   // 该方法记录了前一个触摸点的位置
   - (CGPoint)previousLocationInView:(UIView *)view;

10.object-c 的内存管理 ?

如果您通过分配和初始化(比如[[MyClass alloc] init])的方式来创建对象,您就拥有这个对象,需要负责该对象的释放。这个规则在使用NSObject的便利方法new 时也同样适用。

如果您拷贝一个对象,您也拥有拷贝得到的对象,需要负责该对象的释放。

如果您保持一个对象,您就部分拥有这个对象,需要在不再使用时释放该对象。

反过来,如果您从其它对象那里接收到一个对象,则您不拥有该对象,也不应该释放它(这个规则有少数的例外)

11.iOS单件实例是什么 ?

Foundation 和 Application Kit 框架中的一些类只允许创建单件对象,即这些类在当前进程中的唯一实例。举例来说,NSFileManager 和NSWorkspace 类在使用时都是基于进程进行单件对象的实例化。当向这些类请求实例的时候,它们会向您传递单一实例的一个引用,如果该实例还不存在,则首先进行实例的分配和初始化。单件对象充当控制中心的角色,负责指引或协调类的各种服务。如果类在概念上只有一个实例(比如NSWorkspace),就应该产生一个单件实例,而不是多个实例;如果将来某一天可能有多个实例,您可以使用单件实例机制,而不是工厂方法或函数。

12.iOS类工厂方法是什么 ?

类工厂方法的实现是为了向客户提供方便,它们将分配和初始化合在一个步骤中,返回被创建的对象,并进行自动释放处理。这些方法的形式是+ (type)className...(其中 className不包括任何前缀)。

工厂方法可能不仅仅为了方便使用。它们不但可以将分配和初始化合在一起,还可以为初始化过程提供对象的分配信息,类工厂方法的另一个目的是使类(比如NSWorkspace)提供单件实例。虽然init...方法可以确认一个类在每次程序运行过程只存在一个实例,但它需要首先分配一个“生的”实例,然后还必须释放该实例,工厂方法则可以避免为可能没有用的对象盲目分配内存。

13.一个指针可以是volatile 吗?解释为什么。

是的。尽管这并不很常见。一个例子是当一个中服务子程序修该一个指向一个buffer的指针时。

14.iOS 类别的局限性 ?

有两方面局限性:

(1)无法向类中添加新的实例变量,类别没有位置容纳实例变量。

(2)名称冲突,即当类别中的方法与原始类方法名称冲突时,类别具有更高的优先级。类别方法将完全取代初始方法从而无法再使用初始方法。

无法添加实例变量的局限可以使用字典对象解决

15.什么是iOS键-值,键路径是什么 ?

模型的性质是通过一个简单的键(通常是个字符串)来指定的。视图和控制器通过键来查找相应的属性值。在一个给定的实体中,同一个属性的所有值具有相同的数据类型。键-值编码技术用于进行这样的查找—它是一种间接访问对象属性的机制。

键路径是一个由用点作分隔符的键组成的字符串,用于指定一个连接在一起的对象性质序列。第一个键的性质是由先前的性质决定的,接下来每个键的值也是相对于其前面的性质。键路径使您可以以独立于模型

实现的方式指定相关对象的性质。通过键路径,您可以指定对象图中的一个任意深度的路径,使其指向相关对象的特定属性。

16.iOS 类别的作用 ?

类别主要有3个作用:

(1)将类的实现分散到多个不同文件或多个不同框架中。

(2)创建对私有方法的前向引用。

(3)向对象添加非正式协议。

17.sprintf,strcpy,memcpy使用上有什么要注意的地方 ?

strcpy是一个字符串拷贝的函数,它的函数原型为strcpy(char dst, ct char *src);

将 src开始的一段字符串拷贝到dst开始的内存中去,结束的标志符号为'\0',由于拷贝的长度不是由我们自己控制的,所以这个字符串拷贝很容易出错。具备字符串拷贝功能的函数有memcpy,这是一个内存拷贝函数,它的函数原型为memcpy(char dst, c*t char src, unsigned int len);

将长度为len的一段内存,从src拷贝到dst中去,这个函数的长度可控。但是会有内存叠加的问题。

sprintf是格式化函数。将一段数据通过特定的格式,格式化到一个字符串缓冲区中去。sprintf格式化的函数的长度不可控,有可能格式化后的字符串会超出缓冲区的大小,造成溢出。

14答案是:

a) int a; // An integer

b) int *a; // A pointer to an integer

c) int **a; // A pointer to a pointer to an integer

d) int a[10]; // An array of 10 integers

e) int *a[10]; // An array of 10 pointers to integers

f) int (*a)[10]; // A pointer to an array of 10 integers

g) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer

h) int (a[10])(int); // An array of 10 pointers to functi that take an integer argument and return an integer

18.readwrite,readonly,assign,retain,copy,nonatomic属性的作用

@property是一个属性访问声明,扩号内支持以下几个属性:

1,getter=getterName,setter=setterName,设置setter与getter的方法名

2,readwrite,readonly,设置可供访问级别

2,assign,setter方法直接赋值,不进行任何retain操作,为了解决原类型与环循引用问题

3. The retain, setter method releases the old value of the parameter and then retains the new value. All implementations are in this order (there is relevant information on CC)

4. The copy and setter methods perform the Copy operation, which is the same as the retain processing flow. The old value is released first, and then the new object is copied, and the retainCount is 1. This is a mechanism introduced to reduce dependence on context. Copy is used when you don't want a and b to share a piece of memory. A and b each have their own memory.

5. Nonatomic, non-atomic access, no synchronization, multi-threaded concurrent access will improve performance. Note that if this attribute is not added, the default is that both access methods are atomic transaction access. The lock is added to the instance level of the belonging object (this is how I understand it...).

Atomic and nonatomic are used to determine whether the getters and setters generated by the compiler are atomic operations. In a multi-threaded environment, atomic operations are necessary, otherwise it may cause erroneous results.

19 "NSMutableString *" This data type represents the "NSMutableString" object itself, there is a difference between the two.

NSString is just a pointer to an object.

Process-oriented is to analyze the steps required to solve the problem, and then use functions to implement these steps step by step, and call them one by one when using them.

Object-oriented is to decompose the problematic affairs into various objects. The purpose of establishing objects is not to complete a step, but to describe the behavior of something in the entire problem-solving step.


Guess you like

Origin blog.51cto.com/15146321/2679215