iOS基礎 CoreData 總結

2021-10-05 11:35:35 字數 2038 閱讀 6822

coredata 資料持久化框架是cocoa api的一部分,它允許按照 實體-屬性-值 模型組織資料,並以xml(做mac-os用), 二進位制, 或者sqlite資料檔案的格式持久化資料. coredata主要提供 物件-關係對映(orm)功能,把oc物件轉化為資料儲存到檔案,也可以資料轉化成oc物件.

coredata與sqlite區別 :

coredata是官方推出的資料持久化框架,sqlite是蘋果使用別人開發好的乙個動態庫,本質是關係型資料庫.

coredata是ios平台下的乙個資料持久化的方式,不可以跨平台使用,sqlite可以跨平台使用.

核心物件:

nspersistentstorecoordinator  資料聯結器類(中間人,不能直接控制)

nsmanagedobjectcontext       資料管理器類  (臨時資料庫 )

nsmanagedobject                   資料管理類

nsmanagedobject                   資料模型器類

nsentitydescription                  實體描述類

操作過程: context想要獲取值,先要告訴聯結器,我要什麼東西,聯結器再告訴store,你給我什麼東西,store去找,找到之後返回給context.

資料庫的簡單操作

.xcdatamodeld裡

add entity 新增實體類

修改實體類名

修改屬性名與型別

建立檔案  command+n —> coredata —> nsmanageobjuect subclass

viewcontroller.m 裡

匯入框架及各種標頭檔案

//coredata的框架

//匯入框架的方式: 《框架名字/標頭檔案名字》

#import

#import"person.h"

@inte***ceviewcontroller()

@property(nonatomic,strong)uitableview*tableview;

//臨時資料庫

@property(nonatomic,strong)nsmanagedobjectcontext*objectcontext;

//建立乙個可變陣列,配合table設定cell的相關設定

@property(nonatomic,strong)nsmutablearray*dataarray;

@end

@implementationviewcontroller

- (void)viewdidload elseelse{

//tableview必須實現的方法

-(nsinteger)tableview:(uitableview*)tableview numberofrowsinsection:(nsinteger)section{

returnself.dataarray.count;

-(uitableviewcell*)tableview:(uitableview*)tableview cellforrowatindexpath:(nsindexpath*)indexpath{

uitableviewcell*cell = [tableviewdequeuereusablecellwithidentifier:@"cell"forindexpath:indexpath];

//建立例項接收

Core Data基礎概念總結

core data涉及到的幾個主要的概念可以對應資料庫來理解 nsmanagedobjectcontext 託管物件上下文 資料庫 nsentitydescription 實體描述 表 nsfetchrequest 請求 命令集 nspredicate 謂詞 查詢語句 在書中給出的例子中的一些語句可...

IOS資料儲存之 Core Data

前言 構成 1 nsmanagedobjectcontext 被管理的資料上下文 操作實際內容 操作持久層 作用 插入資料,查詢資料,刪除資料 2 nsmanagedobjectmodel 被管理的資料模型 資料庫所有 或資料結構,包含各實體的定義資訊 作用 新增實體的屬性,建立屬性之間的關係 3 ...

IOS基礎 總結

一 在.m檔案中宣告的方法都是私有方法,外界是無法訪問的,這就保證了程式的封裝性 二 uiview常見屬性 property nonatomic,readonly uiview superview 獲得自己的父控制項物件 property nonatomic,readonly,copy nsarra...