iOS面试小抄
1.谈下对路由看法
2.一个对象的生命周期
3. iOS深拷贝(MutableCopy)与浅拷贝(Copy)的区别:
深拷贝和浅拷贝的概念
iOS中有深拷贝和浅拷贝的概念,那么何为深拷贝何为浅拷贝呢?
浅拷贝:浅拷贝并不拷贝对象本身,只是对指向对象的指针进行拷贝
深拷贝:直接拷贝对象到内存中一块区域,然后把新对象的指针指向这块内存
在iOS中并不是所有对象都支持Copy和MutableCopy,遵循NSCopying协议的类可以发送Copy协议,遵循NSMutableCopying协议的类可以发送MutableCopy消息。如果一个对象没有遵循这两个协议而发送Copy或者MutableCopy消息那么会发生异常。如果要遵循NSCopying协议,那么必须实现copyWithZone方法。如果要遵循NSMutableCopying协议那么必须实现mutableCopyWithZone方法。
可变对象和不可变对象分别调用Copy和MutableCopy方法的区别
1.系统非容器类对象
(1)调用copy和mutableCopy方法
NSString *str = @"123";
NSString *strCopy = [str copy];
NSMutableString *mustr = [str copy];
NSMutableString *muCopy = [str mutableCopy];
[mustr appendString:@"4"]; //会直接崩溃
[muCopy appendString:@"5"];
NSLog(@"str = %@ strCopy = %@ mustr = %@ muCopy = %@",str,strCopy,mustr,muCopy);
NSLog(@"str地址%p strCopy地址%p mustr地址%p muCopy地址%p",str ,strCopy,mustr,muCopy);
输出结果:
NSMutableString *mustr = [NSMutableString stringWithFormat:@"123"];
NSString *strCopy = [mustr copy];
NSMutableString *mutableStr = [mustr copy];
NSMutableString *mutableStrCopy = [mustr mutableCopy];
[mutableStr appendString:@"5"]; //会崩溃
[mutableStrCopy appendString:@"6"];
NSLog(@"mustr = %@ strCopy = %@ mutableStr = %@ mutableStrCopy = %@",mustr,strCopy,mutableStr,mutableStrCopy);
NSLog(@"mustr地址%p strCopy地址%p mutableStr地址%p mutableStrCopy地址%p",mustr,strCopy,mutableStr,mutableStrCopy);
输出结果:
2.系统容器类对象
(1)调用copy和mutableCopy方法
NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3", nil];
NSArray *arrayCopy = [array copy];
NSMutableArray *muarray = [array copy];
NSMutableArray *copyArray = [array mutableCopy];
NSLog(@"array地址%p arrayCopy地址%p muarray地址%p copyArray地址%p",array,arrayCopy,muarray,copyArray);
NSMutableArray *muarray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3", nil];
NSArray *array = [muarray copy];
NSArray *mutableCopy = [muarray mutableCopy];
NSMutableArray *muCopy = [muarray copy];
NSMutableArray *arrayCopy = [muarray mutableCopy];
NSLog(@"muarray地址%p array地址%p mutableCopy地址%p muCopy地址%p arrayCpy地址%p",muarray,array,mutableCopy,muCopy,arrayCopy);
输出结果:
NSArray *array = [NSArray arrayWithObjects:[NSMutableString stringWithString:@"1"],@"2",@"3", nil];
NSArray *arrayCopy = [array copy];
NSArray *arrayMutableCopy = [array mutableCopy];
NSMutableArray *mutableArrayCopy = [array copy];
NSMutableArray *mutableArrayMutableCopy = [array mutableCopy];
NSLog(@"array地址%p arrayCopy地址%p arrayMutableCopy地址%p mutableArrayCopy地址%p mutableArrayCopy地址%p",array,arrayCopy,arrayMutableCopy,mutableArrayCopy,mutableArrayMutableCopy);
NSLog(@"array = %@ arrayCopy = %@ arrayMutableCopy = %@ mutableArrayCopy = %@ mutableArrayCopy = %@",array,arrayCopy,arrayMutableCopy,mutableArrayCopy,mutableArrayMutableCopy);
NSMutableString *mustr = array[0];
[mustr appendString:@"2"];
NSLog(@"array地址%p arrayCopy地址%p arrayMutableCopy地址%p mutableArrayCopy地址%p mutableArrayCopy地址%p",array,arrayCopy,arrayMutableCopy,mutableArrayCopy,mutableArrayMutableCopy);
NSLog(@"array = %@ arrayCopy = %@ arrayMutableCopy = %@ mutableArrayCopy = %@ mutableArrayCopy = %@",array,arrayCopy,arrayMutableCopy,mutableArrayCopy,mutableArrayMutableCopy);
输出结果:
未更改数组里面内容前的结果:
可以看到当更改原数组的值之后,所有新数组的值都更改了,即使调用了MutableCopy方法创建的新数组里面的值也因此更改,所以可以看出对于系统容器类对象,其元素对象始终是指针复制。
4.runtime(其实项目里有很多,刚才有点赶,忘记了)(介绍下,有没有应用到项目中的一些功能)
5.多线程的应用(准备不足)
6.property具有什么作用
7.frameword和.a文件有什么区别
8.block属性用什么修饰
9.哪些功能需要组件化
10.KVC和KVO
11.UIViewController生命周期