Skip to content

Instantly share code, notes, and snippets.

@edwardean
Last active November 18, 2018 08:04
Show Gist options
  • Save edwardean/508dfa5b124d546957bb667e410a74f5 to your computer and use it in GitHub Desktop.
Save edwardean/508dfa5b124d546957bb667e410a74f5 to your computer and use it in GitHub Desktop.
iOS review

像strong, retain, copy,block引用循环这些常见的这里不再赘述。

  • 下面代码输出结果
- (void)viewDidLoad {
    [super viewDidLoad];

    dispatch_sync(dispatch_get_global_queue(0, 0), ^{
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            NSLog(@"1");
        }];
        
        [[NSOperationQueue currentQueue] addOperationWithBlock:^{
            NSLog(@"2");
        }];
        
        __block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"MyNotif" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
            NSLog(@"Receive Notification");
            [[NSNotificationCenter defaultCenter] removeObserver:observer];
        }];
        
        [[NSOperationQueue currentQueue] addOperationWithBlock:^{
            NSLog(@"3");
        }];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotif" object:self];
        
        [[NSOperationQueue currentQueue] addOperationWithBlock:^{
            NSLog(@"4");
        }];
    });

正确结果:Receive Notification 1 2 3 4

  • 下面代码的输出结果是什么?
- (void)log1
{
    NSLog(@"-----------$$log1");
}

- (void)log2
{
    NSLog(@"-----------$$log2");
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self performSelector:@selector(log2) withObject:nil afterDelay:0];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self log1];
    });
}

-----------$$log1
-----------$$log2

ref

  • 下面代码输出结果是什么?
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self method1];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self performSelector:@selector(method2) withObject:nil afterDelay:0];
    });
    [self method3];
}

- (void)method1
{
    NSLog(@"method1");
}

- (void)method2
{
    NSLog(@"method2");
}

- (void)method3
{
    NSLog(@"method3");
}

正确答案:method1 method3。 method2不会输出。考察子线程没有默认开启runLoop的知识点。 - (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay; 默认是在defaultMode的runLoop中执行。

  • NSNotification是同步还是异步

正确答案:同步(和第一题考察的点差不多) http://stackoverflow.com/questions/16298671/are-nsnotificationcenter-events-received-synchronously-or-asynchronously

可以做一个实验:在监听通知的方法里写一个很耗时的方法,然后看NSNotificationCenter的postNotificationName:object:会不会被阻塞

wiki

  • UIButton的继承链:

正确答案:UIResponder->UIView->UIControl->UIButton

  • MD5和Base64都是用来做什么的,区别是什么? (说加密的下面不要接着问了)

  • isKindOfClass和isMemberOfClass的区别

  • 什么是 KVC 和 KVO?KVC 查找方法的顺序?什么时候系统会调用 valueForUndefinedKey ?

  • NSMapTable,NSDictionary,NSCache的异同,NSHashTable和NSArray的异同

  • 举一个利用NSInvocation调用方法的步骤

  • 如果暂停动画再开始: (主要考察CALayer的speed,beginTime和timeOffset属性)

  • 设计一个能够显示屏幕刷新率的工具类: (主要考察CADisplayLink)

  • 调用一个block块对象之前要对该对象先判空,否则会crash,为什么会crash, crash时的内存地址是0xc,背后的原理是什么

void (^block)() = nil;
    block();

http://stackoverflow.com/questions/4145164/why-do-nil-null-blocks-cause-bus-errors-when-run

  • strlen([@"💩" UTF8String]) 和 [@"💩" length]算出来的值相等吗?

不相等(主要考察NSString的length是算的UTF-16编码的长度) http://boredzo.org/blog/archives/2012-06-03/characters-in-nsstring

  • NSDateFormatter是线程安全吗,使用的正确姿势是?

(主要考察NSDateFormatter创建消耗比较大,如果能答出用C语言time_t结构体来替换NSDateFormatter就更赞了)

  • 如何自己实现抓crash的工具,发生crash时不让app崩溃怎么做?

(抓取crash考察NSSetUncaughtExceptionHandler和singal的用法, crash发生不让app崩溃考察runloop的使用原理)

  • 下面代码块的能正常运行吗?为什么?
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    dispatch_async(dispatch_get_main_queue(), ^{
        [webView stringByEvaluatingJavaScriptFromString:@"alert(hello)"];
    });

(这段代码会死锁,考察dispatch_async的原理)

  • 下面代码块运行结果是什么?为什么?
 - (BOOL)doSomethingWithError:(NSError **)error {
    __block BOOL success = YES;
    [@[@1] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == 0) {
            success = NO;
            if (error) {
                *error = [NSError errorWithDomain:@"com.custom.error" code:-1 userInfo:nil];
            }
        }
    }];
    
    return success;
}
/////////////////////////
    NSError *error = nil;
    [self doSomethingWithError:&error];
    NSLog(@"%@", error);

(这段代码会crash,考察__autorelease对象和enumerateObjectsUsingBlock代码块中自动添加的@autoreleasePool)

  • UIView和CALayer的关系?

能答出来前者是后者的代理基本就算过了。

  • (上一题答对的情况下问这一题)

分别改变UIView和CALayer的hidden属为什么UIView没有隐式动画而CALayer有?

  • 下面两段代码分别输出什么结果?
### OC
  NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@1,@2,@3,@4,nil];
    for (NSNumber *number in mutableArray) {
        NSLog(@"%@",number);
        if (number.unsignedIntegerValue == 1) {
            [mutableArray addObject:@5];
        }
    }
### Swift
   var mutableArray = [1,2,3,4]
   for number in mutableArray {
       print("\(number)")
         if number == 1 {
            mutableArray.append(5)
         }
    }

(为什么OC的代码crash而Swift的没事)

  • 下面的代码段运行结果是什么?
+ (instancetype)sharedInstance {
    static LHDefaultManager *_sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[self alloc] init];
        [[LHDefaultManager sharedInstance] callMe];
    });
    return _sharedInstance;
}

- (void)callMe {
    NSLog(@"callMe");
}

(这段代码会死锁,考察dispatch_once原理)

  • 简述__bridge_retained,__bridge_transfer,__bridge的区别和各自的使用场景?

  • 简述一下利用dispatch_source实现定时器功能的实现方式。

  • NSURLProtocol用来干什么?

  • __block关键字在MRC和ARC下的含义一样吗?

  • 讨论一下平时App常见的优化怎么做。

  • 过滤App所有网络请求中图片请求

  • 如何统计App中的网络流量

  • 如何判断一个网页内容是完整的

  • 客户端请求用户头像,图片URL是固定的,但是在后端图片内容可能随时会被替换,客户端怎么保证头像图片正常显示?

  • 如何自己设计一个BAD_EXCESS野指针定位功能

  • 如果设计一个查找内存循环引用的功能

  • 项目中引入两个相同的静态库会发生什么,怎么解决?

  • 在一些对时间精确度要求高的页面如何做本地与服务端时间同步

  • 怎么通过一个view找到它的viewController

  • 如何扩大按钮点击区域

  • 用autoLayout实现两个view竖直排列在一个父view上,保证从上往下三个间距相等怎么实现

  • 手写单例,block循环引用,字符串反转,

  • 求100以内所有质数的代码:

for (int a = 1; a<=100; a++) {	
 BOOL finisned = NO;
 for (int b = 2; b<=sqrt(a); b++) {
	 if (a%b == 0) {
	   finisned = YES;
	 }
  }
  
 if (!finisned) {
  NSLog(@"%d ",a);
 }
}
  • 滑动窗口 tcp慢启动特性

  • 断点续传怎么实现

  • 如果要向后端一次性上传多张图片,你会怎么处理

  • weak的实现

  • OC的runtime中已知一个objc_property_t怎么判断它对应的属性是strong强引用的?

  • ARC中内存泄露的场景有哪些

  • 单例模式的弊端

  • NSTimer使用时有哪些需要注意的地方?

  • 不用NSTimer的话怎么自己实现一个定时器功能?

  • 列举一个GCD中死锁的例子

  • dispatch_after是一段时间后执行block还是一段时间后将block派发到队列上

  • dispatch_apply是同步还是异步的

  • OC完整的消息转发过程

  • 如何热修复hook一个Swift方法

  • 网络劫持怎么解决?

  • SDWebImage图片缓存原理

  • 如何hook知道系统私有类的方法,这个私有类不能保证在App启动时已经被加载。

  • swift中的weak和OC中的weak的实现有什么不一样

  • 怎么给swift类实现像OC中+ (void)load; 和+ (void)initialize;两个方法一样的功能?

  • 如果想要在App发生崩溃后进行截屏该怎么做?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment