Ios Xcode 6从UIViewController访问UIApplication
我有一个使用CoreData存储用户信息的简单应用程序。我对Xcode非常陌生,所以请原谅我的无知,但我似乎没有包括正确的模块,因为Xcode没有自动完成UIApplication、NSManagedObjectContext和NSEntityDescription等类及其函数 此外,我得到一些错误显示在屏幕截图 我是否需要导入另一个模块以便识别它们Ios Xcode 6从UIViewController访问UIApplication,ios,swift,xcode6,Ios,Swift,Xcode6,我有一个使用CoreData存储用户信息的简单应用程序。我对Xcode非常陌生,所以请原谅我的无知,但我似乎没有包括正确的模块,因为Xcode没有自动完成UIApplication、NSManagedObjectContext和NSEntityDescription等类及其函数 此外,我得到一些错误显示在屏幕截图 我是否需要导入另一个模块以便识别它们 import UIKit import CoreData class OutlineViewController: UIViewControll
import UIKit
import CoreData
class OutlineViewController: UIViewController {
@IBOutlet weak var textFieldTitle: UITextField = nil
@IBOutlet weak var textFieldTag: UITextField = nil
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func saveTapped(sender: AnyObject) {
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext
let en: NSEntityDescription.entityForName("Outline", inManagedObjectContext: context)
// I get the error on the above line that "Consecutive statements on a line must be separated by ';'" and it suggests that I place a semicolon after entityForName
var newItem = Model(entity: en, insertIntoManagedObjectContext: context)
newItem.title = textFieldTitle.text
newItem.Tag = textFieldTag.text
context.save(nil)
println(newItem)
self.navigationController.popToRootViewControllerAnimated(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
从我在中所读到的,NSEntityDescription是CoreData的一部分。为什么它没有被识别?您在需要类型声明的地方给出了语句。你忘了作业:
import UIKit
import CoreData
class OutlineViewController: UIViewController {
@IBOutlet weak var textFieldTitle: UITextField = nil
@IBOutlet weak var textFieldTag: UITextField = nil
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func saveTapped(sender: AnyObject) {
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext
let en: NSEntityDescription.entityForName("Outline", inManagedObjectContext: context)
// I get the error on the above line that "Consecutive statements on a line must be separated by ';'" and it suggests that I place a semicolon after entityForName
var newItem = Model(entity: en, insertIntoManagedObjectContext: context)
newItem.title = textFieldTitle.text
newItem.Tag = textFieldTag.text
context.save(nil)
println(newItem)
self.navigationController.popToRootViewControllerAnimated(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
let en: NSEntityDescription? = NSEntityDescription.entityForName("Outline", inManagedObjectContext: context)
要解决所有其他问题,请签出:
class OutlineViewController: UIViewController {
@IBOutlet weak var textFieldTitle: UITextField? = nil
@IBOutlet weak var textFieldTag: UITextField? = nil
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func saveTapped(sender: AnyObject) {
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext
let en: NSEntityDescription = NSEntityDescription.entityForName("Outline", inManagedObjectContext: context)!
// I get the error on the above line that "Consecutive statements on a line must be separated by ';'" and it suggests that I place a semicolon after entityForName
var newItem = Model(entity: en, insertIntoManagedObjectContext: context)
newItem.title = textFieldTitle.text
newItem.Tag = textFieldTag.text
context.save(nil)
println(newItem)
self.navigationController?.popToRootViewControllerAnimated(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
感谢您的评论,但这似乎引发了一些其他有趣的错误,似乎您没有展开或可选链接可选变量。查看更新的答案。