自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

通過歸檔永久存儲數(shù)據(jù)

移動開發(fā) iOS
這次的小例子中,我們將會通過歸檔實(shí)現(xiàn)數(shù)據(jù)的保存。當(dāng)程序運(yùn)行時,先檢查歸檔文件是否存在,如果存在的話就從歸檔文件中讀取數(shù)據(jù)顯示在界面上;如果歸檔文件不存在,就使用默認(rèn)設(shè)置。當(dāng)程序關(guān)閉時,會將數(shù)據(jù)存儲在歸檔文件中,這樣下次運(yùn)行程序時就會顯示上次的設(shè)置了。

之前有文章簡單介紹了怎樣在Settings程序中設(shè)置自己的程序,并實(shí)現(xiàn)保存,使得下次運(yùn)行自己的程序時顯示的還是上次的設(shè)置 項(xiàng)。而文章沙盒SandBox的結(jié)構(gòu)介紹SandBox時,我們看到其實(shí)使用Settings程序設(shè)置后,數(shù)據(jù)是保存在一個plist文件的。

想要***保存數(shù)據(jù),我們當(dāng)然可以使用plist文件,當(dāng)退出程序時,我們執(zhí)行將數(shù)據(jù)寫入plist文件的操作,使用writeToFile:atomically:方法。

具有這個方法的類有:

  1. NSArray 
  2. NSMutableArray 
  3. NSDictionary 
  4. NSMutableDictionary 
  5. NSData 
  6. NSMutableData 
  7. NSString 
  8. NSMutableString 
  9. NSNumber 
  10. NSDate 

例如,我們的數(shù)據(jù)存儲在NSArray的一個對象array中,保存數(shù)據(jù)時執(zhí)行:

  1. [array writeToFile:filePath atomically:YES]; 

其中filePath是放在SandBox中的一個plist文件的完整路徑。

不過,使用plist文件還是有局限性的,例如,我們不好將一個圖片存儲在plist中。

這次的小例子中,我們將會通過歸檔實(shí)現(xiàn)數(shù)據(jù)的保存。當(dāng)程序運(yùn)行時,先檢查歸檔文件是否存在,如果存在的話就從歸檔文件中讀取數(shù)據(jù)顯示在界面上;如果歸檔文件不存在,就使用默認(rèn)設(shè)置。當(dāng)程序關(guān)閉時,會將數(shù)據(jù)存儲在歸檔文件中,這樣下次運(yùn)行程序時就會顯示上次的設(shè)置了。

1、運(yùn)行Xcode 4.3,新建一個Single View Application,名稱為:Archiving Test:

然后將準(zhǔn)備好的兩張圖片添加到工程中。

2、先進(jìn)行界面設(shè)計(jì):

單擊ViewController.xib,向其中添加控件:

然后向ViewController.h中為控件建立Outlet映射和Action映射,具體是為所有的TextField、ImageView、UISlider控件和UISwitch控件建立Outlet映射,為Button建立Action映射:

3、新建一個類,用于存儲我們的數(shù)據(jù):

在菜單欄依次選擇File — New — File…,在打開的窗口選擇Objective-C Class:

單擊Next,輸入類名:ArchivingData,選擇super class為NSObject:

單擊Next,選好位置和分組,點(diǎn)擊創(chuàng)建,完成類的建立。

4、打開ArchivingData.h,向其中添加屬性,以及協(xié)議:

  1. #import <Foundation/Foundation.h> 
  2.  
  3. @interface ArchivingData : NSObject <NSCoding, NSCopying> 
  4.  
  5. @property (copy, nonatomic) UIImage *image; 
  6. @property (copy, nonatomic) NSString *name; 
  7. @property (copy, nonatomic) NSString *gender; 
  8. @property (copy, nonatomic) NSString *vocation; 
  9. @property (copy, nonatomic) NSString *page; 
  10. @property float theSlider; 
  11. @property BOOL isSwitchOn; 
  12.  
  13. @end 

5、打開ArchivingData.m,向其中添加代碼:

5.1 在@implementation之前添加代碼:

  1. #define kImageKey @"ImageKey" 
  2. #define kNameKey @"NameKey" 
  3. #define kGenderKey @"GenderKey" 
  4. #define kVocationKey @"VocationKey" 
  5. #define kPageKey @"PageKey" 
  6. #define kTheSliderKey @"TheSliderKey" 
  7. #define kIsSwitchOn @"IsSwitchOnKey" 

5.2 在@implementation之后添加代碼:

  1. @synthesize image; 
  2. @synthesize name; 
  3. @synthesize gender; 
  4. @synthesize vocation; 
  5. @synthesize page; 
  6. @synthesize theSlider; 
  7. @synthesize isSwitchOn; 

5.3 在@end之前添加代碼:

  1. #pragma mark NSCoding 
  2. - (void)encodeWithCoder:(NSCoder *)aCoder { 
  3.     [aCoder encodeObject:image forKey:kImageKey]; 
  4.     [aCoder encodeObject:name forKey:kNameKey]; 
  5.     [aCoder encodeObject:gender forKey:kGenderKey]; 
  6.     [aCoder encodeObject:vocation forKey:kVocationKey]; 
  7.     [aCoder encodeObject:page forKey:kPageKey]; 
  8.     [aCoder encodeFloat:theSlider forKey:kTheSliderKey]; 
  9.     [aCoder encodeBool:isSwitchOn forKey:kIsSwitchOn]; 
  10. - (id)initWithCoder:(NSCoder *)aDecoder { 
  11.     if (self = [super init]) { 
  12.         image = [aDecoder decodeObjectForKey:kImageKey]; 
  13.         name = [aDecoder decodeObjectForKey:kNameKey]; 
  14.         gender = [aDecoder decodeObjectForKey:kGenderKey]; 
  15.         vocation = [aDecoder decodeObjectForKey:kVocationKey]; 
  16.         page = [aDecoder decodeObjectForKey:kPageKey]; 
  17.         theSlider = [aDecoder decodeFloatForKey:kTheSliderKey]; 
  18.         isSwitchOn = [aDecoder decodeBoolForKey:kIsSwitchOn]; 
  19.     } 
  20.     return self; 

5.4 在@end之前添加代碼:

  1. #pragma mark NSCoping 
  2. - (id)copyWithZone:(NSZone *)zone { 
  3.     ArchivingData *copy = [[[self class] allocWithZone:zone] init]; 
  4.     copy.image = self.image; 
  5.     copy.name = [self.name copyWithZone:zone]; 
  6.     copy.gender = [self.gender copyWithZone:zone]; 
  7.     copy.vocation = [self.vocation copyWithZone:zone]; 
  8.     copy.page = [self.page copyWithZone:zone]; 
  9.     copy.theSlider = self.theSlider; 
  10.     copy.isSwitchOn = self.isSwitchOn; 
  11.     return copy; 

在ArchivingData類中,我們添加了幾個屬性,這些屬性與上面創(chuàng)建的控件是一一對應(yīng)的。之后實(shí)現(xiàn)了幾個協(xié)議方法,這些方法分別用于編碼、解碼和復(fù)制。

別走開,下頁內(nèi)容更勁爆!

#p#

6、打開ViewController.h,向其中添加屬性和方法:

  1. @property (copy, nonatomic) NSString *archivingFilePath; 
  2.  
  3. - (void)applicationWillResignActive:(NSNotification *)notification; 

7、打開ViewController.m,添加代碼:

7.1 在@implementation之后添加代碼:

  1. @synthesize archivingFilePath; 

7.2 在#import之后添加代碼:

  1. #import "ArchivingData.h" 
  2.  
  3. #define kArchivingFileKey @"archivingFile" 
  4. #define kArchivingDataKey @"ArchivingDataKey" 

7.3 在viewDidLoad方法中添加代碼:

  1. - (void)viewDidLoad 
  2.     [super viewDidLoad]; 
  3.     // Do any additional setup after loading the view, typically from a nib. 
  4.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
  5.     NSString *documentsDirectory = [paths objectAtIndex:0]; 
  6.     self.archivingFilePath = [documentsDirectory stringByAppendingPathComponent:kArchivingFileKey]; 
  7.     NSFileManager *fileManager = [NSFileManager defaultManager]; 
  8.      
  9.     if ([fileManager fileExistsAtPath:self.archivingFilePath]) { 
  10.         //如果歸檔文件存在,則讀取其中內(nèi)容,顯示在界面上 
  11.         NSData *data = [[NSMutableData alloc] initWithContentsOfFile:self.archivingFilePath]; 
  12.         NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; 
  13.         ArchivingData *archivingData = [unarchiver decodeObjectForKey:kArchivingDataKey]; 
  14.         [unarchiver finishDecoding]; 
  15.         theImageView.image = archivingData.image; 
  16.         nameTextField.text = archivingData.name; 
  17.         genderTextField.text = archivingData.gender; 
  18.         vocationTextField.text = archivingData.vocation; 
  19.         pageTextField.text = archivingData.page; 
  20.         theSlider.value = archivingData.theSlider; 
  21.         theSwitch.on = archivingData.isSwitchOn; 
  22.     } else { 
  23.         //如果歸檔文件不存在,則設(shè)置imageView為boy.png 
  24.         theImageView.image = [UIImage imageNamed:@"boy.png"]; 
  25.     } 
  26.      
  27.     //當(dāng)程序進(jìn)入后臺時,將當(dāng)前設(shè)置項(xiàng)寫入歸檔文件 
  28.     UIApplication *app = [UIApplication sharedApplication]; 
  29.     [[NSNotificationCenter defaultCenter] addObserver:self 
  30.                                              selector:@selector(applicationWillResignActive:) 
  31.                                                  name:UIApplicationWillResignActiveNotification 
  32.                                                object:app]; 

7.4 找到switchImage方法,添加代碼:

  1. - (IBAction)switchImage:(id)sender { 
  2.     UIImage *image1 = [UIImage imageNamed:@"boy.png"]; 
  3.     UIImage *image2 = theImageView.image; 
  4.     if (![image1 isEqual:image2]) { 
  5.         theImageView.image = image1; 
  6.     } else { 
  7.         theImageView.image = [UIImage imageNamed:@"gemini.png"]; 
  8.     } 

7.5 在@end之前添加代碼:

  1. //程序進(jìn)入后臺時,保存設(shè)置 
  2. - (void)applicationWillResignActive:(NSNotification *)notification { 
  3.     ArchivingData *archivingData = [[ArchivingData alloc] init]; 
  4.     archivingData.image = self.theImageView.image; 
  5.     archivingData.name = self.nameTextField.text; 
  6.     archivingData.gender = self.genderTextField.text; 
  7.     archivingData.vocation = self.vocationTextField.text; 
  8.     archivingData.page = self.pageTextField.text; 
  9.     archivingData.theSlider = theSlider.value; 
  10.     archivingData.isSwitchOn = theSwitch.on; 
  11.     NSMutableData *data = [[NSMutableData alloc] init]; 
  12.     NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; 
  13.     [archiver encodeObject:archivingData forKey:kArchivingDataKey]; 
  14.     [archiver finishEncoding]; 
  15.     [data writeToFile:self.archivingFilePath atomically:YES]; 

8、***,為了使得鍵盤可以關(guān)閉,我們還要添加關(guān)閉鍵盤的操作,參考《iOS開發(fā)4:關(guān)閉鍵盤》中的第2步。

9、運(yùn)行程序

剛運(yùn)行程序如下左圖:

我們添加一些數(shù)據(jù),更換頭像,再調(diào)整Silder和Switch,如上圖右。

之后,按模擬器上的Home建,使得程序在后臺運(yùn)行。

此時,查看程序的SandBox,可以看到程序的Documents目錄下出現(xiàn)了文件archivingFile:

之后使用Xcode結(jié)束運(yùn)行,再運(yùn)行程序。程序第二次運(yùn)行時,顯示如上圖左,這說明我們實(shí)現(xiàn)了數(shù)據(jù)的***存儲。

責(zé)任編輯:閆佳明 來源: cocoachina
相關(guān)推薦

2009-07-17 14:51:22

.Net Micro

2018-06-21 15:14:51

Kubernetes存儲容器

2018-07-19 10:56:16

Kubernetes存儲架構(gòu)

2010-07-22 15:33:36

BlackBerry開

2011-08-10 09:50:43

iPhoneArchive數(shù)據(jù)

2017-11-21 14:32:05

容器持久存儲

2020-09-17 13:15:20

騰訊云冷數(shù)據(jù)存儲

2010-04-02 15:25:40

云歸檔

2009-04-09 13:58:58

JavaXML存儲

2009-01-19 16:09:44

NetApp賽門鐵克歸檔

2010-04-02 15:20:18

云存儲

2020-04-03 10:54:38

多云歸檔備份

2017-11-07 08:36:58

云計(jì)算歸檔存儲

2020-03-25 11:37:17

存儲云原生DevOps

2021-06-30 11:08:44

網(wǎng)絡(luò)本地化漫游網(wǎng)絡(luò)

2020-07-15 16:09:51

戴爾

2021-02-22 15:03:01

金山云歸檔存儲數(shù)據(jù)

2011-08-01 13:28:09

Oracle歸檔模式非歸檔模式

2010-05-07 13:03:01

Oracle通過存儲過

2023-11-23 15:05:02

玻璃歸檔存儲微軟
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號