zoukankan      html  css  js  c++  java
  • iOS中的一些细节

    1.

    在使用单例模式时一般使用allocWithZone

    因为alloc最终还是会调用allocWithZone进行分配空间

    2.

    synchronized

    线程锁(互斥锁)

    优点:能防止多线程抢夺资源造成的数据安全问题

    缺点:需要消耗大量的CPU资源

    3.

    GCD的队列类型

    并发队列:自己创建的,全局

    串行队列:主队列,自己创建的

    4.

     NSOperationQueue的队列类型

     1.主队列[NSOperationQueue mainQueue]

     2.其他队列(串行,并发):[[NSOperationQueue alloc] init];

    5.

    沙盒目录中的文件

     Documents(不要将大文件放这里,否则有可能appstore审核不通过)

     Library

        -Caches (除非用户删掉,否则一直存在)

        -Preference

     tmp(随时有可能清空)

    6.

            //获取沙盒路径
            NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
            //获得文件名
            NSString *filename = [app.icon lastPathComponent];
            //计算出文件的全路径
            NSString *file = [cachesPath stringByAppendingPathComponent:filename];
            //加载沙盒中的数据
            NSData *data = [NSData dataWithContentsOfFile:file];

    7.

    如何使一个线程常驻?

        [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];

    使这个线程开启RunLoop,并给它添加Source即可

    8.

    自动释放池什么时候释放?

    在RunLoop唤醒时创建自动释放池,

    在RunLoop休眠之前会释放 

    9.什么是RunLoop?

    运行循环,跑圈,

    其实它的内部就是do-while循环,在这个循环内部不断处理各种任务

    (比如Timer,Source,Observer)

    一个线程对应一个RunLoop,主线程的RunLoop默认已经启动,

    子线程的RunLoop是手动启动(调用run方法)

    RunLoop只能选择一个Mode启动,如果当前模式中没有Source,Timer,Observer,

    那么就直接退出RunLoop。

     10.在开发中如何使用RunLoop?什么应用场景?

    开启一个常驻线程(让一个子线程不进入消亡状态,等待其他线程发来消息,处理其他事件)

    在子线程中开启一个定时器

    在子线程中进行一些长期监控

    可以控制定时器在哪种模式下运行

    可以让某些事件在特定模式下执行

    11.

    NSString与NSData的相互转换

    NSData *data = [@"str" dataUsingEncoding:NSUTF8StringEncoding];

    NSString *str = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];

    12.

    在使用字典类别时,如何遍历字典中的元素

        //遍历的所有的元素
        [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
           
            [string appendFormat:@"	%@",key];
            [string appendString:@" : "];
            [string appendFormat:@"%@,
    ",obj];
            
        }];

    13.

    NSHTTPURLResponse是NSURLRequest的子类

    获取请求头,响应码,从这个类中获取。

    14.

    获取MIMEType类型

        NSString *url = @"file:///Users/DDZ/Desktop/ARC.png";
        
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
        
        NSURLResponse *response = nil;
        [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
        
        NSLog(@"%@",response.MIMEType);

     15.

    NSURLConnection的一些坑

    NSURLConnection在主线程发送请求,正常可以接收服务器回传的data

    但是如果将其放在子线程中,将接收data失败

    原因:

    因为主线程中有RunLoop,它会自动关联起来,来不断的接收data

    子线程没有RunLoop,所以接收失败,

    如果启动子线程的RunLoop则可以正常使用

  • 相关阅读:
    VS2008编写MFC程序--使用opencv2.4()
    November 02nd, 2017 Week 44th Thursday
    November 01st, 2017 Week 44th Wednesday
    October 31st, 2017 Week 44th Tuesday
    October 30th, 2017 Week 44th Monday
    October 29th, 2017 Week 44th Sunday
    October 28th, 2017 Week 43rd Saturday
    October 27th, 2017 Week 43rd Friday
    October 26th, 2017 Week 43rd Thursday
    October 25th, 2017 Week 43rd Wednesday
  • 原文地址:https://www.cnblogs.com/langji/p/5319641.html
Copyright © 2011-2022 走看看