iOS 面试集锦

1. OC 语言的优缺点

优点:

    1. category
    1. posing
    1. 动态特性
      runtime: 函数实现 IMP 动态绑定, resolve
    1. 指标计算
    1. 弹性讯息传递
    1. 不是一个过度复杂的 C 衍生语言
    1. Objective_C 与 C++可以混编;
      缺点:
    1. 不支持元命名空间
    1. 不支持运算符重载
    1. 不支持多重继承
    1. 使用动态运行时类型, 所有方法都是函数调用, 所有很多编译时优化方案, 都用不到(如内联函数), 性能低劣!

2. OC 消息发送机制

    // 1.方法调用
    ViewController *VC = [ViewController new];
    [VC testCommand];
    
    // 2. 通过objc_msgSend 函数调用
    ViewController *VC1 = ((ViewController *(*)(id, SEL))objc_msgSend)([ViewController class], @selector(alloc));
    VC1 = ((ViewController *(*)(id, SEL))objc_msgSend)(VC1, @selector(init));
    ((ViewController *(*)(id, SEL))objc_msgSend)(VC1, @selector(testCommand));
    
    // 3. 通过 performSelector map 映射
    [VC performSelector:@selector(testCommand) withObject:@"132"];

3. OC 消息转发机制

@interface ADDIMP :NSObject
- (void)testCommand1;
@end

@implementation ADDIMP
- (void)testCommand1 {
    NSLog(@"%s\n我是新的 IMP ", __FUNCTION__);
}
@end

@interface ViewController ()
 // 为了能编译通过, 在此声明这个方法
- (void)testCommand1;
@end

@implementation ViewController
// 自定义函数 IMP
void dynamicMethodIMP(id self, SEL _cmd)
{
    printf("%s", __func__);
}

// 1. 动态方法解析, 通过 runtime 添加方法 IMP
+ (BOOL)resolveInstanceMethod:(SEL)sel {
    NSLog(@"sel =%@", NSStringFromSelector(sel));
//    if (sel == NSSelectorFromString(@"testCommand1")) {
//        NSLog(@"%s\n绑定我自己的 IMP ", __FUNCTION__);
//        class_addMethod(self, sel, (IMP)dynamicMethodIMP, "v@:");
//        return YES;
//    }
    return [super resolveInstanceMethod:sel];
}

// 2. 快速消息转发
- (id)forwardingTargetForSelector:(SEL)aSelector {
    NSLog(@"%s\n绑定别人的 IMP ", __FUNCTION__);
    
//    if (aSelector == NSSelectorFromString(@"testCommand1")){
//
//        return [ADDIMP new];
//    }
    return [super forwardingTargetForSelector:aSelector];
 
}

// 3. 标准的消息转发
// 3.1 方法签名
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    if (aSelector == NSSelectorFromString(@"testCommand1")){
        NSMethodSignature *sign = [NSMethodSignature signatureWithObjCTypes:"v@:"];
        return sign;
    }
    return [super methodSignatureForSelector:aSelector];
}

// 3.2 开始转发
- (void)forwardInvocation:(NSInvocation *)anInvocation {
    SEL aSelector = [anInvocation selector];
    
    ADDIMP *add = [ADDIMP new];
    if ([add respondsToSelector:aSelector])
        [anInvocation invokeWithTarget:add];
    else
        [super forwardInvocation:anInvocation];
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self testCommand1];
}
@end

4. OC 模拟多重继承

// SubClass.h

#import <Foundation/Foundation.h>

@protocol ClassOne_protocol
- (void)ClassOneString:(NSString *)string;
@end

@interface ClassOne : NSObject <ClassOne_protocol>

@end

@protocol ClassTwo_protocol
- (void)ClassTwoString:(NSString *)string;
@end
@interface ClassTwo : NSObject<ClassTwo_protocol>

@end

@interface SubClass : NSProxy <ClassOne_protocol, ClassTwo_protocol>
+ (instancetype)subClass;
@end

// SubClass.m

#import "SubClass.h"
#import <objc/message.h>
 
@implementation ClassOne
- (void)ClassOneString:(NSString *)string {
    NSLog(@"\n%s\n ClassOneString\n", __FUNCTION__);
}
@end
 
@implementation ClassTwo
- (void)ClassTwoString:(NSString *)string {
    NSLog(@"\n%s\n ClassTwoString\n", __FUNCTION__);
}
@end
 
@implementation SubClass{
    NSMutableDictionary *_methodMap;
}

+ (instancetype)subClass {
    return [[SubClass alloc] init];
}

- (instancetype)init { 
    _methodMap = [NSMutableDictionary dictionary];
    
    [self registerMethodsWithTarget:[ClassOne new]];
    [self registerMethodsWithTarget:[ClassTwo new]];
    
    return self;
}

- (void)registerMethodsWithTarget:(id)target {
    unsigned int count = 0;
    
    // 获取方法列表
    Method *methodList = class_copyMethodList([target class], &count);
    
    // 依次添加方法
    for (NSInteger i = 0; i < count; i++) {
        
        Method method = methodList[i];
        
        SEL sel = method_getName(method);
        [_methodMap setObject:target forKey:NSStringFromSelector(sel)];
    }
    
    // dealloc 中不会提醒
    free(methodList);
}
 
// 标准的消息转发机制, 动态添加方法
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    
    // 1. 获取方法名
    NSString *methodName = NSStringFromSelector(aSelector);
    
    // 2. 方法目标对象
    id target = [_methodMap objectForKey:methodName];
    
    // 3. 开始对方法签名
    if(target && [target respondsToSelector:aSelector]) {
        return [target methodSignatureForSelector:aSelector];
    }
    return [super methodSignatureForSelector:aSelector];
}

// 开始转发
- (void)forwardInvocation:(NSInvocation *)anInvocation {
    SEL sel = anInvocation.selector;
    NSString *methodName = NSStringFromSelector(sel);
    id target = [_methodMap objectForKey:methodName];
    
    if (target && [target respondsToSelector:sel]) {
        // 执行
        [anInvocation invokeWithTarget:target];
    }else
    [super forwardInvocation:anInvocation];
}

@end

// 测试

   SubClass *subClass = [SubClass subClass]; 
   [subClass ClassOneString:@"111111111"];
   [subClass ClassTwoString:@"222222222"];
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,655评论 8 265
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,148评论 1 32
  • 1、NSThread/NSOperation/GCD 三种多线程不同,分别使用场景? •NSThread: –优点...
    懒人09阅读 365评论 0 4
  • 1,weak的底层原理http://www.jianshu.com/p/13c4fb1cedea 2,wkwebv...
    RobinYu阅读 260评论 0 0
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,219评论 30 472