zoukankan      html  css  js  c++  java
  • 如何找到根控制起找到根控制器及获得文件夹路径

     // 全局队列 异步下载图片

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

        dispatch_async(dispatch_get_global_queue(0, 0), ^{

            NSLog(@"%@", [NSThread currentThread]);

            // 耗时操作: 放在全局队列,异步执行

            // 1. url, 确定一个网络上的资源路径

            NSURL *url = [NSURL URLWithString:@"http://fe.topit.me/e/d1/12/1170068721aa112d1el.jpg"];

            // 2. 通过url可以下载对应的网络资源, 网络资源传输的都是二进制

            NSData *data = [NSData dataWithContentsOfURL:url];

            // 3. 二进制转成图片

            UIImage *image = [UIImage imageWithData:data];

            // 4. 更新UI,在主线程-》 直接把任务添加到主队列,就会在主队列执行

            dispatch_async(dispatch_get_main_queue(), ^{

                self.iconView.image = image;

                NSLog(@"-----%@", [NSThread currentThread]); 

            });

        });

    }

    //1.获取plist文件的路径

    SString *listPath=[[NSBundle mainBundle] pathForResource:@"textList1.plist" ofType:nil];

    //2.字典初始化--根据一个文件初始化字典

    NSDictionary *dict=[NSDictionary dictionaryWithContentsOfFile:listPath];

    /**NSString字符串特性*/

      [[UITabBar appearance] setTintColor:RGBA_COLOR(18, 191, 195, 1.0)];

    // 过滤掉输入的特色字符串的

    NSString *string = [textField.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /* 清除缓存中存储的缓存*/

    -(void)didReceiveMemoryWarning{

        [super didReceiveMemoryWarning];

        // 需要在这里做一些内存清理工作,如果不清,会被系统闪退

        // 清理图片的缓存

        [self.imageCache removeAllObjects];

        // 清理操作的缓存

        [self.operationCache  removeAllObjects];

           // 取消下载队列里面的任务

        [self.opQueue cancelAllOperations];

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    //显示的创建线程

    // 又创建了一个新的线程

    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];

    thread.name = @"线程A"; // 线程

    [thread start];

    //使线程放到可调度线程池,处于就绪状态,等待被cup调用

    // 隐世线程

    [self performSelectorInBackground:@selector(longTimeOperation) withObject:nil];

     //隐式的创建方式

    [self performSelectorInBackground:@selector(run2:) withObject:@"DF"];

     // 类方法

    [NSThread detachNewThreadSelector:@selector(run2:) toTarget:self withObject:@"Hello"]

     // 对象方法

    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /**获得当前机器的版本**/

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        

       if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {

            // 注册推送通知(本地通知/远程通知)

            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];

            [application registerUserNotificationSettings:settings];

            // 获取DeviceToken

            [application registerForRemoteNotifications];

        } else {

            [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];

        }

        

        if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {

            // 写跳转代码即可

        }

        

        return YES;

    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /*story加载控制*/

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Common" bundle:nil];

    CMLaunchAdViewController *adVC = [storyboard instantiateViewControllerWithIdentifier:@"CMLaunchAdViewController"];

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Common" bundle:[NSBundle mainBundle]];

    // 如何找到根控制起找到根控制器

    UINavigationController *navigationController =(UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;

    // 先判断上次缓存的二级广告开关值

    //全局并发队列

    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    //主队列: 属于串行队列

    dispatch_queue_t mainQueue = dispatch_get_main_queue();

    //定时循环执行事件

    //dispatch_source_set_timer 方法值得一提的是最后一个参数(leeway),他告诉系统我们需要计时器触发的精准程度。所有的计时器都不会保证100%精准,这个参数用来告诉系统你希望系统保证精准的努力程度。如果你希望一个计时器每5秒触发一次,并且越准越好,那么你传递0为参数。另外,如果是一个周期性任务,比如检查email,那么你会希望每10分钟检查一次,但是不用那么精准。所以你可以传入60,告诉系统60秒的误差是可接受的。他的意义在于降低资源消耗。

    // 在全局队列里面

    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, globalQueue);

    // 设置隔多久执行时间

    dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0.0 * NSEC_PER_SEC);

    // 执行事件

    dispatch_source_set_event_handler(_timer, ^{ //计时器事件处理器

        DLog(@"Event Handler");

        if (timeout <= 0) {

            //

            dispatch_source_cancel(_timer); //取消定时循环计时器;使得句柄被调用,即事件被执行

            // 主队列异步执行

            dispatch_async(mainQueue, ^{

                if (![CMAdManager shareInstance].launchAdClicked) {

                    if (!self.skipButton.selected) {

                        [self skipAction:self.skipButton];

                    }

                }

            });

        } else {

            NSString *strTime = [NSString stringWithFormat:@"%d S跳过", timeout];

            

            // 在主队列里面执行

            dispatch_async(mainQueue, ^{

                [self.skipButton setTitle:strTime forState:UIControlStateNormal];

            });

            timeout--;

        }

    });

    dispatch_source_set_cancel_handler(_timer, ^{ //计时器取消处理器;调用 dispatch_source_cancel 时执行

        DLog(@"Cancel Handler");

    });

    // 开启定时器

    dispatch_resume(_timer);

    //恢复定时循环计时器;Dispatch Source 创建完后默认状态是挂起的,需要主动恢复,否则事件不会被传递,也不会被执行

    ///////////////////////////////////////////////////////////////////////////////////////

    /*数组便利*/

    [self.adList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        CMAdObject *adObj = (CMAdObject *)obj;

        if ([adObj.imageUrl isEqualToString:object.imageUrl])

        {

            sameImg = YES;

        }

    }];

    /*USerDefalut*/

    [userDefaults setObject:ad.Adid forKey:kLaunchAdID];

    [userDefaults synchronize];

    ///////////////////////////////////////////////////////////////////////////////////////

    // 总大小

    unsigned long long size = 0;

    // 获得缓存文件夹路径

    NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;

    // 文件管理者

    NSFileManager *fileManager = [NSFileManager defaultManager];

    // 包含的文件数

    NSLog(@"%@", [mgr contentsOfDirectoryAtPath:cachesPath error:nil]);

    // 子文件夹                                            是component

    NSString *dirpath = [cachesPath stringByAppendingPathComponent:@"Mp3/MySister"];

    // 子文件夹包含的文件数

    NSLog(@"%@", [fileManage contentsOfDirectoryAtPath:dirpath error:nil]);

    // 获得文件夹的属性

    // 文件的创建时间、大小、位置

    NSDictionary *dict = [mgr attributesOfItemAtPath:dirpath error:nil];

    NSLog(@"%@",dict);

    size = [mgr attributesOfItemAtPath:dirpath error:nil].fileSize;

    NSLog(@"-------%llu", size);

    // 打印出所有文件的路径

    NSArray *subpaths = [mgr subpathsAtPath:dirpath];

    NSLog(@"%@",subpaths);

    for (NSString *subpath in subpaths) {

        // 所有路径

        NSString *fullSubpath = [dirpath stringByAppendingPathComponent:subpath];

        // 累加文件大小

        size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;

        

        //        NSDictionary *attrs = [mgr attributesOfItemAtPath:fullSubpath error:nil];

        //        size += [attrs[NSFileSize] unsignedIntegerValue];

    }

    NSLog(@"%llu",size);

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /* 获取时间 */

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    formatStr = @"YYHHDD"

    dateFormatter.dateFormat = formatStr;

    NSDate* nowDate=[NSDate date];

    /*Block 弱引用相关*/

    __weak CMAdManager* weakSelf=self;

    /*单例子*/

    + (instancetype)shareInstance{

        static Manager *sharedInstance;

        static dispatch_once_t prdictate;

        dispatch_once(&prdictate, ^{

            sharedInstance = [[Manager alloc] init];

        });  

        return sharedInstance;

    }

    /*消息通知*/

    [[CMAppDescription sharedInstance] addObserver:self forKeyPath:@"showAdSwitch" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];

     

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onApplicationWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];

     

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setLoginPage:) name:kSetLoginPageNotification object:nil];

  • 相关阅读:
    实验四 决策树算法及应用
    实验三 朴素贝叶斯算法及应用
    实验二 K-近邻算法及应用
    实验一 感知器及其应用
    实验3: 面向对象分析与设计
    实验2:结构化分析与设计
    实验1:软件开发文档与工具的安装与使用
    软件工程第三次作业——ATM管理系统
    软件工程第二次作业——举例分析流程图与活动图的区别与联系
    软件工程第一次作业——小学四则运算题目生成程序
  • 原文地址:https://www.cnblogs.com/1018475062qq/p/6256888.html
Copyright © 2011-2022 走看看