最近小结,供自己和大家学习:
一. [superdealloc] ; 一定要放到最后,如果放在其他dealloc之前可能造成程序不稳定,莫名其妙的崩溃。这个问题困扰小w N久,程序检查了n遍就是哪里都没问题 单程序就是崩溃 最后请教高人才发现此问题
二.创建文件夹,文件 读写文件
//获取document目录
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [pathsobjectAtIndex:0];
//创建文件夹路径
NSString *folderPath = [documentsDirectorystringByAppendingPathComponent:@"text"];
//创建文件夹管理器
NSFileManager *fileManager = [NSFileManagerdefaultManager];
//判大text文件是否存在
BOOL folderExists = [fileManagerfileExistsAtPath:folderPath];
//如果文件不存在则创建文件夹
if (!folderExists) {
[fileManager createDirectoryAtPath:folderPathwithIntermediateDirectories:YESattributes:nilerror:nil];
}
NSString *filePath = [folderPathstringByAppendingPathComponent:@"text.txt"];
//判断文件是否存在
BOOL fileExists = [fileManagerfileExistsAtPath:filePath];
//如果不存在则创建文件
if (!fileExists) {
[fileManager createFileAtPath:filePathcontents:nilattributes:nil];
}
//写文件
NSString *str = @“sssssssss”;
[str writeToFile:filePathatomically:YES];
//读文件 读文件之前应判断文件路径是否存在
NSString *strfile = [NSStringstringWithContentsOfFile:filePath];
三。NSDate,NSDateFormatter的使用
//默认日期格式为yyyy-MM-dd
NSDateFormatter *dateFormater;
dateFormater= [[NSDateFormatteralloc] init];
[dateFormater setLocale:[[NSLocalealloc]initWithLocaleIdentifier:@"zh_CN"]];//location设置为中国
[dateFormater setDateFormat:@"yyyy-MM-dd EEEE hh:mm:ss"];
//picker的默认时间为当前时间
date=[NSDatedate];
//显示当前时间
self.timeLabel.text = [NSStringstringWithString:[dateFormater stringFromDate:date]];
[dateFormater release];