归档解档

来源:互联网 发布:淘宝客推广产品找不到 编辑:程序博客网 时间:2024/04/29 04:57



AppDelegate.m

//    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
   
//获取内容
//    BOOL result = [[userDefault objectForKey:@"isLogin"] boolValue];
//    if (result) {
//        NSLog(@"用户已经登录");
//    }
   
  
MyTabBarController* myTabBarVC = [[MyTabBarControlleralloc]init];
   
self.window.rootViewController= myTabBarVC;
    [myTabBarVC
release];


MyTabBarController.m


- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
   
self= [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];
   
if(self) {
       
// Custom initialization
        [
selfloadViewControllers];
    }
   
returnself;
}

- (
void)loadViewControllers
{
   
SandBoxViewController* sandBoxVC = [[SandBoxViewControlleralloc]init];
   
UINavigationController* nav1 = [[UINavigationControlleralloc]initWithRootViewController:sandBoxVC];
    sandBoxVC.
title= @"SandBox";
    [sandBoxVC
release];
   
   
WriteReadViewController* writeReadVC = [[WriteReadViewControlleralloc]init];
   
UINavigationController* nav2 = [[UINavigationControlleralloc]initWithRootViewController:writeReadVC];
    writeReadVC.
title= @"W & R";
    [writeReadVC
release];
   
   
PlistViewController* plistVC = [[PlistViewControlleralloc]init];
   
UINavigationController* nav3 = [[UINavigationControlleralloc]initWithRootViewController:plistVC];
    plistVC.
title= @"Plist";
    [plistVC
release];
   
   
AchiverViewController* achiverVC = [[AchiverViewControlleralloc]init];
   
UINavigationController* nav4 = [[UINavigationControlleralloc]initWithRootViewController:achiverVC];
    achiverVC.
title= @"Achiver";
    [achiverVC
release];
   
   
UnachiverViewController* unachiverVC = [[UnachiverViewControlleralloc]init];
   
UINavigationController* nav5 = [[UINavigationControlleralloc]initWithRootViewController:unachiverVC];
    unachiverVC.
title= @"UnAchiver";
    [unachiverVC
release];
   
   
   
self.viewControllers= @[nav1, nav2, nav3, nav4, nav5];
    [nav1
release];
    [nav2
release];
    [nav3
release];
    [nav4
release];
    [nav5
release];
}

- (
void)viewDidLoad
{
    [
superviewDidLoad];
   
// Do any additional setup after loading the view.
   
self.tabBar.translucent= NO;
   
}

SandBoxViewController.m

- (void)viewDidLoad
{
    [
superviewDidLoad];
   
// Do any additional setup after loading the view.
   
   
//获得沙盒路径
   
NSString* sandBoxPath = NSHomeDirectory();
   
NSLog(@"%@", sandBoxPath);
   
   
//获得temp文件夹路径
   
NSString* tempPath = NSTemporaryDirectory();
   
NSLog(@"tempPath-----%@", tempPath);
   
   
//重要___
   
//获得Documents文件夹路径
   
//要用NSDocumentDirectory
   
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
   
NSLog(@"documentsPath-----%@", documentsPath);
   
   
//重要___
   
NSString* cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)objectAtIndex:0];
   
NSLog(@"cachesPath-----%@", cachesPath);
   
   
//获得Library文件夹路径,使用NSLibraryDirectory
   
NSString* libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES)objectAtIndex:0];
   
NSLog(@"libraryPath------%@", libraryPath);

   
//获取应用程序中的文件路径
   
//应用程序包里面的文件是只读的,不能写入
   
NSString* resourcePath = [[NSBundlemainBundle]pathForResource:@"33"ofType:@"jpg"];
   
NSLog(@"resourcePath-----%@", resourcePath);
   
   
//原始路径
   
NSString* originPath = [documentsPathstringByAppendingPathComponent:@"test.txt"];
   
//目的路径
   
NSString* toPath = [tempPath stringByAppendingPathComponent:@"test.txt"];
   
NSFileManager* fileManager = [NSFileManagerdefaultManager];
//    NSError * error = nil;
   
//从一个位置移动到另一个位置
//    [fileManager moveItemAtPath:originPath toPath:toPath error:&error];
 
//   NSLog(@"error = %@", error);
   
   
NSString* testArrayFilePath = [documentsPathstringByAppendingPathComponent:@"test.txt"];
//    if ([fileManager fileExistsAtPath:testArrayFilePath]) {
//        NSLog(@"存在");
//        //删除文件
//        [fileManager removeItemAtPath:testArrayFilePath error:nil];
//    }
   
   
//拷贝文件
   
if([fileManager fileExistsAtPath:testArrayFilePath]) {
        [fileManager
copyItemAtPath:originPathtoPath:toPatherror:nil];

    }
   
   
   
NSString* filesPath = [documentsPathstringByAppendingPathComponent:@"Files"];
   
//创建文件夹Files
    [fileManager
createDirectoryAtPath:filesPathwithIntermediateDirectories:YESattributes:nilerror:nil];
   
}



WriteReadViewController.m

- (void)viewDidLoad
{
    [
superviewDidLoad];
   
// Do any additional setup after loading the view.
   
   
UITextField* textField = [[UITextFieldalloc]initWithFrame:CGRectMake(20, 80, 280, 30)];
    textField.
borderStyle= UITextBorderStyleRoundedRect;
    textField.
backgroundColor= [UIColoryellowColor];
    textField.
tag= 100;
    [
self.viewaddSubview:textField];
    [textField
release];
   
   
UIButton* writeButton = [UIButtonbuttonWithType:UIButtonTypeSystem];
    writeButton.
frame= CGRectMake(20, 150, 100, 30);
    [writeButton
addTarget:selfaction:@selector(dowrite:)forControlEvents:UIControlEventTouchUpInside];
    [writeButton
setTitle:@"写入"forState:UIControlStateNormal];
    writeButton.
backgroundColor= [UIColorcyanColor];
    [
self.viewaddSubview:writeButton];
   
   
UIButton* readButton = [UIButtonbuttonWithType:UIButtonTypeSystem];
    readButton.
frame= CGRectMake(180, 150, 100, 30);
    [readButton
addTarget:selfaction:@selector(doread:)forControlEvents:UIControlEventTouchUpInside];
    [readButton
setTitle:@"读取"forState:UIControlStateNormal];
    readButton.
backgroundColor= [UIColorcyanColor];
    [
self.viewaddSubview:readButton];
   
   
   
}
//IOS中基本的文件读写
//IOS中支持NSString,NSArray,NSDictionary,NSData 4种数据类型,可以直接写入到磁盘,对于数组和字典,要想写入磁盘,里面存储的数据也只能是这4种数据类型
//写入
- (
void)dowrite:(UIButton*)btn
{
   
//取出textfield中输入的信息
   
UITextField* tf = (UITextField*)[self.viewviewWithTag:100];
   
   
//字符串写入磁盘,第一个参数是文件路径,第二个参数YES是保证多线程写入下安全,第三个参数是编码格式,通常UTF8编码,第四个参数是如果写入失败,捕获信息
    [tf.
textwriteToFile:[selffilePath]atomically:YESencoding:NSUTF8StringEncodingerror:nil];
   
   
//存储数组
//    NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//    NSString * filePath = [documentsPath stringByAppendingPathComponent:@"testarray"];
//    NSArray * array = [NSArray arrayWithObjects:@"111", @"222", nil];
//    [array writeToFile:filePath atomically:YES];
   
}

//读取
- (
void)doread:(UIButton*)btn
{
   
UITextField* tf = (UITextField*)[self.viewviewWithTag:100];
   
//    NSString * string = [NSString stringWithContentsOfFile:[self filePath] encoding:NSUTF8StringEncoding error:nil];
//    tf.text = string;
   
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
   
NSString* filePath = [documentsPathstringByAppendingPathComponent:@"testarray"];
   
NSArray* array = [NSArrayarrayWithContentsOfFile:filePath];
    tf.
text= [array firstObject];
   
   
   
}

- (
NSString*)filePath
{
   
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
   
NSString* filePath = [documentsPathstringByAppendingPathComponent:@"test.txt"];
   
returnfilePath;

}



PlistViewController.m

- (void)viewDidLoad
{
    [
superviewDidLoad];
   
// Do any additional setup after loading the view.
   
   
NSString* filePath = [[NSBundlemainBundle]pathForResource:@"Data"ofType:@"plist"];
   
NSDictionary* dic = [NSDictionarydictionaryWithContentsOfFile:filePath];
   
NSLog(@"+++++++%@", dic);
   
   
//把内存中的字典或数组写入磁盘,保存成plist文件
   
//plist能够存储的数据类型有7
   
//NSString NSArray NSDictionary NSDate NSData NSNumber BOOL
   
NSDictionary* dic1 = @{@"name":@"zhanger",@"age": @18,@"isstudent": @YES};
   
//保存成plist文件
 
//   NSString * writePath = [[self documentsPath] stringByAppendingString:@"/TestDic.plist"];
   
NSString* writePath = [[selfdocumentsPath]stringByAppendingPathComponent:@"TestDic.plist"];
    [dic1
writeToFile:writePathatomically:YES];
  
   
//NSUserDefaults系统提供了一个快速持久化的类,用于保存一些用户的偏好设置信息
   
NSUserDefaults* userDefaults = [NSUserDefaultsstandardUserDefaults];
   
    [userDefaults 
setObject:[NSNumbernumberWithBool:YES]forKey:@"isLogin"];
   
//保存到磁盘,如果没有这行代码,数据只会在退入后台或彻底关闭时才写入磁盘
    [userDefaults
synchronize];
}

- (
NSString*)documentsPath
{
    
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
   
returndocumentsPath;
}


AchiverViewController.m

- (void)viewDidLoad
{
    [
superviewDidLoad];
   
// Do any additional setup after loading the view.
   
   
UITextField* nameTextField = [[UITextFieldalloc]initWithFrame:CGRectMake(20, 80, 280, 30)];
    nameTextField.
borderStyle= UITextBorderStyleRoundedRect;
    nameTextField.
placeholder= @"name";
    nameTextField.
tag= 100;
    [
self.viewaddSubview:nameTextField];
    [nameTextField
release];
   
   
UITextField* sexTextField = [[UITextFieldalloc]initWithFrame:CGRectMake(20, 130, 280, 30)];
    sexTextField.
borderStyle= UITextBorderStyleRoundedRect;
    sexTextField.
placeholder= @"sex";
    sexTextField.
tag= 101;
    [
self.viewaddSubview:sexTextField];
    [sexTextField
release];
   
   
UITextField* ageTextField = [[UITextFieldalloc]initWithFrame:CGRectMake(20, 180, 280, 30)];
    ageTextField.
borderStyle= UITextBorderStyleRoundedRect;
    ageTextField.
placeholder= @"age";
    ageTextField.
tag= 102;
    [
self.viewaddSubview:ageTextField];
    [ageTextField
release];
   
   
UIButton* achiverButton = [UIButtonbuttonWithType:UIButtonTypeSystem];
    achiverButton.
frame= CGRectMake(20, 230, 280, 30);
    [achiverButton
addTarget:selfaction:@selector(doAchiver:)forControlEvents:UIControlEventTouchUpInside];
    [achiverButton
setTitle:@"归档"forState:UIControlStateNormal];
    achiverButton.
backgroundColor= [UIColorcyanColor];
    [
self.viewaddSubview:achiverButton];
   
}

- (
void)doAchiver:(UIButton*)btn
{
   
UITextField* nameTf = (UITextField*)[self.viewviewWithTag:100];
    
UITextField* sexTf = (UITextField*)[self.viewviewWithTag:101];
    
UITextField* ageTf = (UITextField*)[self.viewviewWithTag:102];
   
   
Person* person = [[Personalloc]init];
    person.
name= nameTf.text;
    person.
sex= sexTf.text;
    person.
age= [ageTf.textintegerValue];
   
   
//创建一个容器data
   
NSMutableData* data = [NSMutableDatadata];
   
//创建一个归档器
   
NSKeyedArchiver* achiver = [[NSKeyedArchiveralloc]initForWritingWithMutableData:data];
    [achiver
encodeObject:personforKey:@"Person"];
    [achiver
finishEncoding];
   
   
NSString* personFilePath = [[selfdocumentsPath]stringByAppendingPathComponent:@"PersonDataFile"];
   
//data写入磁盘
    [data
writeToFile:personFilePathatomically:YES];
   
}

- (
NSString*)documentsPath
{
   
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
   
returndocumentsPath;
}


UnachiverViewController.m

- (void)viewDidLoad
{
    [
superviewDidLoad];
   
// Do any additional setup after loading the view.
   
   
UILabel* aLabel = [[UILabelalloc]initWithFrame:CGRectMake(20, 80, 280, 100)];
    aLabel.
backgroundColor= [UIColorcyanColor];
    aLabel.
numberOfLines= 0;
    aLabel.
tag= 100;
    [
self.viewaddSubview:aLabel];
    [aLabel
release];
       
   
UIButton* unAchiverButton = [UIButtonbuttonWithType:UIButtonTypeSystem];
    unAchiverButton.
frame= CGRectMake(20, 230, 280, 30);
    [unAchiverButton
addTarget:selfaction:@selector(doUnAchiver:)forControlEvents:UIControlEventTouchUpInside];
    [unAchiverButton
setTitle:@"解档"forState:UIControlStateNormal];
    unAchiverButton.
backgroundColor= [UIColorredColor];
    [
self.viewaddSubview:unAchiverButton];

   
}

- (
void)doUnAchiver:(UIButton*)btn
{
   
NSString* personFilePath = [[selfdocumentsPath]stringByAppendingPathComponent:@"PersonDataFile"];
   
   
//从文件中读出personData
   
NSData* personData = [NSDatadataWithContentsOfFile:personFilePath];
   
   
//创建一个解档器
   
NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiveralloc]initForReadingWithData:personData];
   
//data中解码出一个person对象
   
Person* p = [unarchiver decodeObjectForKey:@"Person"];
   
UILabel* label = (UILabel*)[self.viewviewWithTag:100];
    label.
text= [NSStringstringWithFormat:@"name = %@, sex = %@, age = %ld", p.name, p.sex, (long)p.age];
   

}

- (
NSString*)documentsPath
{
   
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
   
returndocumentsPath;
}


Person.h

#import<Foundation/Foundation.h>

//1.要归档的类必须要接受NSCoding协议
//2.类中的实例变量的类型也必须是接受NSCoding协议

@interfacePerson : NSObject<NSCoding]] >

@property(nonatomic,retain)NSString* name;

@property(nonatomic,retain)NSString* sex;

@property(nonatomic,assign)NSIntegerage;

@end

Person.m

#import"Person.h"
#define NAME_KEY @
"name"
#define SEX_KEY @
"sex"
#define AGE_KEY @
"age"

@implementationPerson

//编码的过程其实就是对所有的实例变量转成二进制数据
- (
void)encodeWithCoder:(NSCoder*)aCoder
{
    [aCoder
encodeObject:self.nameforKey:NAME_KEY];
    [aCoder
encodeObject:self.sexforKey:SEX_KEY];
    [aCoder
encodeInteger:self.ageforKey:AGE_KEY];
}

//解码的过程就是把data中的二进制信息转成想要的数据,并赋值给实例变量
- (
id)initWithCoder:(NSCoder*)aDecoder
{
   
self= [superinit];
   
if(self) {
       
       
self.name= [aDecoder decodeObjectForKey:NAME_KEY];
       
self.sex= [aDecoder decodeObjectForKey:SEX_KEY];
       
self.age= [aDecoder decodeIntegerForKey:AGE_KEY];
       
    }
   
returnself;
}


- (
void)dealloc
{
   
self.name= nil;
   
self.sex= nil;
    [
superdealloc];
}
0 0
原创粉丝点击