• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift3.0coredata的使用

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

//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

 
 

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap