ios获取新数据要不要关_iOS开发之数据读写
iOS進(jìn)階
1:數(shù)據(jù)處理之?dāng)?shù)據(jù)讀寫
1):獲取當(dāng)前應(yīng)用程序的沙盒根目錄
NSString*rootPath = NSHomeDirectory();
NSLog(@"%@",rootPath);
rootPath就是根目錄
然后將打印出來(lái)的文件目錄右鍵單擊選擇services下的Reveal In Finder
2):
Documents:存儲(chǔ)持久化文件數(shù)據(jù)
Library/Caches:存儲(chǔ)緩存數(shù)據(jù)
Library/Preferences:存儲(chǔ)應(yīng)用的所有偏好設(shè)置
tmp:保存應(yīng)用運(yùn)行時(shí)所需的臨時(shí)數(shù)據(jù)
3):定位當(dāng)前應(yīng)用程序的沙盒根目錄
//1.Documents
NSString*docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"%@",docPath);
//2.library
NSString*libPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"libPath is %@",libPath);
//3.library/Caches
NSString*cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"cachesPath is %@",cachesPath);
//4.library/preferences ? 只能用拼接方法定位
NSString*prePath = [libPath stringByAppendingPathComponent:@"Preferences"];
NSLog(@"prePath is %@",prePath);
//5.tmp
NSString*tmpPath = NSTemporaryDirectory();
NSLog(@"tmpPath is %@",tmpPath);
4):存儲(chǔ)應(yīng)用程序的偏好設(shè)置的類:NSUserDefaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults] ;
[defaults setObject:@"tang" forKey:@"name"] ;
//同步
[defaults synchronize] ;
NSLog(@"%@",[defaults objectForKey:@"name"]);
5):簡(jiǎn)單對(duì)象的讀寫操作
只有四種簡(jiǎn)單的數(shù)據(jù)類型才能直接寫入進(jìn)文件
NSString NSDictionary NSData NSArray
第一步:獲取沙盒下文件夾Documents的路徑
NSString*docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
第二步:創(chuàng)建需要寫入的文件路徑
NSString*filePath = [docPath stringByAppendingString:@"/text.txt"];
第三步:創(chuàng)建字符串對(duì)象
NSString*string = @"is副科級(jí)領(lǐng)導(dǎo)蓋房了";
第四步:寫入,四種簡(jiǎn)單的數(shù)據(jù)類型的寫入方法差不多的
[string writeToFile:filePath atomically:YESencoding:NSUTF8StringEncodingerror:nil];
第五步:讀取字符串對(duì)象
NSString*resultStr = [NSStringstringWithContentsOfFile:filePath encoding:NSUTF8StringEncodingerror:nil];
數(shù)組和字典可參照字符串對(duì)象的寫入讀取方法
*NSData對(duì)象的寫入和讀取(將圖片存儲(chǔ))
第一步和第二步是一樣的
第三步:UIImage*image = [UIImageimageNamed:@"0"];
第四步:NSData*data = UIImagePNGRepresentation(image);
第五步:寫入
[data writeToFile:dataPath atomically:YES];
讀取圖片并在模擬器上顯示:
NSData*resultData = [NSDatadataWithContentsOfFile:dataPath];
UIImage*reImage = [UIImageimageWithData:resultData];
UIImageView*imageView = [[UIImageViewalloc] initWithImage:reImage];
imageView.frame= self.view.bounds;
[self.viewaddSubview:imageView];
6):在沙盒目錄下創(chuàng)建文件
第一步:找到沙盒路徑(caches)
//1.獲取Documents目錄
NSString*docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//2.創(chuàng)建文件路徑
NSString*filePath = [docPath stringByAppendingString:@"/baobao.txt"];
//3.創(chuàng)建文件管理對(duì)象
NSFileManager*manager = [NSFileManagerdefaultManager];
//4.創(chuàng)建
[manager createFileAtPath:filePath contents:[@"sdfasdfs"dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
NSLog(@"%@",filePath);
//計(jì)算文件或文件夾的大小
NSDictionary*dic = [manager attributesOfItemAtPath:filePath error:nil];
NSLog(@"%@",dic);
NSNumber*number = [dic objectForKey:NSFileSize];
NSLog(@"%@",number);
7):創(chuàng)建文件夾和創(chuàng)建文件的核心代碼差別在:
a:文件夾路徑?jīng)]有后綴名,文件有
b:文件管理器創(chuàng)建方法的不同
//2.創(chuàng)建文件夾的路徑
NSString*filePath = [cachesPath stringByAppendingString:@"/text"];
//3.創(chuàng)建文件管理器對(duì)象
NSFileManager*manager = [NSFileManagerdefaultManager];
[manager createDirectoryAtPath:filePath withIntermediateDirectories:YESattributes:nilerror:nil];
/*
在Documents文件夾下,創(chuàng)建一個(gè)文件夾(path),在該文件夾下創(chuàng)建一個(gè)文件(test.txt),將一個(gè)圖片對(duì)象存入到該文件中,然后在Caches文件夾下創(chuàng)建一個(gè)文件夾名為"testDirectroy",將test.txt文件復(fù)制到這個(gè)文件夾下.
*/
- (void)moveFile {
//1.獲取Documents目錄
NSString*docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//2.創(chuàng)建文件夾路徑
NSString*filePath = [docPath stringByAppendingString:@"/path"];
NSFileManager*manager = [NSFileManagerdefaultManager];
[manager createDirectoryAtPath:filePath withIntermediateDirectories:YESattributes:nilerror:nil];
//3.創(chuàng)建文件路徑
NSString*testPath = [filePath stringByAppendingString:@"/test.txt"];
[manager createFileAtPath:testPath contents:[@"aa"dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
//4.寫入圖片
UIImage*image = [UIImageimageNamed:@"0"];
NSData*data = UIImagePNGRepresentation(image);
[data writeToFile:testPath atomically:YES];
//5.caches目錄
NSString*cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//6.創(chuàng)建文件夾testDirectroy
NSString*directoryPath = [cachesPath stringByAppendingPathComponent:@"testDirectroy"];
[manager createDirectoryAtPath:directoryPath withIntermediateDirectories:YESattributes:nilerror:nil];
//7創(chuàng)建一個(gè)和documents中需要復(fù)制的文件同名的文件
NSString*desPath = [directoryPath stringByAppendingString:@"/test.txt"];
//8.復(fù)制時(shí)需要?jiǎng)?chuàng)建文件管理器
[manager copyItemAtPath:testPath toPath:desPath error:nil];
NSLog(@"%@",directoryPath);
}
/*
練習(xí)要求:
在Documents目錄下創(chuàng)建一個(gè)文件text.txt
從文件的偏移量為3的時(shí)候開(kāi)始追加內(nèi)容1234
*/
- (void)addContent {
//1.得到Documents目錄
NSString*docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//2.創(chuàng)建文件夾路徑
NSString*filePath = [docPath stringByAppendingPathComponent:@"fiel"];
NSFileManager*manager = [NSFileManagerdefaultManager];
[manager createDirectoryAtPath:filePath withIntermediateDirectories:YESattributes:nilerror:nil];
//3.創(chuàng)建文件路徑
NSString*textPath = [filePath stringByAppendingPathComponent:@"/text.txt"];
[manager createFileAtPath:textPath contents:[@"hahahahahaha"dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
//4.創(chuàng)建文件對(duì)接器
NSFileHandle*handle = [NSFileHandlefileHandleForUpdatingAtPath:textPath];
//此時(shí)文件在更新?tīng)顟B(tài)下,即可讀也可寫
[handle seekToFileOffset:3];
//5.開(kāi)始在偏移量為3的地方寫入字符串
[handle writeData:[@"1234"dataUsingEncoding:NSUTF8StringEncoding]];
[handle closeFile];
NSLog(@"%@",textPath);
}
//核心代碼如下
//創(chuàng)建文件對(duì)接對(duì)象
NSFileHandle*handle = [NSFileHandlefileHandleForUpdatingAtPath:filePath];
//此時(shí)的文件對(duì)接對(duì)象既可以讀也可以寫
//將偏移量移動(dòng)到3的位置
[handle seekToFileOffset:3];
//寫入數(shù)據(jù)
[handle writeData:[@"1"dataUsingEncoding:NSUTF8StringEncoding]];
//執(zhí)行完操作之后不要忘了關(guān)閉文件
[handle closeFile];
8):復(fù)雜文件的歸檔和反歸檔(持久化操作)
//歸檔
- (void)archiver {
//1.創(chuàng)建person對(duì)象
Person*p1 = [[Personalloc] initWithName:@"tangxi"age:@"18"];
Person*p2 = [[Personalloc] initWithName:@"aren"age:@"20"];
//2.獲取Documents目錄
NSString*docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString*filePath = [docPath stringByAppendingString:@"/haha.txt"];
//3.創(chuàng)建可變數(shù)據(jù)對(duì)象
NSMutableData*data = [NSMutableDatadata];
//4.創(chuàng)建歸檔類的對(duì)象
NSKeyedArchiver*archiver = [[NSKeyedArchiveralloc] initForWritingWithMutableData:data];
//5.將person對(duì)象歸檔
[archiver encodeObject:p1 forKey:@"person1"];
[archiver encodeObject:p2 forKey:@"person2"];
//6.結(jié)束歸檔
[archiver finishEncoding];//此時(shí)不管有幾個(gè)對(duì)象沒(méi)有被歸檔都會(huì)停止歸檔了
//7.將可變數(shù)據(jù)data寫入創(chuàng)建的文件
[data writeToFile:filePath atomically:YES];
NSLog(@"%@",filePath);
}
//反歸檔
- (void)unarchiver {
//1.獲取Documents目錄
NSString*documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString*filePath = [documents stringByAppendingPathComponent:@"haha.txt"];
NSData*data = [NSDatadataWithContentsOfFile:filePath];
NSKeyedUnarchiver*unarchiver = [[NSKeyedUnarchiveralloc] initForReadingWithData:data];
Person*person = [unarchiver decodeObjectForKey:@"person1"];
NSLog(@"name is %@, age is %@",person.name,person.age);
[unarchiver finishDecoding];
}
//還要?jiǎng)?chuàng)建一個(gè)模型對(duì)象Person,遵循NSCoding協(xié)議
實(shí)現(xiàn)兩個(gè)協(xié)議方法:
- (instancetype)initWithName:(NSString*)name age:(NSString*)age {
self= [superinit];
if(self) {
self.name= name;
self.age= age;
}
returnself;
}
- (void)encodeWithCoder:(NSCoder*)aCoder {
[aCoder encodeObject:self.nameforKey:@"name"];
[aCoder encodeObject:self.ageforKey:@"age"];
}
- (nullableinstancetype)initWithCoder:(NSCoder*)aDecoder {
NSString*name = [aDecoder decodeObjectForKey:@"name"];
NSString*age = [aDecoder decodeObjectForKey:@"age"];
return[selfinitWithName:name age:age];
}
//4.寫入圖片
UIImage*image = [UIImageimageNamed:@"0"];
NSData*data = UIImagePNGRepresentation(image);
[data writeToFile:testPath atomically:YES];
//4.創(chuàng)建文件對(duì)接器
NSFileHandle*handle = [NSFileHandlefileHandleForUpdatingAtPath:textPath];
//此時(shí)文件在更新?tīng)顟B(tài)下,即可讀也可寫
[handle seekToFileOffset:3];
//5.開(kāi)始在偏移量為3的地方寫入字符串
[handle writeData:[@"1234"dataUsingEncoding:NSUTF8StringEncoding]];
[handle closeFile];
NSLog(@"%@",textPath);
總結(jié)
以上是生活随笔為你收集整理的ios获取新数据要不要关_iOS开发之数据读写的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: “幽独抵归山”下一句是什么
- 下一篇: swing 圆角按钮_JFrame实现圆