最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

以代碼實(shí)例總結(jié)iOS應(yīng)用開(kāi)發(fā)中數(shù)據(jù)的存儲(chǔ)方式

 更新時(shí)間:2016年02月24日 16:27:18   作者:喝醉的毛毛蟲(chóng)  
這篇文章主要介紹了iOS應(yīng)用開(kāi)發(fā)中數(shù)據(jù)的存儲(chǔ)方式的實(shí)例總結(jié),代碼為傳統(tǒng)的Objective-C語(yǔ)言,需要的朋友可以參考下

ios數(shù)據(jù)存儲(chǔ)包括以下幾種存儲(chǔ)機(jī)制:

  • 屬性列表
  • 對(duì)象歸檔
  • SQLite3
  • CoreData
  • AppSettings
  • 普通文件存儲(chǔ)

1、屬性列表

復(fù)制代碼 代碼如下:

// 
//  Persistence1ViewController.h 
//  Persistence1 
// 
//  Created by liu lavy on 11-10-3. 
//  Copyright 2011 __MyCompanyName__. All rights reserved. 
// 
 
#import <UIKit/UIKit.h> 
#define kFilename @"data.plist" 
 
@interface Persistence1ViewController : UIViewController { 
    UITextField *filed1; 
    UITextField *field2; 
    UITextField *field3; 
    UITextField *field4; 

@property (nonatomic, retain) IBOutlet UITextField *field1; 
@property (nonatomic, retain) IBOutlet UITextField *field2; 
@property (nonatomic, retain) IBOutlet UITextField *field3; 
@property (nonatomic, retain) IBOutlet UITextField *field4; 
 
- (NSString *)dataFilePath; 
- (void)applicationWillResignActive:(NSNotification *)notification; 
 
@end 

復(fù)制代碼 代碼如下:

// 
//  Persistence1ViewController.m 
//  Persistence1 
// 
//  Created by liu lavy on 11-10-3. 
//  Copyright 2011 __MyCompanyName__. All rights reserved. 
// 
 
#import "Persistence1ViewController.h" 
 
@implementation Persistence1ViewController 
@synthesize field1; 
@synthesize field2; 
@synthesize field3; 
@synthesize field4; 
 
//數(shù)據(jù)文件的完整路徑 
- (NSString *)dataFilePath { 
    //檢索Documents目錄路徑。第二個(gè)參數(shù)表示將搜索限制在我們的應(yīng)用程序沙盒中 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    //每個(gè)應(yīng)用程序只有一個(gè)Documents目錄 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    //創(chuàng)建文件名 
    return [documentsDirectory stringByAppendingPathComponent:kFilename]; 

 
//應(yīng)用程序退出時(shí),將數(shù)據(jù)保存到屬性列表文件 
- (void)applicationWillResignActive:(NSNotification *)notification { 
    NSMutableArray *array = [[NSMutableArray alloc] init]; 
    [array addObject: field1.text]; 
    [array addObject: field2.text]; 
    [array addObject: field3.text]; 
    [array addObject: field4.text]; 
    [array writeToFile:[self dataFilePath] atomically:YES]; 
    [array release]; 

 
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/ 
 
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/ 
 
 
 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSString *filePath = [self dataFilePath]; 
    //檢查數(shù)據(jù)文件是否存在 
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { 
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath]; 
        field1.text = [array objectAtIndex:0]; 
        field2.text = [array objectAtIndex:1]; 
        field3.text = [array objectAtIndex:2]; 
        field4.text = [array objectAtIndex:3]; 
        [array release]; 
    } 
    UIApplication *app = [UIApplication sharedApplication]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                            selector:@selector(applicationWillResignActive:) 
                                            name:UIApplicationWillResignActiveNotification 
                                            object:app]; 
    [super viewDidLoad]; 

 
 
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/ 
 
- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 
     
    // Release any cached data, images, etc that aren't in use. 

 
- (void)viewDidUnload { 
    self.field1 = nil; 
    self.field2 = nil; 
    self.field3 = nil; 
    self.field4 = nil; 
    [super viewDidUnload]; 

 
 
- (void)dealloc { 
    [field1 release]; 
    [field2 release]; 
    [field3 release]; 
    [field4 release]; 
    [super dealloc]; 

 
@end 

2、對(duì)象歸檔

復(fù)制代碼 代碼如下:

// 
//  Fourlines.h 
//  Persistence2 
// 
//  Created by liu lavy on 11-10-3. 
//  Copyright 2011 __MyCompanyName__. All rights reserved. 
// 
 
#import <Foundation/Foundation.h> 
 
@interface Fourlines : NSObject <NSCoding, NSCopying> { 
    NSString *field1; 
    NSString *field2; 
    NSString *field3; 
    NSString *field4; 

@property (nonatomic, retain) NSString *field1; 
@property (nonatomic, retain) NSString *field2; 
@property (nonatomic, retain) NSString *field3; 
@property (nonatomic, retain) NSString *field4; 
 

@end 


復(fù)制代碼 代碼如下:

// 
//  Fourlines.m 
//  Persistence2 
// 
//  Created by liu lavy on 11-10-3. 
//  Copyright 2011 __MyCompanyName__. All rights reserved. 
// 
 
#import "Fourlines.h" 
 
#define kField1Key @"Field1" 
#define kField2Key @"Field2" 
#define kField3Key @"Field3" 
#define kField4Key @"Field4" 
 
@implementation Fourlines 
@synthesize field1; 
@synthesize field2; 
@synthesize field3; 
@synthesize field4; 
 
#pragma mark NSCoding 
- (void)encodeWithCoder:(NSCoder *)aCoder { 
    [aCoder encodeObject:field1 forKey:kField1Key]; 
    [aCoder encodeObject:field2 forKey:kField2Key]; 
    [aCoder encodeObject:field3 forKey:kField3Key]; 
    [aCoder encodeObject:field4 forKey:kField4Key]; 

 
-(id) initWithCoder:(NSCoder *)aDecoder { 
    if(self = [super init]) { 
        field1 = [[aDecoder decodeObjectForKey:kField1Key] retain]; 
        field2 = [[aDecoder decodeObjectForKey:kField2Key] retain]; 
        field3 = [[aDecoder decodeObjectForKey:kField3Key] retain]; 
        field4 = [[aDecoder decodeObjectForKey:kField4Key] retain]; 
    } 
    return self; 

 
#pragma mark - 
#pragma mark NSCopying 
- (id) copyWithZone:(NSZone *)zone { 
    Fourlines *copy = [[[self class] allocWithZone: zone] init]; 
    copy.field1 = [[self.field1 copyWithZone: zone] autorelease]; 
    copy.field2 = [[self.field2 copyWithZone: zone] autorelease]; 
    copy.field3 = [[self.field3 copyWithZone: zone] autorelease]; 
    copy.field4 = [[self.field4 copyWithZone: zone] autorelease]; 
    return copy; 

@end 

復(fù)制代碼 代碼如下:

// 
//  Persistence2ViewController.h 
//  Persistence2 
// 
//  Created by liu lavy on 11-10-3. 
//  Copyright 2011 __MyCompanyName__. All rights reserved. 
// 
 
#import <UIKit/UIKit.h> 
#define kFilename @"archive" 
#define kDataKey @"Data" 
 
@interface Persistence2ViewController : UIViewController { 
    UITextField *filed1; 
    UITextField *field2; 
    UITextField *field3; 
    UITextField *field4; 

@property (nonatomic, retain) IBOutlet UITextField *field1; 
@property (nonatomic, retain) IBOutlet UITextField *field2; 
@property (nonatomic, retain) IBOutlet UITextField *field3; 
@property (nonatomic, retain) IBOutlet UITextField *field4; 
 
- (NSString *)dataFilePath; 
- (void)applicationWillResignActive:(NSNotification *)notification; 
 
@end 

復(fù)制代碼 代碼如下:

// 
//  Persistence2ViewController.m 
//  Persistence2 
// 
//  Created by liu lavy on 11-10-3. 
//  Copyright 2011 __MyCompanyName__. All rights reserved. 
// 
 
#import "Persistence2ViewController.h" 
#import "Fourlines.h" 
 
@implementation Persistence2ViewController 
@synthesize field1; 
@synthesize field2; 
@synthesize field3; 
@synthesize field4; 
 
//數(shù)據(jù)文件的完整路徑 
- (NSString *)dataFilePath { 
    //檢索Documents目錄路徑。第二個(gè)參數(shù)表示將搜索限制在我們的應(yīng)用程序沙盒中 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    //每個(gè)應(yīng)用程序只有一個(gè)Documents目錄 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    //創(chuàng)建文件名 
    return [documentsDirectory stringByAppendingPathComponent:kFilename]; 

 
//應(yīng)用程序退出時(shí),將數(shù)據(jù)保存到屬性列表文件 
- (void)applicationWillResignActive:(NSNotification *)notification { 
    Fourlines *fourlines = [[Fourlines alloc] init]; 
    fourlines.field1 = field1.text; 
    fourlines.field2 = field2.text; 
    fourlines.field3 = field3.text; 
    fourlines.field4 = field4.text; 
     
    NSMutableData *data = [[NSMutableData alloc] init];//用于存儲(chǔ)編碼的數(shù)據(jù) 
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; 
     
    [archiver encodeObject:fourlines forKey:kDataKey]; 
    [archiver finishEncoding]; 
    [data writeToFile:[self dataFilePath] atomically:YES]; 
     
    [fourlines release]; 
    [archiver release]; 
    [data release]; 

 
/*
 // The designated initializer. Override to perform setup that is required before the view is loaded.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 if (self) {
 // Custom initialization
 }
 return self;
 }
 */ 
 
/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView {
 }
 */ 
 
 
 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSString *filePath = [self dataFilePath]; 
    //檢查數(shù)據(jù)文件是否存在 
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { 
         
        //從文件獲取用于解碼的數(shù)據(jù) 
        NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]]; 
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; 
         
        Fourlines *fourlines = [unarchiver decodeObjectForKey:kDataKey]; 
        [unarchiver finishDecoding]; 
         
        field1.text = fourlines.field1; 
        field2.text = fourlines.field2; 
        field3.text = fourlines.field3; 
        field4.text = fourlines.field4; 
         
        [unarchiver release]; 
        [data release]; 
    } 
    UIApplication *app = [UIApplication sharedApplication]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(applicationWillResignActive:) 
                                             name:UIApplicationWillResignActiveNotification 
                                             object:app]; 
    [super viewDidLoad]; 

 
 
/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */ 
 
- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 
     
    // Release any cached data, images, etc that aren't in use. 

 
- (void)viewDidUnload { 
    self.field1 = nil; 
    self.field2 = nil; 
    self.field3 = nil; 
    self.field4 = nil; 
    [super viewDidUnload]; 

 
 
- (void)dealloc { 
    [field1 release]; 
    [field2 release]; 
    [field3 release]; 
    [field4 release]; 
    [super dealloc]; 

@end 

3、SQLite

復(fù)制代碼 代碼如下:

// 
//  Persistence3ViewController.h 
//  Persistence3 
// 
//  Created by liu lavy on 11-10-3. 
//  Copyright 2011 __MyCompanyName__. All rights reserved. 
// 
 
#import <UIKit/UIKit.h> 
#define kFilename @"data.sqlite3" 
 
@interface Persistence3ViewController : UIViewController { 
    UITextField *filed1; 
    UITextField *field2; 
    UITextField *field3; 
    UITextField *field4; 

@property (nonatomic, retain) IBOutlet UITextField *field1; 
@property (nonatomic, retain) IBOutlet UITextField *field2; 
@property (nonatomic, retain) IBOutlet UITextField *field3; 
@property (nonatomic, retain) IBOutlet UITextField *field4; 
 
- (NSString *)dataFilePath; 
- (void)applicationWillResignActive:(NSNotification *)notification; 
@end 

復(fù)制代碼 代碼如下:

// 
//  Persistence3ViewController.m 
//  Persistence3 
// 
//  Created by liu lavy on 11-10-3. 
//  Copyright 2011 __MyCompanyName__. All rights reserved. 
// 
 
#import "Persistence3ViewController.h" 
#import <sqlite3.h> 
 
@implementation Persistence3ViewController 
@synthesize field1; 
@synthesize field2; 
@synthesize field3; 
@synthesize field4; 
 
//數(shù)據(jù)文件的完整路徑 
- (NSString *)dataFilePath { 
    //檢索Documents目錄路徑。第二個(gè)參數(shù)表示將搜索限制在我們的應(yīng)用程序沙盒中 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    //每個(gè)應(yīng)用程序只有一個(gè)Documents目錄 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    //創(chuàng)建文件名 
    return [documentsDirectory stringByAppendingPathComponent:kFilename]; 

 
//應(yīng)用程序退出時(shí),將數(shù)據(jù)保存到屬性列表文件 
- (void)applicationWillResignActive:(NSNotification *)notification { 
    sqlite3 *database; 
    if(sqlite3_open([[self dataFilePath] UTF8String], &database) != SQLITE_OK) { 
        sqlite3_close(database); 
        NSAssert(0, @"Failed to open database"); 
    } 
     
    for(int i = 1; i <= 4; i++) { 
        NSString *fieldname = [[NSString alloc] initWithFormat:@"field%d", i]; 
        UITextField *field = [self valueForKey:fieldname]; 
        [fieldname release]; 
         
        char *update = "INSERT OR REPLACE INTO FIELDS (ROW, FIELD_DATA) VALUES (?, ?);"; 
        sqlite3_stmt *stmt; 
        //將SQL語(yǔ)句編譯為sqlite內(nèi)部一個(gè)結(jié)構(gòu)體(sqlite3_stmt),類(lèi)似java JDBC的PreparedStatement預(yù)編譯 
        if (sqlite3_prepare_v2(database, update, -1, &stmt, nil) == SQLITE_OK) { 
            //在bind參數(shù)的時(shí)候,參數(shù)列表的index從1開(kāi)始,而取出數(shù)據(jù)的時(shí)候,列的index是從0開(kāi)始 
            sqlite3_bind_int(stmt, 1, i); 
            sqlite3_bind_text(stmt, 2, [field.text UTF8String], -1, NULL); 
        } else { 
            NSAssert(0, @"Error:Failed to prepare statemen"); 
        } 
        //執(zhí)行SQL文,獲取結(jié)果  
        int result = sqlite3_step(stmt); 
        if(result != SQLITE_DONE) { 
            NSAssert1(0, @"Error updating table: %d", result); 
        } 
        //釋放stmt占用的內(nèi)存(sqlite3_prepare_v2()分配的) 
        sqlite3_finalize(stmt); 
    } 
    sqlite3_close(database); 

 
/*
 // The designated initializer. Override to perform setup that is required before the view is loaded.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 if (self) {
 // Custom initialization
 }
 return self;
 }
 */ 
 
/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView {
 }
 */ 
 
 
 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSString *filePath = [self dataFilePath]; 
    //檢查數(shù)據(jù)文件是否存在 
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { 
        //打開(kāi)數(shù)據(jù)庫(kù) 
        sqlite3 *database; 
        if(sqlite3_open([filePath UTF8String], &database) != SQLITE_OK) { 
            sqlite3_close(database); 
            NSAssert(0, @"Failed to open database"); 
        } 
         
        //創(chuàng)建表 
        char *errorMsg; 
        NSString *createSQL =  
            @"CREATE TABLE IF NOT EXISTS FIELDS (ROW INTEGER PRIMARY KEY, FIELD_DATA TEXT);"; 
        if(sqlite3_exec(database, [createSQL UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) { 
            sqlite3_close(database); 
            NSAssert(0, @"Error creating table: %s", errorMsg); 
        } 
 
        //查詢(xún) 
        NSString *query = @"SELECT ROW, FIELD_DATA FROM FIELDS ORDER BY ROW"; 
        sqlite3_stmt *statement; 
        //設(shè)置nByte可以加速操作 
        if(sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) { 
            while (sqlite3_step(statement) == SQLITE_ROW) {//返回每一行 
                int row = sqlite3_column_int(statement, 0); 
                char *rowData = (char *)sqlite3_column_text(statement, 1); 
                 
                NSString *fieldName = [[NSString alloc] initWithFormat:@"field%d", row]; 
                NSString *fieldValue = [[NSString alloc] initWithUTF8String:rowData]; 
                 
                UITextField *field = [self valueForKey:fieldName]; 
                field.text = fieldValue; 
                 
                [fieldName release]; 
                [fieldValue release]; 
            } 
            //釋放statement占用的內(nèi)存(sqlite3_prepare()分配的) 
            sqlite3_finalize(statement); 
        } 
        sqlite3_close(database); 
    } 
     
     
    UIApplication *app = [UIApplication sharedApplication]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(applicationWillResignActive:) 
                                                 name:UIApplicationWillResignActiveNotification 
                                               object:app]; 
    [super viewDidLoad]; 

 
 
/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */ 
 
- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 
     
    // Release any cached data, images, etc that aren't in use. 

 
- (void)viewDidUnload { 
    self.field1 = nil; 
    self.field2 = nil; 
    self.field3 = nil; 
    self.field4 = nil; 
    [super viewDidUnload]; 

 
 
- (void)dealloc { 
    [field1 release]; 
    [field2 release]; 
    [field3 release]; 
    [field4 release]; 
    [super dealloc]; 

 
@end 

4、Core Data

復(fù)制代碼 代碼如下:

// 
//  PersistenceViewController.h 
//  Persistence4 
// 
//  Created by liu lavy on 11-10-3. 
//  Copyright 2011 __MyCompanyName__. All rights reserved. 
// 
 
#import <UIKit/UIKit.h> 
 
 
@interface PersistenceViewController : UIViewController { 
    UITextField *filed1; 
    UITextField *field2; 
    UITextField *field3; 
    UITextField *field4; 

@property (nonatomic, retain) IBOutlet UITextField *field1; 
@property (nonatomic, retain) IBOutlet UITextField *field2; 
@property (nonatomic, retain) IBOutlet UITextField *field3; 
@property (nonatomic, retain) IBOutlet UITextField *field4; 
 
@end 

復(fù)制代碼 代碼如下:

// 
//  PersistenceViewController.m 
//  Persistence4 
// 
//  Created by liu lavy on 11-10-3. 
//  Copyright 2011 __MyCompanyName__. All rights reserved. 
// 
 
#import "PersistenceViewController.h" 
#import "Persistence4AppDelegate.h" 
 
@implementation PersistenceViewController 
@synthesize field1; 
@synthesize field2; 
@synthesize field3; 
@synthesize field4; 
 
 
-(void) applicationWillResignActive:(NSNotification *)notification { 
    Persistence4AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
    NSManagedObjectContext *context = [appDelegate managedObjectContext]; 
    NSError *error; 
    for(int i = 1; i <= 4; i++) { 
        NSString *fieldName = [NSString stringWithFormat:@"field%d", i]; 
        UITextField *theField = [self valueForKey:fieldName]; 
         
        //創(chuàng)建提取請(qǐng)求 
        NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
        //創(chuàng)建實(shí)體描述并關(guān)聯(lián)到請(qǐng)求 
        NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Line" 
                                                             inManagedObjectContext:context]; 
        [request setEntity:entityDescription]; 
         
        //設(shè)置檢索數(shù)據(jù)的條件 
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"(lineNum = %d)", i]; 
        [request setPredicate:pred]; 
         
        NSManagedObject *theLine = nil; 
        ////檢查是否返回了標(biāo)準(zhǔn)匹配的對(duì)象,如果有則加載它,如果沒(méi)有則創(chuàng)建一個(gè)新的托管對(duì)象來(lái)保存此字段的文本 
        NSArray *objects = [context executeFetchRequest:request error:&error]; 
        if(!objects) { 
            NSLog(@"There was an error"); 
        } 
        //if(objects.count > 0) { 
        //  theLine = [objects objectAtIndex:0]; 
        //} else { 
            //創(chuàng)建一個(gè)新的托管對(duì)象來(lái)保存此字段的文本 
            theLine = [NSEntityDescription insertNewObjectForEntityForName:@"Line" 
                                                    inManagedObjectContext:context]; 
            [theLine setValue:[NSNumber numberWithInt:i] forKey:@"lineNum"]; 
            [theLine setValue:theField.text forKey:@"lineText"]; 
        //} 
        [request release]; 
    } 
    //通知上下文保存更改 
    [context save:&error]; 
     

 
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/ 
 
 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    Persistence4AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
     
    NSManagedObjectContext *context = [appDelegate managedObjectContext]; 
    //創(chuàng)建一個(gè)實(shí)體描述 
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Line" inManagedObjectContext:context]; 
    //創(chuàng)建一個(gè)請(qǐng)求,用于提取對(duì)象 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    [request setEntity:entityDescription]; 
     
    //檢索對(duì)象 
    NSError *error; 
    NSArray *objects = [context executeFetchRequest:request error:&error]; 
    if(!objects) { 
        NSLog(@"There was an error!"); 
    } 
    for(NSManagedObject *obj in objects) { 
        NSNumber *lineNum = [obj valueForKey:@"lineNum"]; 
        NSString *lineText = [obj valueForKey:@"lineText"]; 
        NSString *fieldName = [NSString stringWithFormat:@"field%d", [lineNum integerValue]]; 
        UITextField *textField = [self valueForKey:fieldName]; 
        textField.text = lineText; 
    } 
    [request release]; 
     
    UIApplication *app = [UIApplication sharedApplication]; 
    [[NSNotificationCenter defaultCenter] addObserver:self  
                                             selector:@selector(applicationWillResignActive:)  
                                                 name:UIApplicationWillResignActiveNotification 
                                               object:app]; 
    [super viewDidLoad]; 

 
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/ 
 
- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 
     
    // Release any cached data, images, etc. that aren't in use. 

 
- (void)viewDidUnload { 
    self.field1 = nil; 
    self.field2 = nil; 
    self.field3 = nil; 
    self.field4 = nil; 
    [super viewDidUnload]; 

 
 
- (void)dealloc { 
    [field1 release]; 
    [field2 release]; 
    [field3 release]; 
    [field4 release]; 
    [super dealloc]; 

 
 
 
@end 

5、AppSettings
復(fù)制代碼 代碼如下:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

6、普通文件存儲(chǔ)

這種方式即自己將數(shù)據(jù)通過(guò)IO保存到文件,或從文件讀取。

相關(guān)文章

  • iOS性能優(yōu)化淺析

    iOS性能優(yōu)化淺析

    給大家通過(guò)理論和經(jīng)驗(yàn)分析了IOS性能優(yōu)化各方面的問(wèn)題,以及處理的對(duì)應(yīng)辦法,有需要的朋友參考下。
    2018-02-02
  • 詳解iOS集成融云SDK即時(shí)通訊整理

    詳解iOS集成融云SDK即時(shí)通訊整理

    這篇文章主要介紹了詳解iOS集成融云SDK即時(shí)通訊整理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • IOS實(shí)現(xiàn)上滑隱藏NvaigtionBar而下拉則顯示效果

    IOS實(shí)現(xiàn)上滑隱藏NvaigtionBar而下拉則顯示效果

    這篇文章給大家介紹了如何實(shí)現(xiàn)APP上滑時(shí)隱藏navigationBar而下拉則又會(huì)顯示,雖然也是隱藏但是效果和其他完全不一樣,因?yàn)橐郧皼](méi)做過(guò)所以試著去實(shí)現(xiàn)一下,現(xiàn)在分享給大家,有需要的可以參考借鑒。
    2016-09-09
  • iOS 中事件的響應(yīng)鏈和傳遞鏈

    iOS 中事件的響應(yīng)鏈和傳遞鏈

    iOS事件鏈有兩條:事件的響應(yīng)鏈;Hit-Testing事件的傳遞鏈。這篇文章主要介紹了iOS 中事件的響應(yīng)鏈和傳遞鏈,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • iPhone/iPad開(kāi)發(fā)通過(guò)LocalNotification實(shí)現(xiàn)iOS定時(shí)本地推送功能

    iPhone/iPad開(kāi)發(fā)通過(guò)LocalNotification實(shí)現(xiàn)iOS定時(shí)本地推送功能

    這篇文章主要介紹了iPhone/iPad開(kāi)發(fā)之通過(guò)LocalNotification實(shí)現(xiàn)iOS定時(shí)本地推送功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • iOS應(yīng)用開(kāi)發(fā)中UITableView的分割線(xiàn)的一些設(shè)置技巧

    iOS應(yīng)用開(kāi)發(fā)中UITableView的分割線(xiàn)的一些設(shè)置技巧

    這篇文章主要介紹了iOS應(yīng)用開(kāi)發(fā)中UITableView分割線(xiàn)的一些設(shè)置技巧,包括消除分割線(xiàn)的方法,示例代碼為傳統(tǒng)的Objective-C語(yǔ)言,需要的朋友可以參考下
    2016-03-03
  • OpenCV??iOS?圖像處理編程入門(mén)詳細(xì)教程

    OpenCV??iOS?圖像處理編程入門(mén)詳細(xì)教程

    這篇文章主要介紹了OpenCV?iOS?圖像處理編程入門(mén),OpenCV?的應(yīng)用領(lǐng)域非常廣泛,包括圖像拼接、圖像降噪、產(chǎn)品質(zhì)檢、人機(jī)交互、人臉識(shí)別、動(dòng)作識(shí)別、動(dòng)作跟蹤、無(wú)人駕駛等,對(duì)于圖像處理、人機(jī)交互及機(jī)器學(xué)習(xí)算法感興趣的可以選擇一個(gè)方向進(jìn)行深入的研究
    2022-07-07
  • IOS 詳解socket編程[oc]粘包、半包處理

    IOS 詳解socket編程[oc]粘包、半包處理

    這篇文章主要介紹了IOS 詳解socket編程[oc]粘包、半包處理的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • IOS 解決推送本地國(guó)際化 loc-key 本地化失敗的問(wèn)題

    IOS 解決推送本地國(guó)際化 loc-key 本地化失敗的問(wèn)題

    本文主要介紹IOS 推送國(guó)際化問(wèn)題,在開(kāi)發(fā) IOS 項(xiàng)目過(guò)程中對(duì)軟件的國(guó)際化有的項(xiàng)目需求是需要的,這里給大家一個(gè)示例,有需要的小伙伴可以參考下
    2016-07-07
  • iOS中設(shè)置網(wǎng)絡(luò)超時(shí)時(shí)間+模擬的方法詳解

    iOS中設(shè)置網(wǎng)絡(luò)超時(shí)時(shí)間+模擬的方法詳解

    這篇文章主要介紹了在iOS中設(shè)置網(wǎng)絡(luò)超時(shí)時(shí)間+模擬的方法,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。
    2017-04-04

最新評(píng)論

灵川县| 霍林郭勒市| 北流市| 买车| 随州市| 宣汉县| 赞皇县| 新野县| 怀宁县| 东平县| 邳州市| 绥中县| 长葛市| 酉阳| 吉水县| 蓝田县| 鸡东县| 乡城县| 山东省| 瑞安市| 襄城县| 宁武县| 石台县| 永胜县| 锦州市| 昌都县| 武安市| 达拉特旗| 海兴县| 湖南省| 北碚区| 余江县| 崇仁县| 德清县| 平泉县| 安陆市| 遵化市| 武城县| 如东县| 博野县| 安溪县|