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