如何在MVC中使用coredata

时间:2015-05-29 17:50:09

标签: swift core-data

我可以在函数中使用Core Data但我无法在MVC中创建,如下所示:

class addBD{
    func add(){

        let entityDescripition = NSEntityDescription.entityForName("Radar",inManagedObjectContext: managedObjectContext!)
        let task = Radar(entity: entityDescripition!, insertIntoManagedObjectContext: managedObjectContext)
        task.descricao = vlrDesc.text
        task.latitude = fieldLatitude.text
        task.longitude = fieldLongitude.text;
        task.velocidade = picker.selectedRowInComponent(0)
        managedObjectContext?.save(nil)

    }
}

import UIKit
import CoreData

class adicionarRadar: UIViewController, NSFetchedResultsControllerDelegate {

func createTask() {

    adic.add()
}

我有这个错误:

  

使用未解析的标识符'managedObjectContext'

     

使用未解析的标识符'vlrDesc'

......所有变量

class addBD{
    func add(){

        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext!

        let entityDescripition = NSEntityDescription.entityForName("Radar",inManagedObjectContext: managedContext)
        let task = Radar(entity: entityDescripition!, insertIntoManagedObjectContext: managedContext)
        task.descricao = vlrDesc.text
        task.latitude = fieldLatitude.text
        task.longitude = fieldLongitude.text;
        task.velocidade = picker.selectedRowInComponent(0)
        managedContext.save(nil)

    }
}

import UIKit
import CoreData

class adicionarRadar: UIViewController, NSFetchedResultsControllerDelegate {

    var velocidade = ["40","50","70","90","100","120"]

    var latitude:String = ""
    var longitude:String = ""
    var descrip:String = ""
    var velocid:NSNumber = 0.0
    var task: Radar? = nil
    var idPicker:Int = 0
    let adic = adiciona()

    let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

    @IBOutlet weak var fieldLatitude: UITextField!
    @IBOutlet weak var fieldLongitude: UITextField!
    @IBOutlet weak var picker: UIPickerView!
    @IBOutlet weak var vlrDesc: UITextField!


    @IBAction func adicionar(sender: AnyObject) {
        if task != nil {
            editTask()
        } else {
            createTask()
        }
        dismissViewController()
    }

func createTask() {

    adic.add()
}

func editTask() {

    task?.descricao = vlrDesc.text
    task?.latitude = fieldLatitude.text
    task?.longitude = fieldLongitude.text
    task?.velocidade = picker.selectedRowInComponent(0)
    println(picker.selectedRowInComponent(0))
    managedObjectContext?.save(nil)
    }

func dismissViewController() {
    navigationController?.popViewControllerAnimated(true)
}

    func textFieldSouldReturn (textField: UITextField) -> Bool{
        textField.resignFirstResponder()
        return true
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        self.view.endEditing(true)
    }

    override func viewDidLoad() {

        vlrDesc.text = descrip
        fieldLatitude.text = latitude
        fieldLongitude.text=longitude
        idPicker = Int(velocid)
        picker.selectRow(idPicker, inComponent: 0, animated: true)
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return velocidade[row]
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return velocidade.count
    }

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

1 个答案:

答案 0 :(得分:2)

首先,我先来看看这个:

http://www.raywenderlich.com/85578/first-core-data-app-using-swift

这应该让你开始,并指导你到你需要去的地方。

对于第一个错误:Use of unresolved identifier 'managedObjectContext',这种情况正在发生,因为您实际上没有从AppDelegate中检索此上下文

add()功能中使用此功能:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!

现在,您可以访问整个应用程序为CoreData共享的托管上下文。

对于第二个错误,您必须未在代码中的某处声明或实例化这些变量。

编辑:第二个错误:

所以,问题是该函数没有对这些变量的引用。它们在不同的类中声明,因此类addBD无法与它们交互。

一种解决方案可能是向函数func add()添加参数,尝试将其更改为

func add(vlrDesc: String, etc, etc)

当你致电add时,传递vlrDesc和其他变量作为参数。