服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - 以代码实例总结iOS应用开发中数据的存储方式

以代码实例总结iOS应用开发中数据的存储方式

2021-01-08 14:13喝醉的毛毛虫 IOS

这篇文章主要介绍了iOS应用开发中数据的存储方式的实例总结,代码为传统的Objective-C语言,需要的朋友可以参考下

ios数据存储包括以下几种存储机制:

  • 属性列表
  • 对象归档
  • SQLite3
  • CoreData
  • AppSettings
  • 普通文件存储

1、属性列表

复制代码 代码如下:

// 
//  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 

复制代码 代码如下:

// 
//  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; 
 
//数据文件的完整路径 
- (NSString *)dataFilePath { 
    //检索Documents目录路径。第二个参数表示将搜索限制在我们的应用程序沙盒中 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    //每个应用程序只有一个Documents目录 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    //创建文件名 
    return [documentsDirectory stringByAppendingPathComponent:kFilename]; 

 
//应用程序退出时,将数据保存到属性列表文件 
- (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]; 
    //检查数据文件是否存在 
    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、对象归档

复制代码 代码如下:

// 
//  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 


复制代码 代码如下:

// 
//  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 

复制代码 代码如下:

// 
//  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 

复制代码 代码如下:

// 
//  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; 
 
//数据文件的完整路径 
- (NSString *)dataFilePath { 
    //检索Documents目录路径。第二个参数表示将搜索限制在我们的应用程序沙盒中 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    //每个应用程序只有一个Documents目录 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    //创建文件名 
    return [documentsDirectory stringByAppendingPathComponent:kFilename]; 

 
//应用程序退出时,将数据保存到属性列表文件 
- (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];//用于存储编码的数据 
    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]; 
    //检查数据文件是否存在 
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { 
         
        //从文件获取用于解码的数据 
        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

复制代码 代码如下:

// 
//  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 

复制代码 代码如下:

// 
//  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; 
 
//数据文件的完整路径 
- (NSString *)dataFilePath { 
    //检索Documents目录路径。第二个参数表示将搜索限制在我们的应用程序沙盒中 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    //每个应用程序只有一个Documents目录 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    //创建文件名 
    return [documentsDirectory stringByAppendingPathComponent:kFilename]; 

 
//应用程序退出时,将数据保存到属性列表文件 
- (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语句编译为sqlite内部一个结构体(sqlite3_stmt),类似java JDBC的PreparedStatement预编译 
        if (sqlite3_prepare_v2(database, update, -1, &stmt, nil) == SQLITE_OK) { 
            //在bind参数的时候,参数列表的index从1开始,而取出数据的时候,列的index是从0开始 
            sqlite3_bind_int(stmt, 1, i); 
            sqlite3_bind_text(stmt, 2, [field.text UTF8String], -1, NULL); 
        } else { 
            NSAssert(0, @"Error:Failed to prepare statemen"); 
        } 
        //执行SQL文,获取结果  
        int result = sqlite3_step(stmt); 
        if(result != SQLITE_DONE) { 
            NSAssert1(0, @"Error updating table: %d", result); 
        } 
        //释放stmt占用的内存(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]; 
    //检查数据文件是否存在 
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { 
        //打开数据库 
        sqlite3 *database; 
        if(sqlite3_open([filePath UTF8String], &database) != SQLITE_OK) { 
            sqlite3_close(database); 
            NSAssert(0, @"Failed to open database"); 
        } 
         
        //创建表 
        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); 
        } 
 
        //查询 
        NSString *query = @"SELECT ROW, FIELD_DATA FROM FIELDS ORDER BY ROW"; 
        sqlite3_stmt *statement; 
        //设置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占用的内存(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

复制代码 代码如下:

// 
//  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 

复制代码 代码如下:

// 
//  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]; 
         
        //创建提取请求 
        NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
        //创建实体描述并关联到请求 
        NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Line" 
                                                             inManagedObjectContext:context]; 
        [request setEntity:entityDescription]; 
         
        //设置检索数据的条件 
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"(lineNum = %d)", i]; 
        [request setPredicate:pred]; 
         
        NSManagedObject *theLine = nil; 
        ////检查是否返回了标准匹配的对象,如果有则加载它,如果没有则创建一个新的托管对象来保存此字段的文本 
        NSArray *objects = [context executeFetchRequest:request error:&error]; 
        if(!objects) { 
            NSLog(@"There was an error"); 
        } 
        //if(objects.count > 0) { 
        //  theLine = [objects objectAtIndex:0]; 
        //} else { 
            //创建一个新的托管对象来保存此字段的文本 
            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]; 
    //创建一个实体描述 
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Line" inManagedObjectContext:context]; 
    //创建一个请求,用于提取对象 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    [request setEntity:entityDescription]; 
     
    //检索对象 
    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
复制代码 代码如下:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

6、普通文件存储

 

这种方式即自己将数据通过IO保存到文件,或从文件读取。

延伸 · 阅读

精彩推荐
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22