零散的记录

  • Post author:
  • Post category:其他


  • 隐藏导航栏

self.navigationController.navigationBarHidden = YES;//最好写在声明该VC的时候

  • 获取本地语言

+ (NSString *)getLocalLanguage
{
    NSString *language = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0];
    return language;
}


  • 网络请求多线程执行模型
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        //子线程中开始网络请求数据
        
        //更新数据模型
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            
            //在主线程中更新UI代码
        });
    });


  • 文件管理常用操作



首先创建单列NSFileManager:

NSFileManager *fileManager = [NSFileManager defaultManager];



然后是常见操作:

读取:

NSData *readData = [fileManager contentsAtPath:@"/Users/Lorwy/Desktop/ImageTest.txt”];

写入:

BOOL saveImage = [fileManager createFileAtPath:@"/Users/Lorwy/Desktop/ImageTest.txt" contents:imageData attributes:[NSDictionary dictionary]];	

复制:

BOOL copyResult = [fileManager copyItemAtPath:@"/Users/Lorwy/Desktop/ImageTest.txt" toPath:@"/Users/Lorwy/Desktop/ImageTest1.txt" error:nil];	

移动:

BOOL moveResult = [fileManager moveItemAtPath:@"/Users/Lorwy/Desktop/ImageTest.txt" toPath:@"/Users/Lorwy/Desktop/Lorwy/ImageTest2.txt" error:nil];

删除:

BOOL removeResult = [fileManager removeItemAtPath:@"/Users/Lorwy/Desktop/Lorwy/ImageTest2.txt" error:nil];

比较:

BOOL comperaResult = [fileManager contentsEqualAtPath:@"/Users/<span style="font-family: Arial, Helvetica, sans-serif;">Lorwy</span><span style="font-family: Arial, Helvetica, sans-serif;">/Desktop/ImageTest2.txt" andPath:@"/Users/Lorwy/Desktop/ImageTest1.txt”];</span>

判断文件是否存在:

[fileManager fileExistsAtPath:@"/Users/Lorwy/Desktop/ImageTest.txt”];


  • 打开状态栏的网络进度指示器

[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];


  • 读取工程目录下的文件

//1正常
NSString *strPath = [[NSBundle mainBundle]pathForAuxiliaryExecutable:@"linkMan.txt"];
    NSString *linkManStr = [NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil];
    
//2正常
    NSString *linkManStr1 = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"level2" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
    
//3出现编码问题
    NSString * st = [[NSString alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"level4" ofType:@"txt"] encoding:nil error:nil];

//4过时
    NSString *linkManStr2 = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"level4" ofType:@"txt"]];







版权声明:本文为Lorwy原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。