美文网首页技术
iOS面试官最喜欢叫你书写的东西

iOS面试官最喜欢叫你书写的东西

作者: 郑莫轩 | 来源:发表于2016-01-18 16:03 被阅读155次
    写出一个单例
    /**
     *  写出一个单例
     */
    static ViewController *view = nil;
    + (instancetype)sharedManager
    {
        // &表示取地址符,这个是定义一个静态变量,然后在dispatch_once函数第一次运行时写入数据,之后就不会再次写入,可以保证后面block函数内部的代码只被执行一次
        static dispatch_once_t patch;
        dispatch_once(&patch, ^{
            view = [[self alloc] init];
        });
        return view;
    }
    
    KVO及KVC
    static Model *model = nil;
    - (void)KVO
    {
        // KVO,即:Key-Value Observing.
        // 它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知。
        // 简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应的观察者了。
    
        model = [[Model alloc] init];
        [model addObserver:self
                forKeyPath:@"sName"
                   options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
                   context:NULL];
    }
    
    // KVO 函数回调
    - (void)observeValueForKeyPath:(NSString *)keyPath
                          ofObject:(id)object
                            change:(NSDictionary<NSString *,id> *)change
                           context:(void *)context
    {
        // keyPath:被修改Key的名称
        // object:回调该函数的本身的实例
        // change:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)返回监听Key的新(new)跟旧(old)值
        // context:忽略改值
    }
    
    
    // KVC的调用
    - (void)KVC
    {
        // KVC 是 Key Value Coding 的简称.
        // 它是一种可以直接通过字符串的名字(key)来访问类属性(实例变量)的机制。
        // 而不是通过调用Setter、Getter方法访问。
        [model setValue:@"243" forKey:@"sName"];
    }
    
    NSNotification通知
    // 发送通知
    - (void)postNotification
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"notification" object:nil userInfo:@{@"key":@"value"}];
    }
    
    // 注册通知
    - (void)addNotification
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotoNotification:) name:@"notification" object:nil];
    }
    
    - (void)gotoNotification:(NSNotification *)notification
    {
        NSLog(@"%@",notification.userInfo);
    }
    
    手势
    - (void)gestrue
    {
        // 手势对应的操作是:
        // UITapGestureRecognizer              Tap(点一下)                                  /tæp/
        // UIPinchGestureRecognizer            Pinch(二指往內或往外拨动,平时经常用到的缩放)     /pɪntʃ/
        // UIRotationGestureRecognizer         Rotation(旋转)                               /ro'teʃən/
        // UISwipeGestureRecognizer            Swipe(滑动,快速移动)                         /swaɪp/
        // UIPanGestureRecognizer              Pan (拖移,慢速移动)                          /pæn/
        // UILongPressGestureRecognizer        LongPress(长按)                             /ˈlɔ:ŋˈreɪndʒ/
    
        UITapGestureRecognizer        *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(aaa)];
    
        UIPinchGestureRecognizer      *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(aaa)];
    
        UIRotationGestureRecognizer   *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(aaa)];
    
        UISwipeGestureRecognizer      *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(aaa)];
    
        UIPanGestureRecognizer        *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(aaa)];
    
        UILongPressGestureRecognizer  *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(aaa)];
    }
    
    GCD
        // GCD优先级:DISPATCH_QUEUE_PRIORITY_DEFAULT   默认的
        //           DISPATCH_QUEUE_PRIORITY_HIGH      最高的
        //           DISPATCH_QUEUE_PRIORITY_LOW       最低的
    
        // 异步 提交的任务立刻返回,在后台队列中执行
        // 同步 提交的任务在执行完成后才会返回
        // 并行执行(全局队列) -- dispatch_get_global_queue
        // 串行执行(用户创建队列) -- dispatch_queue_create
    
        // 开启一条子线程(并行队列 异步加载)
           dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        });
    
        // 开启一条子线程(并行队列 同步加载)
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        });
    
    
        dispatch_queue_t queue = dispatch_queue_create("GCD", nil);
    
        // 开启一条子线程(串行队列 异步加载)
        dispatch_async(queue, ^{
        
        });
    
        // 开启一条子线程(串行队列 同步加载)
        dispatch_sync(queue, ^{
        
        });
    
        // 开启主线程
        dispatch_async(dispatch_get_main_queue(), ^{
        
        });
    
        // 延迟2秒执行:
        double delayInSeconds = 2.0;
        dispatch_time_t popTime =     dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        
        });   
    }
    
    Delegate写法
    // 在.h 文件中这样写
    @protocol AppleDelegate <NSObject>
    
    - (void)ThisIsADelegate;
    
    @end
    
    @property (nonatomic, assign) id<AppleDelegate>    delegate;
    
    // 在.m 文件中这样写
    // 代理 Delegate 的调用
    - (void)executeDelegate
    {
    if (_delegate && [_delegate respondsToSelector:@selector(ThisIsADelegate)])
        {
        
        }
    }
    
    Block的写法
    // 在.h 文件中这样写
    /**
     *  Block 定义
     *
     *  @param arguments
     *
     *  @return void
     */
    typedef void(^AppleBlock)(NSString *sBlock);
    // void       -- 返回参数
    // AppleBlock -- Block函数名
    // sBlock     -- 回调的参数
    
    // 在.m 文件中这样写
    // Block 的调用
    - (void)executeBlock
    {
        if (_block) {
            _block(@"Block");
        }
    }
    
    排序算法

    九大排序:
    插入排序、选择排序、冒泡排序、堆排序、快速排序。(不占用额外内存或占用常数的内存)
    归并排序、计数排序、基数排序、桶排序。

    插入排序:
    // 1.插入排序—直接插入排序(Straight Insertion Sort)
    // 基本思想:
    // 将一个记录插入到已排序好的有序表中,从而得到一个新,记录数增1的有序表。
    // 即:先将序列的第1个记录看成是一个有序的子序列,然后从第2个记录逐个进行插入,直至整个序列有序为止。
    
        NSMutableArray *array1 = [NSMutableArray arrayWithObjects:
                                 [NSNumber numberWithInt:3],
                                 [NSNumber numberWithInt:1],
                                 [NSNumber numberWithInt:5],
                                 [NSNumber numberWithInt:7],
                                 [NSNumber numberWithInt:2],
                                 [NSNumber numberWithInt:4],
                                 [NSNumber numberWithInt:9],
                                 [NSNumber numberWithInt:6], nil];
    
        for (int i = 1; i < array1.count; i ++) {
            if (array1[i] < array1[i - 1]) {         //若第i个元素大于i-1元素,直接插入。小于的话,移动有序表后插入
                int j = i - 1;
                id x = array1[i];                   //复制为哨兵,即存储待排序元素
                array1[i] = array1[i - 1];           //先后移一个元素
                while (x < array1[j]) {             //查找在有序表的插入位置
                    array1[j + 1] = array1[j];
                    j --;                          //元素后移
                    if (j < 0) {
                        break;
                    }
                }
                array1[j + 1] = x;                  //插入到正确位置
            }
            NSLog(@"array=%@\n",array1);            //打印每趟排序的结果
        }
    
    插入排序—希尔排序:
    // 2. 插入排序—希尔排序(Shell`s Sort)
    // 希尔排序是1959 年由D.L.Shell 提出来的,相对直接排序有较大的改进。希尔排序又叫缩小增量排序
    // 基本思想:
    // 先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录“基本有序”时,再对全体记录进行依次直接插入排序。
    
    选择排序—简单选择排序:
    // 3. 选择排序—简单选择排序(Simple Selection Sort)
    // 基本思想:
    // 在要排序的一组数中,选出最小(或者最大)的一个数与第1个位置的数交换;
    // 然后在剩下的数当中再找最小(或者最大)的与第2个位置的数交换,依次类推,直到第n-1个元素(倒数第二个数)和第n个元素(最后一个数)比较为止。
    NSMutableArray *array2 = [NSMutableArray arrayWithObjects:
                                  [NSNumber numberWithInt:3],
                                  [NSNumber numberWithInt:1],
                                  [NSNumber numberWithInt:5],
                                  [NSNumber numberWithInt:7],
                                  [NSNumber numberWithInt:2],
                                  [NSNumber numberWithInt:4],
                                  [NSNumber numberWithInt:9],
                                  [NSNumber numberWithInt:6], nil];
        for (int i = 0; i < array2.count; i ++) {
            id temp;
            for (int j = i + 1; j < array2.count; j ++) {
                if (array2[i] > array2[j]) {
                    temp = array2[i];
                    array2[i] = array2[j];
                    array2[j] = temp;
                }
            }
            NSLog(@"%@",array2);
        }
    
    交换排序—冒泡排序:
    // 4. 交换排序—冒泡排序(Bubble Sort)
    // 基本思想:
    // 在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。
    // 即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换。
    NSMutableArray *array3 = [NSMutableArray arrayWithObjects:
                                  [NSNumber numberWithInt:3],
                                  [NSNumber numberWithInt:1],
                                  [NSNumber numberWithInt:5],
                                  [NSNumber numberWithInt:7],
                                  [NSNumber numberWithInt:2],
                                  [NSNumber numberWithInt:4],
                                  [NSNumber numberWithInt:9],
                                  [NSNumber numberWithInt:6], nil];
    
        for(int i = 0; i < array3.count; i++)
        {
            for(int j = 0; i + j < array3.count - 1; j++)
            {
                if(array3[j] > array3[j + 1])
                {
                    id temp = array3[j];
                    array3[j] = array3[j + 1];
                    array3[j + 1] = temp;
                }  
            }
            NSLog(@"%@",array3);
        }
    
    其他的排序请参考 http://blog.csdn.net/hguisu/article/details/7776068

    未完待续

    相关文章

      网友评论

        本文标题:iOS面试官最喜欢叫你书写的东西

        本文链接:https://www.haomeiwen.com/subject/skykkttx.html