OC学习笔记21--OC中集合类的读写文件操作及NSData与NSValue
来源:互联网 发布:淘宝蓝海产品 编辑:程序博客网 时间:2024/04/28 02:05
#import <Foundation/Foundation.h> // 定义一个Scores结构体typedef struct {double mathScore,engScore,cScore;} Scores;NSString * NSStringFromScores(Scores s);int main(int argc, const char * argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSFileManager *fm=[NSFileManager defaultManager]; NSString * aryPath= @"C:/Users/Administrator/Desktop/ary.xml"; NSString * dictPath= @"C:/Users/Administrator/Desktop/dict.xml"; // *1> OC数组的读写文件操作 NSArray *sary= [NSArray arrayWithObjects:@"Jack",@"Tommy",@"Bolt",nil]; BOOL aryFlag=[sary writeToFile:aryPath atomically:YES]; if(aryFlag) { sary=[NSArray arrayWithContentsOfFile:aryPath]; NSLog(@"\naryFromFile=\n%@",sary); } // *2> OC字典的读写文件操作 NSMutableDictionary * dict = [NSMutableDictionary dictionary]; [dict setValue:sary forKey:@"ary"]; [dict setValue:aryPath forKey:@"aryPath"]; BOOL dictFlag=[dict writeToFile:dictPath atomically:YES]; if(dictFlag) { dict=[NSMutableDictionary dictionaryWithContentsOfFile:dictPath]; NSLog(@"\ndictFromFile=\n%@",dict); } // *3> OCstr的序列化与反序列化 NSString * path= @"C:/Users/Administrator/Desktop/str.txt"; // 将OCstr按某种编码方式encode成bytes保存在NSData NSData *data=[@"this is an NSData test!!\n" dataUsingEncoding:NSUTF8StringEncoding]; // 将bytes写入文件 BOOL strPathFlag=[fm createFileAtPath:path contents:data attributes:nil]; if(strPathFlag) { // 从文件读取bytes保存为NSData类型NSData *strData=[fm contentsAtPath:path];// 将bytes按某种解码方式decode成OCstrNSString *strFromData=[[[NSString alloc] initWithData:strDataencoding:NSUTF8StringEncoding] autorelease];NSLog(@"\nstrFromData=%@",strFromData); } // *4> struct与NSValue的互相转换 Scores scores1={66.0,77.0,88.0},scores2={0},* scores3={0}; // 将struct包装成NSValue,@encode(Type)将struct类型转换成(char *)类型 NSValue *scoresVal=[NSValue valueWithBytes:&scores1 objCType:@encode(Scores)]; // 从NSValue中取出struct [scoresVal getValue:&scores2]; NSLog(@"\nscoresValFromStruct=%@\nscores2FromNSValue=%@",scoresVal,NSStringFromScores(scores2)); // *5> 将数据按照其在内存中的bytes写入文件,并将文件中的bytes读入内存 NSString * bytePath= @"C:/Users/Administrator/Desktop/byte.txt"; // 将struct按照其内存中的bytes保存到NSData对象 NSData *data2=[NSData dataWithBytes:&scores2 length:sizeof(Scores)]; // 将bytes写入文件 BOOL bytePathFlag=[fm createFileAtPath:bytePath contents:data2 attributes:nil]; if(bytePathFlag) { // 从文件读取bytes保存到NSData对象 NSData *data3=[fm contentsAtPath:bytePath]; // 取出NSData对象中的bytes,转换成struct scores3=(Scores *)[data2 bytes]; NSLog(@"\nscores3FromNSData=%@",NSStringFromScores(*scores3)); } [pool drain]; return 0;} // 将Scores结构体转字符串NSString * NSStringFromScores(Scores s){return [NSString stringWithFormat:@"{ mathScore=%.2f, engScore=%.2f, cScore=%.2f }",s.mathScore,s.engScore,s.cScore];}
0 0
- OC学习笔记21--OC中集合类的读写文件操作及NSData与NSValue
- OC中集合与java中集合的比较(1)Array
- OC中集合与java中集合的比较(2)NSDictionary
- OC中集合的基本使用
- OC-文件的读写操作
- OC类 之 NSData NSDate NSSet NSNumber NSValue
- oc文件操作学习笔记
- [OC学习笔记]NSArray、NSMutableArray、NSValue方法
- OC学习笔记16--OC中文件操作-NSFileManager的使用及文件操作中常用的一些字符串方法
- OC学习:文件读写
- OC中集合类对象的枚举和数组的排序
- OC学习----NSDate&NSData
- NSValue和NSNumber(OC的包装类)
- python中集合的操作与应用
- OC学习--文件操作
- ——黑马程序员——OC中集合对象的内存管理及copy的用法
- OC-NSValue
- OC-NSValue
- BufferImage 与imagezh整理归纳
- Linux异步机制
- Android Support Annotations 使用详解
- Maven学习 (三) 使用m2eclipse创建web项目
- 从Android资源角度谈Android代码内存优化
- OC学习笔记21--OC中集合类的读写文件操作及NSData与NSValue
- vps建站及相关知识整理
- bzoj 2594
- atitit.js 与c# java交互html5化的原理与总结.doc
- beatbox最终版本
- 关于C++函数调用的那些事儿
- 学习matlab小结(1)-monte carlo & binomial method-2015.08.18
- HashMap的用法
- 黑马程序员_Java入门及基础语法上