gpt4 book ai didi

swift - MVC中如何使用coredata

转载 作者:可可西里 更新时间:2023-11-01 01:05:52 25 4
gpt4 key购买 nike

我可以在函数中使用核心数据,但我不能在 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()
}

我有这个错误:

Use of unresolved identifier 'managedObjectContext'

Use of unresolved identifier '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.
}

}

最佳答案

首先,我将从这里开始:

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 和其他变量作为参数传入。

关于swift - MVC中如何使用coredata,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30536009/

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