美文网首页算法或者代码
一些也许会用到的oc知识

一些也许会用到的oc知识

作者: iOS小乔 | 来源:发表于2016-06-01 09:56 被阅读85次

    一、oc如何将数据写入到plist文件中

    /**
    *数组、字典只能将BoolNSNumberNSStringNSDataNSDateNSArrayNSDictionary 写入属性列表plist文件
    */

        NSValue *value = [NSValue valueWithRange:NSMakeRange(1, 5)];
        NSDictionary *dic = @{@"key1":@12345,@"key2":@"tttxxx",@"key3":value};
        NSString *homePath = NSHomeDirectory();
        NSString *path = [homePath stringByAppendingPathComponent:@"t.plist"];
        BOOL success = [dic writeToFile:path atomically:YES];
        if (success) {
            NSLog(@"write success");
        }
    

    二、拆分路径

    NSString *path = @"/Users/apple/testfile.text";
    NSArray *pathComps = [path pathComponents];
    NSLog(@"%@",pathComps);
    
    屏幕快照 2016-05-26 下午2.12.51.png

    三、计算一个目录的容量大小

    NSString *directPath = [homePath stringByAppendingPathComponent:@"test"];
    NSDirectoryEnumerator *files = [fileManager enumeratorAtPath:directPath];
    
    NSString *path = [files nextObject];
    NSInteger fileNum = 0;
    while (path != nil) {
        NSLog(@"%@",path);
        NSString *path2 = [directPath stringByAppendingPathComponent:path];
        NSDictionary *attrDic = [fileManager attributesOfItemAtPath:path2 error:nil];
        NSNumber *fileSize = attrDic[NSFileSize];
        fileNum += [fileSize intValue];
        
        path = [files nextObject];
    }
    NSLog(@"目录的总大小:%d",fileNum);
    

    四、获取对应的时区时间

        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate *date = [NSDate date];
        NSTimeZone *zone = [NSTimeZone systemTimeZone];
        NSInteger interval = [zone secondsFromGMTForDate: date];
        NSDate *localeDate = [NSDate dateWithTimeInterval:interval sinceDate:date];
        NSString * s =  [dateFormatter stringFromDate:localeDate];
    

    五、取消延迟执行的函数

    延迟执行函数

    [self performSelector:@selector(scrollDone) withObject:nil afterDelay:0.5];
    

    在0.5秒内取消执行函数,带的参数必须一样,才能取消成功

    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(scrollDone) object:nil];

    相关文章

      网友评论

        本文标题:一些也许会用到的oc知识

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