像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
- 下面代码输出结果是什么?
- (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:会不会被阻塞
- 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常见的优化怎么做。