2014-09-29 86 views
1

我有一個簡單的應用程序,使用CoreData存儲用戶信息。我對Xcode很陌生,所以原諒我的無知,但似乎我沒有包含正確的模塊,因爲Xcode不是自動完成的類,如UIApplication,NSManagedObjectContextNSEntityDescription(及其函數)。Xcode 6訪問UIAiewlication從UIViewController

此外我收到一些錯誤(在屏幕截圖中顯示)。

我是否需要導入另一個模塊才能識別它們?

enter image description here

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() 
    }  
} 

從我在docs讀過,NSEntityDescriptionCoreData一部分。爲什麼不被認可?

回答

0

您在聲明需要類型聲明的地方給出聲明。你忘了分配:

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() 
    }  
} 
+0

謝謝你的評論,但是這似乎拋出一些其他有趣的錯誤http://imgur.com/4ldT2MG – 2014-09-29 23:46:08

+0

似乎你不打開或可選鏈接可選變量。查看更新後的答案。 – Kirsteins 2014-09-30 06:16:33