gpt4 book ai didi

ios - Swift 和 CoreData/数据存储

转载 作者:IT王子 更新时间:2023-10-29 05:22:28 25 4
gpt4 key购买 nike

我正在学习 Objective-C/Cocoa Touch 和 Core Data。那么,在纯 Swift 编写的 iOS App 项目中实现数据存储有哪些新的可能性呢?我真的很喜欢这种语言,但据我所知,所有核心数据方法都是用 Objective-C 编写的。那么核心数据类/方法会自动转换为 Swift-Code 还是我们必须将核心数据的 Objective-C 代码和其他所有内容的 Swift-Code 混合起来?

最佳答案

这就是我实现核​​心数据的方式。

几个非常重要的注意事项:

  • 您必须将其添加到您的 NSManagedObject 类中:

    @objc(我的对象)

  • 您必须将实体名称添加到 .xcdatamodel 中的默认配置类(包括图片)

xcdatamodel

  • 您不能简单地创建一个 NSManagedObject。

    var myObject : MyObject = MyObject()

你必须这样做:

let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context: NSManagedObjectContext = appDelegate.managedObjectContext
let entityName: String = "MyObject"
let myEntityDescription = NSEntityDescription.entityForName(entityName, inManagedObjectContext: context)
var myObject = MyObject(entity: myEntityDescription, insertIntoManagedObjectContext: context)

这是我的 NSManagedObject。我包括了两个获取方法以及一个用于对象构造的类方法。您可能会注意到我正在利用新的枚举系统,以便我可以轻松访问我的实体名称和实体属性

import UIKit
import CoreData

enum MyObjectPropertyList {
case name
func description() -> String {
switch self {
case .name:
return "name"
}
}
}

@objc(MyObject)
class MyObject: NSManagedObject {

@NSManaged var name: String

//
//// CREATE CLASS OBJECT
//

class func createMyObject (propertyName:MyObjectPropertyList, value:String, context: NSManagedObjectContext) -> MyObject? {
if !value.isEmpty {
let propertyType = propertyName.description()

let entityName = "MyObject"
let request : NSFetchRequest = NSFetchRequest(entityName: entityName)
request.returnsObjectsAsFaults = false
request.predicate = NSPredicate(format: "\(propertyType) = %@", value)
var error: NSError? = nil
var matches: NSArray = context.executeFetchRequest(request, error: &error)

if (matches.count > 1) {
// handle error
return matches[0] as? MyObject
} else if matches.count == 0 {
let entityDescription = NSEntityDescription.entityForName(entityName, inManagedObjectContext: context)
var myObject : MyObject = MyObject(entity: entityDescription, insertIntoManagedObjectContext: context)
myObject.name = value
return myObject
}
else {
println(matches[0])
return matches[0] as? MyObject
}
}
return nil
}
}

//
//// FETCH REQUESTS
//

func myGeneralFetchRequest (entity : CoreDataEntities,
property : MyObjectPropertyList,
context : NSManagedObjectContext) -> AnyObject[]?{

let entityName = entity.description()
let propertyName = property.description()

let request :NSFetchRequest = NSFetchRequest(entityName: entityName)
request.returnsObjectsAsFaults = false
let sortDescriptor : NSSortDescriptor = NSSortDescriptor(key: propertyName, ascending: true)
request.sortDescriptors = [sortDescriptor]
var error: NSError? = nil
var matches: NSArray = context.executeFetchRequest(request, error: &error)

if matches.count > 0 {
return matches
}
else {
return nil
}
}

func myNameFetchRequest (entity : CoreDataEntities,
property : MyObjectPropertyList,
value : String,
context : NSManagedObjectContext) -> AnyObject[]? {

let entityName = entity.description()
let propertyName = property.description()

let request :NSFetchRequest = NSFetchRequest(entityName: entityName)
request.returnsObjectsAsFaults = false
request.predicate = NSPredicate(format: "\(propertyName) = %@", value)
let sortDescriptor :NSSortDescriptor = NSSortDescriptor(key: propertyName, ascending: true)
request.sortDescriptors = [sortDescriptor]
var error: NSError? = nil
var matches: NSArray = context.executeFetchRequest(request, error: &error)

if matches.count > 0 {
return matches
}
else {
return nil
}
}

//
//// PRINT FETCH REQUEST
//

func printFetchedArrayList (myarray:AnyObject[]) {
if myarray.count > 0 {
println("Has \(myarray.count) object")
for myobject : AnyObject in myarray {
var anObject = myobject as MyObject
var thename = anObject.name
println(thename)
}
}
else {
println("empty fetch")
}
}

这是我的 View Controller

import UIKit
import CoreData

enum CoreDataEntities {
case MyObject
func description() -> String {
switch self {
case .MyObject:
return "MyObject"
}
}
}

class ViewController: UIViewController {

//
//// MOC
//

var managedObjectContext : NSManagedObjectContext = NSManagedObjectContext()

//
//// Text Field
//

@IBOutlet var myTextField : UITextField

//
//// BUTTONS
//

@IBAction func saveButtonPress(sender : UIButton) {
makeEntityAction()
}

@IBAction func fetchButtonPress(sender : UIButton) {
fetchObjectAction()
}

//
//// ACTIONS
//

func makeEntityAction () {
println("-- Make action --")

let value:String = self.myTextField.text
var myObject : MyObject = MyObject.createMyObject(MyObjectPropertyList.name, value : value, context: self.managedObjectContext)!
saveContext(self.managedObjectContext)
}

func fetchObjectAction () {
println("-- Fetch action --")

if let myTotalarray = myGeneralFetchRequest(CoreDataEntities.MyObject, MyObjectPropertyList.name, self.managedObjectContext) {
printFetchedArrayList(myTotalarray)
}
if let mySinglearray: AnyObject[] = myNameFetchRequest(CoreDataEntities.MyObject, MyObjectPropertyList.name, "Bill", self.managedObjectContext) {
println("(-- --)")
printFetchedArrayList(mySinglearray)
}

}

//
//// LOAD & SAVE
//

func loadContext () {
let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context: NSManagedObjectContext = appDelegate.managedObjectContext
self.managedObjectContext = context
}

func saveContext (context: NSManagedObjectContext) {
var error: NSError? = nil
context.save(&error)
}

//
//// LOAD
//

func myLoad () {
loadContext ()
println("Loaded Context")
}

//
//// Life Cycle
//

override func viewDidLoad() {
super.viewDidLoad()
myLoad ()
}

}

关于ios - Swift 和 CoreData/数据存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24005342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com