成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

通過歸檔永久存儲數據

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

之前有文章簡單介紹了怎樣在Settings程序中設置自己的程序,并實現保存,使得下次運行自己的程序時顯示的還是上次的設置 項。而文章沙盒SandBox的結構介紹SandBox時,我們看到其實使用Settings程序設置后,數據是保存在一個plist文件的。

想要***保存數據,我們當然可以使用plist文件,當退出程序時,我們執行將數據寫入plist文件的操作,使用writeToFile:atomically:方法。

具有這個方法的類有:

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

例如,我們的數據存儲在NSArray的一個對象array中,保存數據時執行:

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

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

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

這次的小例子中,我們將會通過歸檔實現數據的保存。當程序運行時,先檢查歸檔文件是否存在,如果存在的話就從歸檔文件中讀取數據顯示在界面上;如果歸檔文件不存在,就使用默認設置。當程序關閉時,會將數據存儲在歸檔文件中,這樣下次運行程序時就會顯示上次的設置了。

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

然后將準備好的兩張圖片添加到工程中。

2、先進行界面設計:

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

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

3、新建一個類,用于存儲我們的數據:

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

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

單擊Next,選好位置和分組,點擊創建,完成類的建立。

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

  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類中,我們添加了幾個屬性,這些屬性與上面創建的控件是一一對應的。之后實現了幾個協議方法,這些方法分別用于編碼、解碼和復制。

別走開,下頁內容更勁爆!

#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.         //如果歸檔文件存在,則讀取其中內容,顯示在界面上 
  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.         //如果歸檔文件不存在,則設置imageView為boy.png 
  24.         theImageView.image = [UIImage imageNamed:@"boy.png"]; 
  25.     } 
  26.      
  27.     //當程序進入后臺時,將當前設置項寫入歸檔文件 
  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. //程序進入后臺時,保存設置 
  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、***,為了使得鍵盤可以關閉,我們還要添加關閉鍵盤的操作,參考《iOS開發4:關閉鍵盤》中的第2步。

9、運行程序

剛運行程序如下左圖:

我們添加一些數據,更換頭像,再調整Silder和Switch,如上圖右。

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

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

之后使用Xcode結束運行,再運行程序。程序第二次運行時,顯示如上圖左,這說明我們實現了數據的***存儲。

責任編輯:閆佳明 來源: cocoachina
相關推薦

2009-07-17 14:51:22

.Net Micro

2018-06-21 15:14:51

Kubernetes存儲容器

2018-07-19 10:56:16

Kubernetes存儲架構

2010-07-22 15:33:36

BlackBerry開

2011-08-10 09:50:43

iPhoneArchive數據

2017-11-21 14:32:05

容器持久存儲

2020-09-17 13:15:20

騰訊云冷數據存儲

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

云存儲

2025-05-08 01:20:00

2020-04-03 10:54:38

多云歸檔備份

2025-06-12 00:00:00

對象存儲多站點

2017-11-07 08:36:58

云計算歸檔存儲

2020-03-25 11:37:17

存儲云原生DevOps

2021-02-22 15:03:01

金山云歸檔存儲數據

2020-07-15 16:09:51

戴爾

2011-08-01 13:28:09

Oracle歸檔模式非歸檔模式

2021-06-30 11:08:44

網絡本地化漫游網絡
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 超碰电影 | 亚洲精品成人 | 成人精品在线观看 | 久久久视 | 国产在线视频一区 | 午夜精品一区 | 日本欧美黄色片 | 天堂在线中文 | 91在线免费视频 | 亚洲精品一区二区网址 | 九九亚洲 | 国产激情一区二区三区 | 久久久久久国产免费视网址 | 亚洲欧美一区二区三区国产精品 | 天天天操 | 精品伊人 | 亚洲成人免费av | 欧美黄在线观看 | 欧美乱码精品一区二区三区 | 中文字幕在线精品 | 一区二区三区电影在线观看 | 国内精品久久久久久影视8 最新黄色在线观看 | 在线亚洲电影 | 蜜桃视频成人 | 美女久久久久 | 日韩欧美亚洲一区 | 四色永久 | 国产视频1区 | 欧美激情视频一区二区三区在线播放 | 香蕉超碰| 操久久 | 亚洲精品字幕 | 精品日本中文字幕 | 欧美成人免费在线 | 91在线观看免费 | 久热精品免费 | 欧美a区 | 欧美福利专区 | 天天玩天天干天天操 | 自拍偷拍一区二区三区 | 国产精品99久久久久久动医院 |