您的位置:首页 > 移动开发 > IOS开发

归档解档

2015-11-13 22:27 363 查看
AppDelegate.m

//    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];

    //获取内容
//    BOOL result = [[userDefault objectForKey:@"isLogin"] boolValue];
//    if (result) {
//        NSLog(@"用户已经登录");
//    }

   

   MyTabBarController
* myTabBarVC = [[MyTabBarController
alloc]
init];

    self.window.rootViewController
= myTabBarVC;

    [myTabBarVC
release];

MyTabBarController.m

- (id)initWithNibName:(NSString
*)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil

{

    self
= [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];

    if
(self) {

        // Custom initialization

        [self
loadViewControllers];

    }

    return
self;

}

- (void)loadViewControllers

{

    SandBoxViewController
* sandBoxVC = [[SandBoxViewController
alloc]
init];

    UINavigationController
* nav1 = [[UINavigationController
alloc]
initWithRootViewController:sandBoxVC];

    sandBoxVC.title
=
@"SandBox";

    [sandBoxVC
release];

   

    WriteReadViewController
* writeReadVC = [[WriteReadViewController
alloc]
init];

    UINavigationController
* nav2 = [[UINavigationController
alloc]
initWithRootViewController:writeReadVC];

    writeReadVC.title
=
@"W & R";

    [writeReadVC
release];

   

    PlistViewController
* plistVC = [[PlistViewController
alloc]
init];

    UINavigationController
* nav3 = [[UINavigationController
alloc]
initWithRootViewController:plistVC];

    plistVC.title
=
@"Plist";

    [plistVC
release];

   

    AchiverViewController
* achiverVC = [[AchiverViewController
alloc]
init];

    UINavigationController
* nav4 = [[UINavigationController
alloc]
initWithRootViewController:achiverVC];

    achiverVC.title
=
@"Achiver";

    [achiverVC
release];

   

    UnachiverViewController
* unachiverVC = [[UnachiverViewController
alloc]
init];

    UINavigationController
* nav5 = [[UINavigationController
alloc]
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

{

    [super
viewDidLoad];

    // Do any additional setup after loading the view.

    self.tabBar.translucent
=
NO;

   

}

SandBoxViewController.m

- (void)viewDidLoad

{

    [super
viewDidLoad];

    // 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 = [[NSBundle
mainBundle]
pathForResource:@"33"
ofType:@"jpg"];

    NSLog(@"resourcePath-----%@",
resourcePath);

   

    //原始路径

    NSString
* originPath = [documentsPath
stringByAppendingPathComponent:@"test.txt"];

    //目的路径

    NSString
* toPath = [tempPath
stringByAppendingPathComponent:@"test.txt"];

    NSFileManager
* fileManager = [NSFileManager
defaultManager];
//    NSError * error = nil;

    //从一个位置移动到另一个位置
//    [fileManager moveItemAtPath:originPath toPath:toPath error:&error];

 //   NSLog(@"error = %@", error);

   

    NSString
* testArrayFilePath = [documentsPath
stringByAppendingPathComponent:@"test.txt"];
//    if ([fileManager fileExistsAtPath:testArrayFilePath]) {
//        NSLog(@"存在");
//        //删除文件
//        [fileManager removeItemAtPath:testArrayFilePath error:nil];
//    }

   

    //拷贝文件

    if
([fileManager
fileExistsAtPath:testArrayFilePath]) {

        [fileManager
copyItemAtPath:originPath
toPath:toPath
error:nil];

    }

   

   

    NSString
* filesPath = [documentsPath
stringByAppendingPathComponent:@"Files"];

    //创建文件夹Files

    [fileManager
createDirectoryAtPath:filesPath
withIntermediateDirectories:YES
attributes:nil
error:nil];

   

}

WriteReadViewController.m

- (void)viewDidLoad

{

    [super
viewDidLoad];

    // Do any additional setup after loading the view.

   

    UITextField
* textField = [[UITextField
alloc]
initWithFrame:CGRectMake(20,
80, 280, 30)];

    textField.borderStyle
=
UITextBorderStyleRoundedRect;

    textField.backgroundColor
= [UIColor
yellowColor];

    textField.tag
= 100;

    [self.view
addSubview:textField];

    [textField
release];

   

    UIButton
* writeButton = [UIButton
buttonWithType:UIButtonTypeSystem];

    writeButton.frame
=
CGRectMake(20, 150, 100, 30);

    [writeButton
addTarget:self
action:@selector(dowrite:)
forControlEvents:UIControlEventTouchUpInside];

    [writeButton
setTitle:@"写入"
forState:UIControlStateNormal];

    writeButton.backgroundColor
= [UIColor
cyanColor];

    [self.view
addSubview:writeButton];

   

    UIButton
* readButton = [UIButton
buttonWithType:UIButtonTypeSystem];

    readButton.frame
=
CGRectMake(180, 150, 100, 30);

    [readButton
addTarget:self
action:@selector(doread:)
forControlEvents:UIControlEventTouchUpInside];

    [readButton
setTitle:@"读取"
forState:UIControlStateNormal];

    readButton.backgroundColor
= [UIColor
cyanColor];

    [self.view
addSubview:readButton];

   

   

   

}
//IOS中基本的文件读写
//IOS中支持NSString,NSArray,NSDictionary,NSData
4种数据类型,可以直接写入到磁盘,对于数组和字典,要想写入磁盘,里面存储的数据也只能是这4种数据类型
//写入

- (void)dowrite:(UIButton
*)btn

{

    //取出textfield中输入的信息

    UITextField
* tf = (UITextField
*)[self.view
viewWithTag:100];

   

    //字符串写入磁盘,第一个参数是文件路径,第二个参数YES是保证多线程写入下安全,第三个参数是编码格式,通常UTF8编码,第四个参数是如果写入失败,捕获信息

    [tf.text
writeToFile:[self
filePath]
atomically:YES
encoding:NSUTF8StringEncoding
error: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.view
viewWithTag:100];

   
//    NSString * string = [NSString stringWithContentsOfFile:[self filePath] encoding:NSUTF8StringEncoding error:nil];
//    tf.text = string;

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

    NSString
* filePath = [documentsPath
stringByAppendingPathComponent:@"testarray"];

    NSArray
* array = [NSArray
arrayWithContentsOfFile:filePath];

    tf.text
= [array
firstObject];

   

   

   

}

- (NSString
*)filePath

{

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

    NSString
* filePath = [documentsPath
stringByAppendingPathComponent:@"test.txt"];

    return
filePath;

}

PlistViewController.m

- (void)viewDidLoad

{

    [super
viewDidLoad];

    // Do any additional setup after loading the view.

   

    NSString
* filePath = [[NSBundle
mainBundle]
pathForResource:@"Data"
ofType:@"plist"];

    NSDictionary
* dic = [NSDictionary
dictionaryWithContentsOfFile: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 = [[self
documentsPath]
stringByAppendingPathComponent:@"TestDic.plist"];

    [dic1 writeToFile:writePath
atomically:YES];

  

    //NSUserDefaults
系统提供了一个快速持久化的类,用于保存一些用户的偏好设置信息

    NSUserDefaults
* userDefaults = [NSUserDefaults
standardUserDefaults];

   

    [userDefaults 
setObject:[NSNumber
numberWithBool:YES]
forKey:@"isLogin"];

    //保存到磁盘,如果没有这行代码,数据只会在退入后台或彻底关闭时才写入磁盘

    [userDefaults
synchronize];

}

- (NSString
*)documentsPath

{

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

    return
documentsPath;

}

AchiverViewController.m

- (void)viewDidLoad

{

    [super
viewDidLoad];

    // Do any additional setup after loading the view.

   

    UITextField
* nameTextField = [[UITextField
alloc]
initWithFrame:CGRectMake(20,
80, 280, 30)];

    nameTextField.borderStyle
=
UITextBorderStyleRoundedRect;

    nameTextField.placeholder
=
@"name";

    nameTextField.tag
= 100;

    [self.view
addSubview:nameTextField];

    [nameTextField
release];

   

    UITextField
* sexTextField = [[UITextField
alloc]
initWithFrame:CGRectMake(20,
130, 280, 30)];

    sexTextField.borderStyle
=
UITextBorderStyleRoundedRect;

    sexTextField.placeholder
=
@"sex";

    sexTextField.tag
= 101;

    [self.view
addSubview:sexTextField];

    [sexTextField
release];

   

    UITextField
* ageTextField = [[UITextField
alloc]
initWithFrame:CGRectMake(20,
180, 280, 30)];

    ageTextField.borderStyle
=
UITextBorderStyleRoundedRect;

    ageTextField.placeholder
=
@"age";

    ageTextField.tag
= 102;

    [self.view
addSubview:ageTextField];

    [ageTextField
release];

   

    UIButton
* achiverButton = [UIButton
buttonWithType:UIButtonTypeSystem];

    achiverButton.frame
=
CGRectMake(20, 230, 280, 30);

    [achiverButton
addTarget:self
action:@selector(doAchiver:)
forControlEvents:UIControlEventTouchUpInside];

    [achiverButton
setTitle:@"归档"
forState:UIControlStateNormal];

    achiverButton.backgroundColor
= [UIColor
cyanColor];

    [self.view
addSubview:achiverButton];

   

}

- (void)doAchiver:(UIButton
*)btn

{

    UITextField
* nameTf = (UITextField
*)[self.view
viewWithTag:100];

     UITextField
* sexTf = (UITextField
*)[self.view
viewWithTag:101];

     UITextField
* ageTf = (UITextField
*)[self.view
viewWithTag:102];

   

    Person
* person = [[Person
alloc]
init];

    person.name
= nameTf.text;

    person.sex
= sexTf.text;

    person.age
= [ageTf.text
integerValue];

   

    //创建一个容器data

    NSMutableData
* data = [NSMutableData
data];

    //创建一个归档器

    NSKeyedArchiver
* achiver = [[NSKeyedArchiver
alloc]
initForWritingWithMutableData:data];

    [achiver
encodeObject:person
forKey:@"Person"];

    [achiver
finishEncoding];

   

    NSString
* personFilePath = [[self
documentsPath]
stringByAppendingPathComponent:@"PersonDataFile"];

    //把data写入磁盘

    [data writeToFile:personFilePath
atomically:YES];

   

}

- (NSString
*)documentsPath

{

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

    return
documentsPath;

}

UnachiverViewController.m

- (void)viewDidLoad

{

    [super
viewDidLoad];

    // Do any additional setup after loading the view.

   

    UILabel
* aLabel = [[UILabel
alloc]
initWithFrame:CGRectMake(20,
80, 280, 100)];

    aLabel.backgroundColor
= [UIColor
cyanColor];

    aLabel.numberOfLines
= 0;

    aLabel.tag
= 100;

    [self.view
addSubview:aLabel];

    [aLabel
release];

       

    UIButton
* unAchiverButton = [UIButton
buttonWithType:UIButtonTypeSystem];

    unAchiverButton.frame
=
CGRectMake(20, 230, 280, 30);

    [unAchiverButton
addTarget:self
action:@selector(doUnAchiver:)
forControlEvents:UIControlEventTouchUpInside];

    [unAchiverButton
setTitle:@"解档"
forState:UIControlStateNormal];

    unAchiverButton.backgroundColor
= [UIColor
redColor];

    [self.view
addSubview:unAchiverButton];

   

}

- (void)doUnAchiver:(UIButton
*)btn

{

    NSString
* personFilePath = [[self
documentsPath]
stringByAppendingPathComponent:@"PersonDataFile"];

   

    //从文件中读出personData

    NSData
* personData = [NSData
dataWithContentsOfFile:personFilePath];

   

    //创建一个解档器

    NSKeyedUnarchiver
* unarchiver = [[NSKeyedUnarchiver
alloc]
initForReadingWithData:personData];

    //从data中解码出一个person对象

    Person
* p = [unarchiver
decodeObjectForKey:@"Person"];

    UILabel
* label = (UILabel
*)[self.view
viewWithTag:100];

    label.text
= [NSString
stringWithFormat:@"name = %@, sex = %@, age = %ld",
p.name, p.sex,
(long)p.age];

   

}

- (NSString
*)documentsPath

{

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

    return
documentsPath;

}

Person.h

#import
<Foundation/Foundation.h>

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

@interface
Person :
NSObject<NSCoding]] >

@property
(nonatomic,
retain)NSString
* name;

@property
(nonatomic,
retain)NSString
* sex;

@property
(nonatomic,
assign)NSInteger
age;

@end

Person.m

#import
"Person.h"

#define NAME_KEY @"name"

#define SEX_KEY @"sex"

#define AGE_KEY @"age"

@implementation
Person

//编码的过程其实就是对所有的实例变量转成二进制数据

- (void)encodeWithCoder:(NSCoder
*)aCoder

{

    [aCoder
encodeObject:self.name
forKey:NAME_KEY];

    [aCoder
encodeObject:self.sex
forKey:SEX_KEY];

    [aCoder
encodeInteger:self.age
forKey:AGE_KEY];

}

//解码的过程就是把data中的二进制信息转成想要的数据,并赋值给实例变量

- (id)initWithCoder:(NSCoder
*)aDecoder

{

    self
= [super
init];

    if
(self) {

       

        self.name
= [aDecoder
decodeObjectForKey:NAME_KEY];

        self.sex
= [aDecoder
decodeObjectForKey:SEX_KEY];

        self.age
= [aDecoder
decodeIntegerForKey:AGE_KEY];

       

    }

    return
self;

}

- (void)dealloc

{

    self.name
=
nil;

    self.sex
=
nil;

    [super
dealloc];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息