什么网站做简历模板,软文范例大全1000字,做网站搞个物理服务器,wordpress 安全性设置一、应用程序沙盒 IOS应用程序职能在系统为该应用所分配的文件区域下读写文件#xff0c;这个文件区域就是应用程序沙盒。所有的非代码文件如#xff1a;图片、声音、映象等等都存放在此。 在mac中command#xff0b;shift#xff0b;G命令#xff0c;然后输入users/用户名…一、应用程序沙盒 IOS应用程序职能在系统为该应用所分配的文件区域下读写文件这个文件区域就是应用程序沙盒。所有的非代码文件如图片、声音、映象等等都存放在此。 在mac中commandshiftG命令然后输入users/用户名/library命令进入库然后依次进入application support/iphone simulator/版本/applications文件夹这里面的各个文件夹对应着各个应用程序。 Documents除了基于NSUserDefaults的首选项设置外应用程序的数据、文件都保存在该目录下 Library:基于NSUserDefaults的首选项参数保存在Library/Preferences下 tmp:应用程序存储临时文件ios同步时itunes不会同步这里面的数据当应用程序不在需要这些文件时应当删除以避免占用空间。 获取documents路径 1 NSArray *paths NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
2 NSString *dd [paths objectAtIndex:0]; 获取tmp目录 1 NSString *tempPath NSTemporaryDirectory(); 那么我们在保存数据时使用哪种发式比较好呢这个当然还要具体情况具体对待。当保存小数据量的数据时可以使用NSArray或者NSDictionary调用writeToFile:atomically:方法写入一个文件使用时读取文件内容即可。 当保存大数据量的数据时可以选择使用sqlliteios提供了CoreData框架。 二、应用程序参数与用户默认设置 1、使用Settings Bundle Settings Bundle时应用程序中的一组特殊文件用于保存较简单的各种配置信息。如果使用了Settings Bundleios自带的设置应用则会显示你的app 2、使用NSUserDefaults读取、保存应用程序参数 NSUserDefaults是一个单例类每个应用程序只有一个NSUserDefaults对象Settings Bundle设置的参数也可以通过NSUserDefaults来读取和设置 获取NSUserDefaults的方法 1 NSUserDefaults *defaults [NSUserDefaults standardUserDefaults]; 获取NSUserDefaults对象后可以获取和设置应用程序参数 xxForKey:(NSString *) key xx表示各种类型根据key获取值 setBool:(xxx) value forKey:(NSString *) key 设置参数 设置完参数后调用synchronize方法进行保存。 3、属性列表 属性列表就是文章开始提到的利用NSArray和NSDictionary将数据写入到文件的保存方法。 但是有一些限制只有下列类型的值保存到NSArray和NSDictionary中才可以直接调用writeToFile方法执行保存 NSArray、NSMutableArray、NSDictionary、NSMutableDictionary、NSData、NSMutableData、NSString、NSMutableString、NSValue、NSNumber 如果NSArray和NSDictionary保存了我们自定义的类那么将不能直接调用writeToFile方法执行保存。可以考虑使用对象归档的方法进行保存 1 //使用属性列表保存3个用户的账号密码2 accountList [[NSMutableArray alloc] init];3 [accountList addObject:[NSDictionary4 dictionaryWithObjects:[NSArray arrayWithObjects:loginname,loginpwd, nil]5 forKeys:[NSArray arrayWithObjects:305213781,123123, nil]]];6 [accountList addObject:[NSDictionary7 dictionaryWithObjects:[NSArray arrayWithObjects:loginname,loginpwd, nil]8 forKeys:[NSArray arrayWithObjects:475782389,123456, nil]]];9 [accountList addObject:[NSDictionary
10 dictionaryWithObjects:[NSArray arrayWithObjects:loginname,loginpwd, nil]
11 forKeys:[NSArray arrayWithObjects:330577588,123456789, nil]]];
12 [accountList writeToFile:[self filePath] atomically:YES];
13
14 //使用UIActionSheet提示用户保存成功
15 UIActionSheet *sheet [[UIActionSheet alloc] initWithTitle:保存成功 delegate:nil cancelButtonTitle:nil destructiveButtonTitle:确定 otherButtonTitles:nil, nil];
16 [sheet showInView:self.view]; 4、使用Sqlite3数据库 1)为项目增加libsqlite3.dylib,这是一个原生的C函数库 常用函数 int sqlite3_close(sqlite3 *):关闭sqlite3 所代表的数据连接并释放底层数据库连接资源。在调用该函数之前必须先调用sqlite3_finalize()函数调用sqlite3_blob_close()函数关闭所有的blob处理器否则将会返回SQLITE_BUSY int sqlite3_exec(sqlite3*,const char *sql, int (*callback)(void*, int ,char**,char**),void *,char **errmsg):用于执行没有返回值的sql语句 sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*):返回sqlite3代表的数据库最后一次插入行的id int sqlite3_changes(sqlite3*):执行某条dml语句后返回受影响行数 void sqlite3_interrupt(sqlite3*):中断一个长时间执行的查询语句 int sqlite3_complete(const char *sql):用于判断sql语句是否执行完成 int sqlite3_open(const char * filename,sqlite3 ** ppdb):打开与filename文件的链接并让ppdb参数引用被打开的数据库连接 const char *sqlite3_sql(sqlite3_stmt *pStmt):用于提取sqlite3_stmt(预编译SQL语句产生的结果)中包装的sql语句 等等。。。太多了不一一列举了 操作sqlite数据库的大致步骤如下 1、调用sqlite3_open方法打开与数据库的连接 2、执行语句 3、sqlite3_close函数关闭数据库连接 2使用Core Data框架 Core Data框架是一个纯粹的面向对象的框架允许开发者以面向对象的方式持久化操作SQLite数据库。core data底层的持久化存储方式可以是Sqlite数据库也可以是xml也可以是内存。 Core Data的核心概念是实体由Core Data管理的模型对象它必须是NSManagedObject类或者它的子类的实例。 Core Data应用中的核心API有如下几个 托管对象模型(NSManagedObjectModel)该对象负责管理整个应用的所有实体以及实体之间的关联关系。当开发者使用Xcode的图形界面设计了实体与实体间的关联关系之后需要使用该对象来加载、管理应用的托管对象模型 持久化存储协调器NSPersistentStoreCoordinator:负责管理底层的存储文件例如sqlite数据库等等 托管对象上下文NSManagedObjectContext:该对象是Core Data的核心增删改查都需要通过它来进行 实体描述NSEntityDescription:关于某个实体的描述信息 抓取请求(NSFetchRequest):该对象封装了查询实体的请求包括程序需要查询哪些实体、查询条件、排序规则等 使用Core Data持久化的步骤大致如下 1、创建NSManagedObjectModel对象来加载管理应用对应的托管对象模型 2、以NSManagedObjectModel对象为基础根据实际需要创建NSPersistentStoreCoordinator对象该对象确定底层数据的存储形式 3、以NSManagedObjectModel对象为基础创建NSManagedObjectContext对象 4、对于普通的增、删、改操作需要分别先新建实体、删除实体、修改实体然后调用NSManagedObjectContext对象的save方法保存修改 5、如果执行查询需要先创建NSFetchRequest对象在调用NSManagedObjectContext对象的executeFetchRequest:error:方法返回所有匹配条件的实体组成的NSArray 使用Core Data的例子来和大家一起学习。 首先创建一个创建一个Single View Application下面有个Use Core Data选项打上勾之后Xcode已经为你做好了准备工作打开AppDelete.m就可以看到 1 #pragma mark - Core Data stack2 3 synthesize managedObjectContext _managedObjectContext;4 synthesize managedObjectModel _managedObjectModel;5 synthesize persistentStoreCoordinator _persistentStoreCoordinator;6 7 - (NSURL *)applicationDocumentsDirectory {8 // The directory the application uses to store the Core Data store file. This code uses a directory named esitech.Unit_3 in the applications documents directory.9 return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
10 }
11
12 - (NSManagedObjectModel *)managedObjectModel {
13 // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
14 if (_managedObjectModel ! nil) {
15 return _managedObjectModel;
16 }
17 //获取实体模型文件对应的nsurl
18 NSURL *modelURL [[NSBundle mainBundle] URLForResource:Unit_3 withExtension:momd];
19 _managedObjectModel [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
20 return _managedObjectModel;
21 }
22
23 - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
24 // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
25 if (_persistentStoreCoordinator ! nil) {
26 return _persistentStoreCoordinator;
27 }
28
29 // Create the coordinator and store
30 //以持久化模型为基础创建持久化存储协调器对象
31 _persistentStoreCoordinator [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
32 //获取sqlite数据库文件的存储目录
33 NSURL *storeURL [[self applicationDocumentsDirectory] URLByAppendingPathComponent:Unit_3.sqlite];
34 NSError *error nil;
35 NSString *failureReason There was an error creating or loading the applications saved data.;
36 //设置存储协调器采用sqlite如果失败打印失败信息
37 if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:error]) {
38 // Report any error we got.
39 NSMutableDictionary *dict [NSMutableDictionary dictionary];
40 dict[NSLocalizedDescriptionKey] Failed to initialize the applications saved data;
41 dict[NSLocalizedFailureReasonErrorKey] failureReason;
42 dict[NSUnderlyingErrorKey] error;
43 error [NSError errorWithDomain:YOUR_ERROR_DOMAIN code:9999 userInfo:dict];
44 // Replace this with code to handle the error appropriately.
45 // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
46 NSLog(Unresolved error %, %, error, [error userInfo]);
47 abort();
48 }
49
50 return _persistentStoreCoordinator;
51 }
52
53
54 - (NSManagedObjectContext *)managedObjectContext {
55 // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
56 if (_managedObjectContext ! nil) {
57 return _managedObjectContext;
58 }
59
60 NSPersistentStoreCoordinator *coordinator [self persistentStoreCoordinator];
61 if (!coordinator) {
62 return nil;
63 }
64 _managedObjectContext [[NSManagedObjectContext alloc] init];
65 //设置上下文使用的持久化类型
66 [_managedObjectContext setPersistentStoreCoordinator:coordinator];
67 return _managedObjectContext;
68 }
69
70 #pragma mark - Core Data Saving support
71
72 - (void)saveContext {
73 NSManagedObjectContext *managedObjectContext self.managedObjectContext;
74 if (managedObjectContext ! nil) {
75 NSError *error nil;
76 if ([managedObjectContext hasChanges] ![managedObjectContext save:error]) {
77 // Replace this implementation with code to handle the error appropriately.
78 // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
79 NSLog(Unresolved error %, %, error, [error userInfo]);
80 abort();
81 }
82 }
83 } 三个重要的对象NSManagedObjectModel、NSPersistentStoreCoordinator、NSManagedObjectContext的实例已经为你设置好了当我们要执行相关增删改查时直接调用委托类的managedObjectContext属性操作即可。 在创建项目后会发现项目中有一个**.xcdatamodeld文件点击后就会出现实体模型设计界面 在刚刚的设计视图中添加一个类EventEntity添加两个属性》happenDateData、namestring 单击Xcode主菜单的Editor》Create NSManagedObject Subclass菜单来创建一个子类之后你的项目中就会创建一个类EventEntity : NSManagedObject 先完成一些准备工作获取appdelete对象定义一个nameCount变量记录添加了几个对象 1 #import SqliteViewController.h2 #import EventEntity.h3 #import Foundation/Foundation.h4 #import CoreData/CoreData.h5 #import AppDelegate.h6 7 interface SqliteViewController ()8 property (nonatomic,weak) AppDelegate *appDelete;9 property (nonatomic) int nameCount;
10 end
11
12 implementation SqliteViewController
13
14 - (void)viewDidLoad {
15 [super viewDidLoad];
16 self.nameCount 1;
17 // Do any additional setup after loading the view.
18 self.appDelete (AppDelegate *)[UIApplication sharedApplication].delegate;
19 }
20
21 - (void)didReceiveMemoryWarning {
22 [super didReceiveMemoryWarning];
23 // Dispose of any resources that can be recreated.
24 } 5.1 添加实体 第一步调用NSEntityDescription的 insertNewObjectForEntityForName:(NSString *) inManagedObjectContext:(NSManagedObjectContext *)方法第一个参数为实体名第二个参数为上下文对象 第二部为新实体设置属性 第三部调用NSManagedObjectContext对象的save方法保存。 1 //添加操作2 - (IBAction)btnAdd_Tap:(id)sender {3 //创建一个新实体4 EventEntity *ee [NSEntityDescription insertNewObjectForEntityForName:EventEntity inManagedObjectContext:self.appDelete.managedObjectContext];5 //给实体设置属性6 ee.happenDate [NSDate date];7 ee.name [NSString stringWithFormat:名字%d,self.nameCount];8 9 //定义NSError对象接收错误信息
10 NSError *error;
11 if ([self.appDelete.managedObjectContext save:error]) {
12 [[[UIAlertView alloc] initWithTitle:添加操作 message:[NSString stringWithFormat:添加成功,添加的实体的name是%,ee.name] delegate:nil cancelButtonTitle:好的 otherButtonTitles:nil, nil] show];
13 }else
14 {
15 NSLog(保存时出现错误%,%,error,[error userInfo]);
16 }
17 } 5.2 删除实体 1 //删除操作2 - (IBAction)btnRemove_Tap:(id)sender {3 //获取要删除的实体4 EventEntity *entity [self getEntityByName:名字1];5 //从上下文对象中删除该实体6 [self.appDelete.managedObjectContext deleteObject:entity];7 NSError *error;8 if (![self.appDelete.managedObjectContext save:error]) {9 NSLog(删除时出现错误%,%,error,[error userInfo]);
10 }
11 } 5.3 修改实体 1 //更新操作2 - (IBAction)btnUpdate_Tap:(id)sender {3 //获取要删除的实体4 EventEntity *entity [self getEntityByName:名字1];5 //修改实体属性6 entity.happenDate [NSDate date];7 NSError *error;8 if (![self.appDelete.managedObjectContext save:error]) {9 NSLog(删除时出现错误%,%,error,[error userInfo]);
10 }
11 } 5.4 查询 1 //根据名字查询一个对象2 -(EventEntity *)getEntityByName:(NSString *) name3 {4 //创建抓取数据的请求对象5 NSFetchRequest *request [[NSFetchRequest alloc] init];6 7 //设置要抓取哪种类型的实体8 NSEntityDescription *des [NSEntityDescription entityForName:EventEntity inManagedObjectContext:self.appDelete.managedObjectContext];9
10 //设置抓取实体
11 [request setEntity:des];
12 //定义抓取条件
13 NSPredicate * qcmd [NSPredicate predicateWithFormat:name % ,name];
14 [request setPredicate:qcmd];
15
16 NSError *error nil;
17 //执行查询请求
18 NSArray *result [[self.appDelete.managedObjectContext executeFetchRequest:request error:error] copy];
19 if (error!nil) {
20 NSLog(查询单个时出现错误%,%,error,[error userInfo]);
21 return nil;
22 }
23 if (result.count0) {
24 return nil;
25 }
26 return result[0];
27 }
28
29 //获得所有实体
30 -(NSArray *)getAllEntities
31 {
32 //创建抓取数据的请求对象
33 NSFetchRequest *request [[NSFetchRequest alloc] init];
34
35 //设置要抓取哪种类型的实体
36 NSEntityDescription *des [NSEntityDescription entityForName:EventEntity inManagedObjectContext:self.appDelete.managedObjectContext];
37
38 //设置抓取实体
39 [request setEntity:des];
40
41 NSError *error nil;
42 //执行查询请求
43 NSArray *result [[self.appDelete.managedObjectContext executeFetchRequest:request error:error] copy];
44 //如果没有数据返回nil
45 if (error!nil result 0) {
46 return nil;
47 }
48 return result;
49
50 } 转载于:https://www.cnblogs.com/chengzi/p/4478763.html