swift3.0 coredata 的使用

//swift3.0在语法上有很大的改变,以简单的增删改查为例,如下:

//User类如下:

import Foundation

import CoreData

extension User {

@nonobjc public class func fetchRequest() -> NSFetchRequest<User> {

return NSFetchRequest<User>(entityName: "User");

}

@NSManaged public var address: String?

@NSManaged public var email: String?

@NSManaged public var headImg: NSData?

@NSManaged public var isLeave: Bool

@NSManaged public var name: String?

@NSManaged public var tel: String?

@NSManaged public var birthDay: String?

}

//CoreDataTools 类如下:该类用于操作coredata

import UIKit

class CoreDataTools: NSObject {

}

///CoreData操作

let EntityName = "User"

//增

func addCoreData()

{

let entity =
NSEntityDescription.insertNewObject(forEntityName:
EntityName, into:
appDelegate.managedObjectContext)
as!
User

entity.name = "周杰伦"

entity.tel ="123456789952"

do

{

try
appDelegate.managedObjectContext.save()

print("添加成功 ~ ~ ")

}catch

{

print("添加失败!!")

}

}

//删

func deleteCoreData(ConditionDic conditionDic:NSMutableDictionary)

{

let request:NSFetchRequest<NSFetchRequestResult>
= NSFetchRequest(entityName:
EntityName)

let condition = "name='周杰伦'"

let predicate =
NSPredicate(format: condition,
"")

request.predicate = predicate

do{

//查询满足条件的联系人

let resultsList =
try appDelegate.managedObjectContext.fetch(request)
as! [User]
as NSArray

if resultsList.count !=
0 {//若结果为多条,则只删除第一条,可根据你的需要做改动

appDelegate.managedObjectContext.delete(resultsList[0]
as! NSManagedObject)

try
appDelegate.managedObjectContext.save()

print("delete success ~ ~")

}else{

print("删除失败!
没有符合条件的联系人!")

}

}catch{

print("delete fail !")

}

}

//改

func updateDataWithCoreData(Model userModel:UserModel, Where condiArray:NSArray)

{

let request:NSFetchRequest<NSFetchRequestResult>
= NSFetchRequest(entityName:
EntityName)

let entity:NSEntityDescription =
NSEntityDescription.entity(forEntityName:
EntityName, in: appDelegate.managedObjectContext)!

let condition =
"name='周杰伦'"

let predicate =
NSPredicate(format: condition,"")

request.entity = entity

request.predicate = predicate

do{

let userList =
try appDelegate.managedObjectContext.fetch(request)
as! [User]
as NSArray

if userList.count !=
0 {

let user = userList[0]
as! User

user.name = "小公举"

try
appDelegate.managedObjectContext.save()

print("修改成功 ~
~")

}else{

print("修改失败,没有符合条件的联系人!")

}

}catch{

print("修改失败 ~ ~")

}

}

//查

func selectDataFromCoreData() ->
NSArray

{

var dataSource =
NSArray()

let request :
NSFetchRequest<NSFetchRequestResult> =
NSFetchRequest()

let entity:NSEntityDescription? =
NSEntityDescription.entity(forEntityName:
EntityName, in: appDelegate.managedObjectContext)

request.entity = entity

do{

dataSource = try
appDelegate.managedObjectContext.fetch(request)
as! [User]
as NSArray

print("数据读取成功 ~ ~")

}catch{

print("get_coredata_fail!")

}

return
dataSource

}

//查询所有数据并输出

func printAllDataWithCoreData()

{

let array =
getDataFromCoreData()

for item
in array {

let user = item
as! User

print("name=",user.name,"tel=",user.tel,"email=",user.email,"address=",user.address)

}

}

//注:appDelegate.managedObjectContext
为定义的全局变量,在Appdelegateclass范围外定义如下:

//let application = UIApplication.shared

//let appDelegate = application.delegate as! AppDelegate

 
 

版权声明:本文为博主原创文章,未经博主允许不得转载。

上一篇:Swift入门篇-结构体


下一篇:Unity NGUI实现Tabview