iOS interview questions summary (continually updated)

Over time going to quit, we got some face questions to do, where to be a convenient summary review, I hope to be able to help interview the children's shoes.

The following is the interview questions:

  1. What happens to run the following code

    NSString *str1 = @"str1";
            NSString *str2 = [NSString stringWithFormat:@"str1"];
            NSString *str3 = @"str1";
            NSLog(@"str1 == str2 --- %d", str1 == str2);
            NSLog(@"str1 == str3 --- %d", str1 == str3);
            NSLog(@"str1 isEqualToString str2 --- %d", [str1 isEqualToString:str2]);
            NSLog(@"str1 isEqualToString str3 --- %d", [str1 isEqualToString:str3]);   

     

  At first glance this question can only be determined using isEqualToString: to compare compare each string is a character, so isEqualToString is certainly true, and the number used to determine whether to use == point to the same address, then the problem is in the OC Lane come, what difference does it create and invoke methods using string literals created?

  Practice makes perfect, honestly knock code marked with a breakpoint to take a closer look

  

  It can be seen using a string literal constant string is created, and the method is to create a string pointer. Constant string will be destroyed after the app is released, will always be present during app exists, and the same constant string point to the same address.

  Run as a result

  

  timer created 2. What is the difference in the following ways

 [NSTimer scheduledTimerWithTimeInterval:1.f repeats:YES block:^(NSTimer * _Nonnull timer) {
            
        }];
        [NSTimer scheduledTimerWithTimeInterval:1.f repeats:NO block:^(NSTimer * _Nonnull timer) {
            
        }];
        [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:2.f] interval:1.f repeats:YES block:^(NSTimer * _Nonnull timer) {
            
        }];
        [NSTimer timerWithTimeInterval:1.f target:self selector:@selector(performTimer:) userInfo:nil repeats:YES]

 

  

  I understand timer before the specified time timer is started, if performed repeatedly, whether immediately or manual fire. Is a little deeper timer can be added to different runloop, so you can slide when scrollview does not affect the timer executed. Check the information found, NSTimer will target a strong reference in the repeats for the YES state, and in the absence invalidate is not released, so it is possible to use the timer when the case of circular references occur. For example, the controller A strong reference timer, while the target for the timer A, which had a circular reference, when the controller is pop, the controller will not be destroyed, it will cause a memory leak. Therefore, when using the timer on the need at the right time to release the timer. Just invalidate still causing a circular reference, only the timer can be set to nil. Take the previous example, the need to do this in the life cycle of the controller, viewWillAppear create a timer, the timer is set to the viewWillDisappear nil.

  3. If there is demand, "specify the location of a text insert a picture," Please write the realization of ideas.

  You can use rich text NSAttributeString and NSTextAttachment to achieve, first find where you want to insert the picture, and then use NSTextAttachment to package picture, and finally NSTextAttachment to generate NSAttributeString can.

  4. WebView memory management issues to talk about their own experiences and views

   UIWebView do have a memory leak problems can only be reduced by means of a number of memory leaks, and can not be completely solved, the solution is to use WKWebView. Optimization method is as follows:

   (1) clear the cache memory when you receive a warning

- (void)applicationDidReceiveMemoryWarning:(UIApplication*)application
{
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}

  (2) released webView

self.webView.delegate = nil;
[self.webView loadHTMLString:@"" baseURL:nil];
[self.webView stopLoading];
[self.webView removeFromSuperview];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[self.webView release];

   (3) webViewDidFinishLoad

[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
    [[NSUserDefaults standardUserDefaults] synchronize];

  5. RunLoop relations and threads

  Typically thread will be destroyed after executing the code, RunLoop event processing loop in fact, received only when an exit event will quit. In iOS development, RunLoop thread is one to one relationship, a RunLoop one thread. Note that only the main thread creates RunLoop By default, the other worker thread needs to call their own display

[NSRunLoop currentRunLoop] to get the current thread bound RunLoop (ps: if RunLoop does not exist, create a new one). RunLoop created out of the need to supplemented with Timer / Source / Observer or march port, otherwise RunLoop will be destroyed.

  6. ARC by what means memory management

  Not just the ARC, MRC also use reference counting for memory management, but do not need their own to manage ARC Release and Retain. The system will be automatically released at the end of the pool, there is no strong reference to the object of unified release.

  7. Use Block, what would cause a circular reference, how to solve?

  Because the block will have to use all of the objects in the block strong reference (capture), so when the block is held by an object, and the object has been used in the Block when there will be a strong reference. Solution is to use only weak references Block code is as follows  

__weak typeof(self)weakSelf = self;
    void (^block)(void) = ^{
        NSLog(@"%@", weakSelf);
    }

  8. What is the role synthesize and dynamic respectively

  @Synthesize action: Specify the name of the member variable generation to generation, and generate getter and setter methods for the Property. Usage: For read-only attribute, if both re-setter and getter methods, it is necessary to use manual synthesis synthesize member variables, as follows

@interface Person : NSObject

@property (nonatomic, assign, readonly) NSInteger age;

@end

@implementation Person
@synthesize age = _age;
- (void)setAge:(NSInteger)age
{
    _age = age;
}

- (NSInteger)age
{
    return _age;
}
@end

 

  The role of @dynamic: tell the compiler not to generate getter and setter for the specified Property. Use: When we use the Property as a class in the classification extended attributes, the compiler generates a default will not do this property getter and setter, then you need to tell the compiler to use dynamic, their own synthesis, the code is as follows

  

@interface Person (Extension)

@property (strong, nonatomic) NSString *name;

@end

@implementation Person (Extension)
@dynamic name;

- (void)test
{
    NSLog(@"%@", self.name);
}

@end

  9. runtime how to find the corresponding IMP address Selector? (Considering each class and instance methods)

  This is from the class structure for the first official look at this given below

  10. initialize the difference between the load and

  initialize calls in the Class for the first time receive a message, will first call the parent class than the subclass, the subclass if not re-implement the initialize method, this method will be called multiple times to accept the message in a subclass. load method will be called when loaded into the runtime environment in the class, the entire run time will only be called once during.

  11. How can declare private variables and private methods? And how external calls

  申明私有变量总的来说有3种方式,一种是在@interface中利用@private 关键字来申明,第二种方式是在@implementation声明。私有方法声明的话,就只能够在@implementation中声明了。访问私有变量可以通过提供getter和setter方法,KVO中key使用成员变量名也可以访问,调用私有方法只能通过暴露方法,或者是利用runtime,以及NSObject提供的方法performSelector系列的方法。

  12.以下代码运行结果

@interface Student : Person

@end

@implementation Student

- (instancetype)init
{
    self = [super init];
    if (self) {
        NSLog(@"%@", NSStringFromClass([self class]));
        NSLog(@"%@", NSStringFromClass([super class]));
    }
    return self;
}

@end

  运行结果都为Student,原因是self表示此方法从自己的方法表开始找,super则表示方法先从父类的方法表里找,因为class方法两个类都没有实现,最终方法的都是会NSObject中找到,所以结果都是调用的类名Student。

 14. 用代码实现一个冒泡算法(明天来)

 15. readwrite,readonly, assign,retain, copy, nonatomic,strong的作用

  readwrite表示此属性可以读写,会自动生成getter与setter

  readonly表示此属性只可读,外部只能方法getter方法

  assign在MRC中用于表示引用计数不用加一,以及用于除类之外的声明。在ARC中用于除了类之外的声明

  retain在MRC中表示引用计数加一,在ARC中表示强引用

  copy在MRC中不会影响调用copy方法的对象的引用计数,在ARC中表示在setter方法中会调用传入对象的copy方法,常用于需要不可变对象的属性NSString、NSArray等

  atomic表示属性读写的原子性,然而并不能保证线程安全

  nonatomic则不保证线程安全

  16. 请写出UIViewController的生命周期

   init->viewDidLoad->ViewWillAppear->ViewDidappear->viewWillDisappear->viewDidDisappear->dealloc

  17. 请写出一个单例实现

  利用dispatch_once,其他的实现方式可以重写allocWithZone方法

+ (instancetype)sharedInstance
{
    static Student *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

  18.UIView相关 

 

  19. 当前维护的App的崩溃率是多少?怎么追踪并解决的?线上崩溃如何解决的?

  崩溃率这个就不谈了,开发过程中遇到的崩溃问题主要查看崩溃的栈信息,利用异常断点来解决。线上崩溃的话,就是利用dSYMS文件来符号化苹果的崩溃日志来解决了

  20. 什么是事件响应链?当用户与iPhone的触屏产生互动时?都发生了什么?事件是如何传递的?

  讲响应者链条前,需要知道iOS中事件响应是基于UIResponder对象的及子类,包括UIResponder的子类UIView、UIViewController、UIWindow、UIApplication,当iOS App接收到触摸事件时,UIKit会自动的找到最合适的第一响应者。没有处理的事件会沿着当前激活状态的响应者链条传递下去。

  如下图所示,这是app中默认的事件响应链条,如果在UILabel上触发了为处理的事件,那么这个事件会传递给label的父视图UIView,然后是UIWindow对象。对于根视图而言,事件会先传递给UIViewController,然后才是window。如果UIWindow也没有处理,那就会传递给UIApplication,application也未处理的话,如果application的代理对象是UIResponder子类并且没有出现在之前的响应者链条中。

  当用户触摸屏幕时,UIKit会根据默认规则来找到第一事件响应者,此规则是基于hit-testing来决定的。UIKit会在touch发生的view的视图层级中比较touch location与View 的bounds。 hitTest:withEvent:方法会遍历整个视图层级找到最深层次的包含此次触摸的子视图,这个子视图就会是第一事件响应者。

  

  21.RunLoop是什么? 使用RunLoop的目的是什么?何时使用?使用要注意些什么?

  RunLoop是一个事件处理的循环,这个循环会不停的从一个地方收到事件,收到事件就做相应的处理,只有收到退出事件时,这个循环才会退出。

  在iOS的开发中,主线程的RunLoop会自动创建,辅助线程的RunLoop只有在主动获取时才会被创建。

  RunLoop由RunLoopMode构成,RunLoopMode又由Timer/Source/Observer构成。RunLoop同时只能够运行在一个Mode下,app主线程的 RunLoop 里有两个预置的 Mode:kCFRunLoopDefaultMode 和 UITrackingRunLoopMode。当TableView处于滑动过程中时,RunLoop的mode为UITrackingRunLoopMode,其余时间Mode为:kCFRunLoopDefaultMode。

  在app开发中默认用到RunLoop的地方有:NSTimer、NSObject提供的performSelectorOnMainThread:withObject:waitUntilDone:方法。当我们使用NSTimer在指定时间执行时,其实是在RunLoop中添加这个timer,并在到达指定的时间点后执行回调。NSObject执行perform等方法的时候同样是在指定时间来执行那个回调,只是在执行perform方法是当前线程必须要存在RunLoop才行,不然无效。

  系统中使用RunLoop的地方有AutoReleasePool,RunLoop会在每一次进入RunLoop时创建自动释放池,然后在RunLoop进入waiting状态时释放旧的释放池并重新创建自动释放池,最后在ExitRunLoop时释放自动释放池。

  在日常的iOS开发中,默认创建的Timer是添加在kCFRunLoopDefaultMode模式下的,所以在滑动TableView时,RunLoop会切换为UITrackingRunLoopMode,timer会被暂停。要想Timer能够在UITrackingRunLoopMode下正常运行有两种办法,一是将Timer分别添加到以上两种Mode中,二是将 Timer 加入到顶层的 RunLoop 的 “commonModeItems” 中。”commonModeItems” 被 RunLoop 自动更新到所有具有”Common”属性的 Mode 里去。

  22.说说你对线程和进程的理解?

  总的来说,进程是程序分配资源的最小单元,线程是程序运行的最小单元,进程可以有多个线程组成。

  23.对大量数据列表有什么优化方案?

   优化1. 利用UITableView重用cell的机制

   优化2. 分批次异步加载数据

   优化3. 缓存高度

   优化4. 将耗时操作放在异步线程来做

  24.平时工作中使用的动画库有哪些?

  faceBook的pop动画、

  25.objc实现多重继承

  oc不支持多重继承,只支持多层继承。要变相的实现多重继承,可以利用protocol来实现

  26.数组查找平衡点

  

int findBalancePoint(int a[], int n)
{
    if (n == 1) {
        return -1;
    }
    int leftSum = 0;
    int rightSum = 0;
    for (int i = 0, j = n - 1; ; i++, j--) {
        leftSum += a[i];
        rightSum += a[j];
        if (i < j) {//继续加
            continue;
        }else{
            if (leftSum == rightSum) {//相等
                if (i == j) {//奇数个
                    return i + 1;
                }else{//偶数个
                    return n;
                }
            }else{//不等
                return -1;
            }
        }
    }
    return -1;
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        int a[] = {-7, 1, 5, 2, -4, 3, 0};
        int b[] = {1, 2, 3, 3, 2, 1};
        
        int found1 = findBalancePoint(a, 7);
        int found2 = findBalancePoint(b, 6);
        NSLog(@"%i %i", found1, found2);
        
    }
    return 0;
}

  

 

 

 

    

转载于:https://www.cnblogs.com/pretty-guy/p/8397843.html

Guess you like

Origin blog.csdn.net/weixin_33979203/article/details/93199963