Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何在MVC中使用coredata_Swift_Core Data - Fatal编程技术网

Swift 如何在MVC中使用coredata

Swift 如何在MVC中使用coredata,swift,core-data,Swift,Core Data,我可以在函数中使用核心数据,但不能在MVC中创建,如下所示: class addBD{ func add(){ let entityDescripition = NSEntityDescription.entityForName("Radar",inManagedObjectContext: managedObjectContext!) let task = Radar(entity: entityDescripition!, ins

我可以在函数中使用核心数据,但不能在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(){
让appDelegate=UIApplication.sharedApplication()。委托为!appDelegate
让managedContext=appDelegate.managedObjectContext!
让EntityDescription=NSEntityDescription.entityForName(“雷达”,在ManagedObjectContext:managedContext中)
让任务=雷达(实体:EntityDescription!,插入ManagedObjectContext:managedContext)
task.descripcao=vlrDesc.text
task.latitude=fieldLatitude.text
task.longitude=fieldLongitude.text;
task.velocide=picker.selectedRowUncomponent(0)
managedContext.save(无)
}
}
导入UIKit
导入CoreData
类adicionarRadar:UIViewController、NSFetchedResultsControllerDelegate{
velocidade变量=[“40”、“50”、“70”、“90”、“100”、“120”]
变量纬度:String=“”
变量经度:String=“”
变量descripp:String=“”
var velocid:NSNumber=0.0
var任务:雷达?=无
变量idPicker:Int=0
设adic=adiciona()
让managedObjectContext=(UIApplication.sharedApplication().delegate为!AppDelegate)。managedObjectContext
@IBOutlet弱var字段纬度:UITextField!
@IBOutlet弱变量字段经度:UITextField!
@IBVAR选择器:UIPickerView!
@IBOutlet弱var vlrDesc:UITextField!
@iAction func adicionar(发送方:AnyObject){
如果任务!=nil{
编辑任务()
}否则{
createTask()
}
dismissViewController()
}
func createTask(){
adic.add()
}
func editTask(){
任务?.descripcao=vlrDesc.text
任务?.latitude=fieldLatitude.text
任务?.longitude=fieldLongitude.text
任务?.Velociade=picker.SelectedRowUncomponent(0)
println(选择器。SelectedRowUncomponent(0))
managedObjectContext?.save(无)
}
func dismissViewController(){
navigationController?.popViewControllerAnimated(真)
}
func textFieldSouldReturn(textField:UITextField)->Bool{
textField.resignFirstResponder()辞职
返回真值
}
覆盖func touchesBegined(触摸:设置,withEvent事件:UIEvent){
self.view.endEditing(true)
}
重写func viewDidLoad(){
vlrDesc.text=descripp
fieldLatitude.text=纬度
fieldLongitude.text=经度
idPicker=Int(velocid)
picker.selectRow(idPicker,不完整:0,动画:true)
super.viewDidLoad()
//加载视图后执行任何其他设置。
}
func pickerView(pickerView:UIPickerView,titleForRow行:Int,forComponent组件:Int)->字符串{
返回velocidade[世界其他地区]
}
func pickerView(pickerView:UIPickerView,numberOfRowsInComponent:Int)->Int{
返回velocidade.count
}
func numberOfComponentsInPickerView(pickerView:UIPickerView)->Int{
返回1
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
}

首先,我想先看看这里:

这会让你开始,并指导你去哪里

对于第一个错误:
使用未解析标识符“managedObjectContext”
,发生这种情况是因为您尚未从AppDelegate中实际检索到此上下文

add()
函数中使用此选项:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!
现在,您可以访问整个应用程序中共享的CoreData托管上下文

对于第二个错误,您必须没有在代码中的某个地方声明或实例化这些变量

编辑:第二个错误:

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

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

func-add(vlrDesc:String等)


当您调用
add
时,将
vlrDesc
和其他变量作为参数传递。

thks,这解决了我的第一个错误,第二个错误是我没有声明变量,但这些变量是出口,我如何在MVC中设置出口的值?您可以发布整个Swift文件吗?我不能不看看vlrDesc在哪里,是什么。我将编辑我的原始答案,这将解决你的问题。
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!