iOS开发笔记 7、数据【Preferences、文件、库、Core Data】

程序开发中根据要使用各种各样的数据,如配制、文件系统、数据库等,iOS对这个有很好的支持

Preferences

If you’re going to create a program that has built-in preferences, you should do so using the Utility Application template.

To create the special cartouched list used by preferences, you must create a table view controller with the special UITableViewGrouped style. You can do this by choosing the Grouped style for your table view in Interface Builder or by using the initWithStyle: method in Xcode.

信息的保存:

1、 保存到文件

2、 保存到数据库:Slqite的集成

3、 NSUserDefaults方式:NSUserDefaults is a persistent shared object that you can use to remember a user’s preferences from one session to another.

4、 system settings:

Xcode allows you to tie multiple files together into a coherent whole called a bundle.

In practice, a bundle is just a directory. Often a bundle is made opaque, so that users can’t casually see its contents; in this case, it’s called a package.The main advantage of a bundle is that it can invisibly store multiple variants of a file, using the right one when the circumstances are appropriate. For example, an application bundle can include executable files for different chip architectures or in different formats.

framework bundles, application bundles, and settings bundles

NSBundleCFBundle类可以发现更多的Bundle的信息

iPhone and iPad in Action例子:

Selfpreferences

Systempreferences

 

文件

软件部署后的目录:

~/Library/Application Support/iPhone Simulator/Users/Applications

这个目录下有应用程序的目录,包括:*.app, Documents,Library,tmp目录

把文件拖到Xcode中,默认作为Application Bundle的资源

当前程序的路径处理

NSString *paths = [[NSBundlemainBundle] resourcePath];

NSString *bundlePath = [paths stringByAppendingPathComponent:dbFile];

其他目录的路径处理

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *docPath = [documentsDirectorystringByAppendingPathComponent:dbFile];

文件处理:

NSFileManager *fileManager = [NSFileManagerdefaultManager];

success = [fileManagercopyItemAtPath:bundlePathtoPath:docPatherror:&error];

iPhone and iPad in Action例子:

filesaver

 

Sqlite

First add the framework, which you can find under /usr/lib/libsqlite3.0.dylib,rather than in the standard framework directory.

Second, you must add an import of sqlite3.h

iPhone and iPad in Action例子:

dbnav

 

Address Book

the Address Book framework and the Address Book UI framework

头文件

AddressBook/AddressBook.h and AddressBookUI/AddressBookUI.h

iPhone and iPad in Action例子:

Contactsearch

Contactselect

 

Core Data[IOS 3 以上]

ore Data is a powerful layer that sits on top of an SQLite database. It removes much of the complexities of SQL and allows you to interface with the database in a more natural way. It does this by making the database rows into real Objective-C objects (called managed objects) and lets you manipulate them without any knowledge of SQL.

MANAGED OBJECT

A managed object is a representation of an object you want to store in a database. Think of it as a record in SQL. It generally contains fields that match up with the properties of an object being saved in your application. After you create a managed object, you must insert it into a managed object context before you can save it to the data store.

MANAGED OBJECT CONTEXT

The managed object context holds all of your managed objects until they’re ready to be committed to the database. Inside this context, managed objects can be added, modified, and deleted. This is like a buffer between your application and the database.

MANAGED OBJECT TABLE

This object describes the schema of your database. It’s used when interfacing the managed object context with the database. A managed object table contains a collection of entity descriptions. Each of these entities describes a table in your database and is used when mapping managed objects to database entries.

Xcode中建立方法

File > New File. Then, select Data Model under Resource

这个和O/R Mapping 工具类似,概念和术语和Ado.net Entity有共同之处

iPhone and iPad in Action例子:

CDJournal

时间: 2011-01-27

iOS开发笔记 7、数据【Preferences、文件、库、Core Data】的相关文章

PHP移动互联网开发笔记(5)——文件的上传下载

一.文件的上传 1.客户端设置: (1).在 标签中将enctype和method两个属性指明相应的值. Enctype="multipart/form-data"; Method="POST" (2).form表单中设置一个hidden类型的input框,其中name的值为MAX_FILE_SIZE的隐藏值 2.服务器端设置: (1).$_FILES多维数组:用于存储各种上传文件有关的信息 (2).文件上传与php配置文件的设置,如以下php.ini文件中的一些指

iOS开发ASIHttpRequest发送数据与下载数据

  发送数据 本文为大家介绍了iOS开发ASIHttpRequest发送数据的内容,其中包括设定request头,使用ASIFormDataRequest POST表单,PUT请求.自定义POST请求等等内容. 设定request头 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request addRequestHeader:@"Referer" value:@"http://www.drea

【Swift】iOS开发笔记(一)

1.隐藏/显示密码功能 光设置secureTextEntry还不行,你会发现UITextField在切换到显示密码时会多一个空字符,看着巨别扭,需要在更改secureTextEntry后进行如下设置:         let pwd = psdField.text         self.psdField.text = pwd + " "         self.psdField.text = pwd 2.获取当前类的名称 String.fromCString(object_get

iOS开发网络篇—数据缓存

iOS开发网络篇-数据缓存 一.关于同一个URL的多次请求 有时候,对同一个URL请求多次,返回的数据可能都是一样的,比如服务器上的某张图片,无论下载多少次,返回的数据都是一样的. 上面的情况会造成以下问题 (1)用户流量的浪费 (2)程序响应速度不够快 解决上面的问题,一般考虑对数据进行缓存. 二.缓存 为了提高程序的响应速度,可以考虑使用缓存(内存缓存\硬盘缓存) 第一次请求数据时,内存缓存中没有数据,硬盘缓存中没有数据. 缓存数据的过程 当服务器返回数据时,需要做以下步骤 (1)使用服务器

IOS开发笔记 IOS如何访问通讯录

    IOS开发笔记  IOS如何访问通讯录 其实我是反对这类的需求,你说你读我的隐私,我肯定不愿意的. 幸好ios6.0 以后给了个权限控制.当打开app的时候你可以选择拒绝. 实现方法: [plain] view plaincopy //读取所有联系人      -(void)ReadAllPeoples      {              //取得本地通信录名柄              ABAddressBookRef tmpAddressBook = nil;         

【Swift 2.2】iOS开发笔记(三)

1.UITableView 中调用 UIButton 的 setTitle 会闪 滚动列表时比较明显,解决办法: buttonType 改成 custom 即可,但是这样一来 UIButton 的高亮效果也没了,但可以自己手动配置 State Config  2.监听 UITextField 文本改变 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementSt

IOS开发笔记

1 iphone开发笔记 2 3 退回输入键盘 4 - (BOOL) textFieldShouldReturn:(id)textField{ 5 [textField resignFirstResponder]; 6 } 7 8 CGRect 9 CGRect frame = CGRectMake (origin.x, origin.y, size.width, size.height):矩形 10 NSStringFromCGRect(someCG) 把CGRect结构转变为格式化字符串:

IOS开发:FMDB数据存储解析

  FMDB是面向对象的,它以OC的方式封装了SQLite的C语言API,使用起来更加的方便,不需要过多的关心数据库操作的知识.但是它本身也存在一些问题,比如跨平台,因为它是用oc的语言封装的,所以只能在ios开发的时候使用,如果想实现跨平台的操作,来降低开发的成本和维护的成本,就需要使用比较原始的SQLite. FMDB是用于进行数据存储的第三方的框架,它与SQLite与Core Data相比较,存在很多优势. Core Data是ORM的一种体现,使用Core Data需要用到模型数据的转化

IOS开发:CocoaPods一个Objective-C第三方库的管理利器

  1.CocoaPods是跑在Ruby的软件,安装可能需要几分钟,安装命名: sudo gem install cocoapods 2.如果想为每个第三方库生成文档,那运行 brew install appledoc 这步是可选的. 安装完成后提示: 开发:CocoaPods一个Objective-C第三方库的管理利器-"> 如果你的Ruby环境不够新的话,可能要更新一下: gem update --system 现在pod安装好了,怎么使用呢? 搜索 先试试搜索功能,你需要什么库,可以