iOS--Practicality Interview Questions

foreword

Many people say that they are familiar with UIKit. Are they familiar with common APIs?

Multithreading is an enduring test point for the front end.

Everyone is familiar with Block's weak-strong dance. Do you know the holder behind each reference and the specific release time of the object?

Come and try these 4 carefully selected questions.

text

Topic 1, UIImage related

Look at the following piece of code,

What is saved to the album? (Describe from format and shape)

1
2
3
4
5
6
7
8
9
10
11
12
13
- ( void )testUIImage {
     UIImage *testImage;
     UIGraphicsBeginImageContext(CGSizeMake( 50 50 ));
     UIView *testView = [[UIView alloc] initWithFrame:CGRectMake( 0 0 50 50 )];
     testView.backgroundColor = [UIColor redColor];
     testView.layer.cornerRadius =  25 ;
     testView.layer.masksToBounds = YES;
     [testView.layer renderInContext:UIGraphicsGetCurrentContext()];
     testImage = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
     
     [[[ALAssetsLibrary alloc] init] writeImageToSavedPhotosAlbum:testImage.CGImage metadata:nil completionBlock:nil];
}

Topic 2, URL related

Look at the following piece of code,

Write down the three lines of Log output and explain what the URL is.

1
2
3
4
5
6
7
8
9
10
11
- ( void )testUrl {
     NSString *path = @ "https://www.baidu.com/" ;
     NSString *path2 = @ "http://fanyi.baidu.com/translate?query=#auto/zh/" ;
     NSString *path3 = @ "http://fanyi.baidu.com/translate?query=#zh/en/测试" ;
     NSURL *url = [NSURL URLWithString:path];
     NSURL *url2 = [NSURL URLWithString:path2];
     NSURL *url3 = [NSURL URLWithString:path3];
     NSLog(@ "%@" , url);

原文出自:http://www.cocoachina.com/ios/20180129/22031.html点击打开链接
     NSLog(@ "%@" , url2);
     NSLog(@ "%@" , url3);
}

Topic 3, thread related

Look at the following piece of code,

Write down the output of Log and explain why.

1
2
3
4
5
6
7
8
9
10
11
- ( void )viewDidLoad {
     [ super  viewDidLoad];
     dispatch_async(dispatch_get_global_queue( 0 0 ), ^{
         NSLog(@ "before perform" );
         [self performSelector:@selector(printLog) withObject:nil afterDelay: 0 ];
         NSLog(@ "after perform" );
     });
}
- ( void )printLog {
     NSLog(@ "printLog" );
}

Topic 4, memory related

Look at the following two pieces of code,

The code of ViewController is as follows

1
2
3
4
5
6
7
8
9
- ( void )testBtn {
     LYButton *btn = [[LYButton alloc] init];
     [self.view addSubview:btn];
     [btn test];
     
     [self.view addSubview:btn];
     [btn test2];
     
}

The code of LYButton is as follows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@implementation LYButton
- ( void )test {
     [self removeFromSuperview];
     
     NSLog(@ "%@" , (self == nil) ? @ "YES"  : @ "NO" );
}
- ( void )test2 {
     __weak  typeof  (LYButton *) weakSelf = self;
     dispatch_async(dispatch_get_main_queue(), ^{
         [weakSelf removeFromSuperview];
         NSLog(@ "%@" , (weakSelf == nil) ? @ "YES"  : @ "NO" );
     });
}
@end

Write down the output of Log and explain why.

Answer

Topic 1

Point of investigation: understanding of common UI operations and image formats.

The testImage in the memory is in an uncompressed format, and you can save it to the album in png or jpeg format.

-writeImageToSavedPhotosAlbum: The default jpeg format used by the interface. If you save png, you need to convert the image to NSData, and then save it.

The operation of the testView is to draw a rounded button, and then use the layer's renderInContext to draw it into the Context;

1.png

result graph

Topic 2

Point of investigation: Understand the -URLWithString: of the API, and the essential knowledge point is URL encode.

A common mistake is to add Chinese to the get parameter, but without re-encoding (also called escaping), which causes NSURL initialization to fail.

正确的做法是调用NSString的(NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding方法。

URL:Uniform Resource Locator,统一资源定位符,用的是ASCII编码。

题目3

考察点:GCD并发队列实现机制,以及performSelector的实现原理以及runloop了解。

上面这段代码,只会打印before perform和after perform,不会打印printLog。

原因:

1、GCD默认的全局并发队列,在并发执行任务的时候,会从线程池获取可执行任务的线程(如果没有就阻塞)。

2、performSelector的原理是设置一个timer到当前线程Runloop,并且是NSDefaultRunLoopMode;

3、非主线程的runloop默认是不启用;

进阶问题:加一行代码使得printLog能正常打印。

题目4

考察点:内存的引用计数。

test1中,removeFromSuperview执行之前,有-testBtn、-test1、self.view三个地方持有强引用,到打印log的时候两个地方的强引用;

test2中,在block中强引用了weakSelf,当block执行的时候,testBtn和test2的两个引用都已经释放,当执行完removeFromSuperview之后,最后一个引用也释放,会立刻执行dealloc方法,weakSelf被置为nil(weak指针的用法就是在对象被回收后变成nil),故而Log输出YES;

类似,在UIButton的onClick:回调方法中,button类的self不仅会被StackThread持有,还会被main thread dispatch持有(系统分发点击事件)。

总结

做题是一个有意思的过程,短时间的思考并得到对or错的回馈,非常适合人脑的学习模式。

希望这几道题能有所帮助。如果错误,请斧正。

原文链接:http://www.cocoachina.com/ios/20180129/22031.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326801822&siteId=291194637