CoreData Swift:如何保存和加载数据?

时间:2014-08-30 20:43:26

标签: ios swift iphone core-data

我正在Swift中编写iOS游戏,我想在最后添加一个高分标签。我认为保存功能是正确的,但负载一是给我带来问题的。我已经创建了一个实体(" BestScores")和属性(" classicBestScoreTF"):

保存高分:

var bestscore25 = 1000
var score: int

func savescore() {    
    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
    var context:NSManagedObjectContext = appDel.managedObjectContext!
    var score25: AnyObject! = NSEntityDescription.insertNewObjectForEntityForName("BestScores", inManagedObjectContext: context) as NSManagedObject
    score25.setValue(score, forKey: "classicBestScoreTF")
    context.save(nil)
}

func loadscore() {
    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
    var context:NSManagedObjectContext = appDel.managedObjectContext!
    bestScore25 = valueForKey("classicBestScoreTF") as Int
}

func endgame() {

    if score > bestScore25 {
        savescore()
        loadscore()
        bestScoreLabel.text = "Best Score: \(bestScore25)"
    }

    if score < bestscore {            
        loadscore()
        bestScoreLabel.text = "Best Score: \(bestScore25)"
    }    
}

它无法正常工作:(请帮忙!

6 个答案:

答案 0 :(得分:25)

保存数据:

var person = NSEntityDescription.insertNewObjectForEntityForName("Person", 
inManagedObjectContext: self.managedObjectContext!) as Person
person.name = "Mary"
person.age = Float(arc4random() % 100)

var error : NSError? = nil
if !self.managedObjectContext!.save(&error) {
    NSLog("Unresolved error \(error), \(error!.userInfo)")
    abort()
}

正在加载数据:

var error: NSError? = nil
var fReq: NSFetchRequest = NSFetchRequest(entityName: "Frases")    
fReq.predicate = NSPredicate(format: "id contains[c] %@", String(day))
var sorter: NSSortDescriptor = NSSortDescriptor(key: "id" , ascending: false)
fReq.sortDescriptors = [sorter]
fReq.returnsObjectsAsFaults = false
let result : [AnyObject] = self.managedObjectContext!.executeFetchRequest(fReq, error:&error)!

答案 1 :(得分:3)

要保存数据,您应该使用:

context.insertNewObjectForEntityForName(...)

在加载数据时也一样,它应该是:

context.valueForKey(...)

但是,我不确定这是否是正确的语法,因为您可能需要获取实体数组,然后在索引0处获取对象。那么为什么您使用核心数据获得高分?您可能希望查看用户默认值或icloud,核心数据是为数据库设计的更多

答案 2 :(得分:3)

  

更新了swift:

在简单代码下面用于加载,插入,删除日期;

import UIKit
import CoreData

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate{

    // MARK:- Variable Declarations

    @IBOutlet weak var mTableView: UITableView!
    var manageObjectContext: NSManagedObjectContext!
    var eventArray = [Event]()

    // MARK:- ViewController LifeCycle Methods

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Event Reminder"
        manageObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        self.loadSaveData()
    }

    override func viewWillAppear(_ animated: Bool) {
        // Remove empty cell from tableview
        mTableView.tableFooterView = UIView(frame: .zero)
    }

    // MARK:- TableView DataSource and Delegate Methods

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return eventArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "ViewControllerTableViewCell")! as! ViewControllerTableViewCell
        cell.layer.cornerRadius = 05

        let eventArrayItem = eventArray[indexPath.row]

        if let eventImage = UIImage(data: (eventArrayItem.event_image! as Data)){
            cell.mImageView.image = eventImage
        }

        cell.mEventHeadingLabel.text = eventArrayItem.event_heading
        cell.mShortDescriptionLabel.text = eventArrayItem.event_description

        return cell
    }

    // To delete Particular cell/row from tableview
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

        let eventArrayItem = eventArray[indexPath.row]

        if editingStyle == .delete {
            manageObjectContext.delete(eventArrayItem)

            do {
                try manageObjectContext.save()
            } catch let error as NSError {
                print("Error While Deleting Note: \(error.userInfo)")
            }
        }
        self.loadSaveData()
    }

    // MARK:- IBAction Methods

    @IBAction func actionOnPlusButton(_ sender: Any) {
        let imagePicker = UIImagePickerController()
        imagePicker.sourceType = .photoLibrary
        imagePicker.delegate = self
        self.present(imagePicker, animated: true, completion: nil)
    }

    // MARK:- ImagePicker Delegate Methods

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        if let mImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
            picker.dismiss(animated: true, completion: { 
                self.createEventItemWith(selectedImage: mImage)
            })
        }
    }

    // MARK:- Instance Methods

    func createEventItemWith(selectedImage: UIImage)  {

        let eventItem = Event(context: manageObjectContext)

        let alertController = UIAlertController(title: "Add Event", message: "Enter event and it's description", preferredStyle: .alert)

        let saveAction = UIAlertAction(title: "Save", style: .default, handler: {
            alert -> Void in

            let eventNameTextField = alertController.textFields![0] as UITextField
            let descriptionTextField = alertController.textFields![1] as UITextField

            print("firstName \(String(describing: eventNameTextField.text)), secondName \(String(describing: descriptionTextField.text))")

            if eventNameTextField.text != "" || descriptionTextField.text != ""{

                eventItem.event_heading = eventNameTextField.text
                eventItem.event_description = descriptionTextField.text
                eventItem.event_image = NSData(data: UIImageJPEGRepresentation(selectedImage, 0.3)!)

                do{
                    try self.manageObjectContext.save()
                    self.loadSaveData()
                }catch{
                    print("Could not save data: \(error.localizedDescription)")
                }

            }else{
                self.showAlertMessageToUser(title: "Alert", messageToUser: "Fields should not be empty, Please enter given info...")
            }
        })

        let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: {
            (action : UIAlertAction!) -> Void in
                self.manageObjectContext.reset()
        })

        alertController.addTextField { (textField : UITextField!) -> Void in
            textField.placeholder = "Enter event Name"
        }
        alertController.addTextField { (textField : UITextField!) -> Void in
            textField.placeholder = "Enter event description in short"
        }

        alertController.addAction(saveAction)
        alertController.addAction(cancelAction)

        self.present(alertController, animated: true, completion: nil)
    }

    func showAlertMessageToUser(title: String, messageToUser: String)  {
        let alert = UIAlertController(title: title, message: messageToUser, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }

    func loadSaveData()  {
        let eventRequest: NSFetchRequest<Event> = Event.fetchRequest()
        do{
            eventArray = try manageObjectContext.fetch(eventRequest)
            self.mTableView.reloadData()
        }catch
        {
            print("Could not load save data: \(error.localizedDescription)")
        }
    }
}

enter image description here

答案 3 :(得分:0)

快速5

第1步:使用CoreData选项创建简单应用

enter image description here

步骤2。打开 .xcdatamodeld 文件,并以此方式添加实体属性

enter image description here

步骤3。您的AppDelegate应该具有 Core Data Stack方法

第4步。确保按以下步骤进行快速代码侦听

import UIKit
import CoreData

class ViewController: UIViewController {

    // MARK: Variables declearations
    let appDelegate = UIApplication.shared.delegate as! AppDelegate //Singlton instance
    var context:NSManagedObjectContext!

    // MARK: View Controller life cycle methods
    override func viewDidLoad() {
        super.viewDidLoad()

        openDatabse()
    }

    // MARK: Methods to Open, Store and Fetch data
    func openDatabse()
    {
        context = appDelegate.persistentContainer.viewContext
        let entity = NSEntityDescription.entity(forEntityName: "Users", in: context)
        let newUser = NSManagedObject(entity: entity!, insertInto: context)
        saveData(UserDBObj:newUser)
    }

    func saveData(UserDBObj:NSManagedObject)
    {
        UserDBObj.setValue("RDC", forKey: "username")
        UserDBObj.setValue("1234", forKey: "password")
        UserDBObj.setValue("21", forKey: "age")

        print("Storing Data..")
        do {
            try context.save()
        } catch {
            print("Storing data Failed")
        }

        fetchData()
    }

    func fetchData()
    {
        print("Fetching Data..")
        let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
        request.returnsObjectsAsFaults = false
        do {
            let result = try context.fetch(request)
            for data in result as! [NSManagedObject] {
                let userName = data.value(forKey: "username") as! String
                let age = data.value(forKey: "age") as! String
                print("User Name is : "+userName+" and Age is : "+age)
            }
        } catch {
            print("Fetching data Failed")
        }
    }
}

第5步。在设备上运行并查看日志以获取核心数据结果

答案 4 :(得分:0)

导入UIKit 导入CoreData

ViewController类:UIViewController {

let nameTxt: UITextField = {

    let td = UITextField()
    td.translatesAutoresizingMaskIntoConstraints = false
    td.placeholder = "Enter Id"
    td.borderStyle = .line
    td.layer.borderWidth = 1
    td.layer.borderColor = UIColor.lightGray.cgColor
    return td
}()

let doneBt: UIButton = {

    let bt = UIButton()
    bt.translatesAutoresizingMaskIntoConstraints = false
    bt.setTitle("DONE", for: .normal)
    bt.setTitleColor(.white, for: .normal)
    bt.addTarget(self, action: #selector(handleDoneBt), for: .touchUpInside)
    bt.backgroundColor = UIColor.blue
    return bt
}()

@objc func handleDoneBt() {

     saveData()
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    view.backgroundColor = .white

    setupView()
    setupLayout()

}


func saveData() {

    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {return}

    let manageContent = appDelegate.persistentContainer.viewContext

    let userEntity = NSEntityDescription.entity(forEntityName: "Users", in: manageContent)!

    let users = NSManagedObject(entity: userEntity, insertInto: manageContent)

    users.setValue(1, forKeyPath: "id")
    users.setValue(nameTxt.text, forKeyPath: "name")
    users.setValue(123, forKeyPath: "mobile")


    do{
        try manageContent.save()
    }catch let error as NSError {

        print("could not save . \(error), \(error.userInfo)")
    }

    fetchData()
}

func fetchData() {

    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {return}
     let manageContent = appDelegate.persistentContainer.viewContext
     let fetchData = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")

    do {

        let result = try manageContent.fetch(fetchData)

        for data in result as! [NSManagedObject]{

             print(data.value(forKeyPath: "id") as Any)
             print(data.value(forKeyPath: "name") as Any)
        }
    }catch {
        print("err")
    }
}

func setupView() {

    view.addSubview(nameTxt)
    view.addSubview(doneBt)
}
func setupLayout(){

    NSLayoutConstraint.activate([

            nameTxt.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            nameTxt.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            nameTxt.widthAnchor.constraint(equalToConstant: 160),
            nameTxt.heightAnchor.constraint(equalToConstant: 30),

            doneBt.topAnchor.constraint(equalTo: nameTxt.bottomAnchor,constant: 6),
            doneBt.leftAnchor.constraint(equalTo: nameTxt.leftAnchor,constant: 8),
            doneBt.widthAnchor.constraint(equalToConstant: 120),
            doneBt.heightAnchor.constraint(equalToConstant: 36)
        ])
}

}

在TABLEVIEW中显示

func fetchData(){

    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {return}
    let manageContent = appDelegate.persistentContainer.viewContext
    let fetchData = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")        
    do {

        let result = try manageContent.fetch(fetchData)
        allData = result as! [NSManagedObject]
        tbl.reloadData()
    }catch {
        print("err")
    }
}

var allData = [NSObject]()

func tableView(_ tableView:UITableView,numberOfRowsInSection部分:Int)-> Int {         返回allData.count     }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! tblCell
    cell.lbl.text = allData[indexPath.row].value(forKey: "name") as? String
    return cell
}

func tableView(_ tableView:UITableView,提交editingStyle:UITableViewCell.EditingStyle,forRowAt indexPath:IndexPath){

    if editingStyle == .delete {
        let task = allData[indexPath.row]
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {return}

        let manageContent = appDelegate.persistentContainer.viewContext

        manageContent.delete(task as! NSManagedObject)
        (UIApplication.shared.delegate as! AppDelegate).saveContext()

        do {
            try manageContent.save()
        } catch {
            print("Fetching Failed")
        }
    }
    fetchData()
}

答案 5 :(得分:0)

CoreData 操作

import CoreData


public class ItemCoreDataStorage {
    
    let context: NSManagedObjectContext
    
    init(context: NSManagedObjectContext) {
        self.context = context
    }
    
    func save(_ items: [Item]) {
        
        cashbackItems.forEach({ item in
            let moItem = MOCashback(context: context)
            moItem.a = item.a
            moItem.b = item.b
        })
        
        do {
            try context.save()
        } catch {
            //Error handling
        }
        
    }
    
    func fetch() -> [MOItem] {
        do {
            let moItems: [MOItem] = try context.fetch(MOItem.fetchRequest())
            return moItems
        } catch {
            //Error handling
        }
    }
    
    func fetchedResultsController() -> NSFetchedResultsController<MOItem> {
        
        let fetchRequest: NSFetchRequest< MOItem > = MOCashback.fetchRequest()
        fetchRequest.sortDescriptors = [NSSortDescriptor(key: "a", ascending: true)]
        let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
        
        return frc
    }
    
    func delete() {

        self.fetch()
            .forEach({ context.delete($0)})
        do {
            try context.save()
        }
        catch {
            //Error handling
        }

    }
}
  • 请注意,例如您可以使用 bundle 来删除项目,但不会由 FRC 管理。

  • 还有 fetchRequest.sortDescriptors 是强制性的