使用UIapplication作为委托无法保存核心数据

时间:2016-02-26 01:28:47

标签: swift

我正在尝试使用核心数据来保存一些数据。我在xmodel中创建了一个名为NewProject的实体。但是,当我尝试通过单击开始按钮保存数据时。它不起作用,我甚至不知道出了什么问题。

 func saveASingleNewProject(eventTitle: String, startTime : String) {
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    let managedContext = appDelegate.managedObjectContext

    let entity = NSEntityDescription.entityForName("NewProject", inManagedObjectContext: managedContext)

    let aNewProject = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)

    aNewProject.setValue(eventTitle, forKey: "eventTitle")
    aNewProject.setValue(startTime, forKey: "startTime")


    do {
        try managedContext.save()
        //projectList.append(aNewProject)
        print ("reached this part")
    }catch let error as NSError {
        print ("could not save it")
    }


}




@IBAction func startButton(sender: AnyObject) {
    if firstTimeClick == 0 {
 timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: ("updateCounter"), userInfo: nil, repeats: true)
    firstTimeClick = 1
    }

    // assign the project title to the variable
    if currentProjectTitle != nil {
    projectTitle = currentProjectTitle.text!
    }


    //create a single project


    //when start is pressed, the window closes
    currentProjectTitle.text = nil
    currentProjectDisplay.text = projectTitle!

    //save the values



    saveASingleNewProject("anew project", startTime: "30 minutes interval")


}

0 个答案:

没有答案