2017-02-14 117 views
0

我正在遵循一個非常簡單的swift課程,儘管一切適用於教師,但相同的代碼並未在我身邊執行,我試圖理解爲什麼會發生這種情況。引用Swift 3中的實體屬性(核心數據)

該應用是很簡單的,是由添加的任務到一個TableView中使用一個名稱和一個開關,以確定是否這些是重要的(在這種情況下的表情符號被添加到名稱)

的當嘗試訪問並修改我的「Taskentity」核心數據實體的「名稱」屬性,編輯器給我錯誤「類型」Taskentity「的值沒有成員」名稱「」。

的代碼如下:

import UIKit 

class AddTaskViewController: UIViewController { 

@IBOutlet weak var textField: UITextField! 

@IBOutlet weak var isImp: UISwitch! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
} 

@IBAction func btnTapped(_ sender: Any) { 

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 

    let task = Taskentity(context: context) 
    task.name = textField.text! // **!error!** 

    task.isImportant = isImp.isOn 

    (UIApplication.shared.delegate as! AppDelegate).saveContext() 

    navigationController!.popViewController(animated: true) 
} 

}

而我的核心數據文件看起來像這樣: Core Data file

感謝您的幫助!

回答

0

你的屏幕快照完美地顯示了問題。您已將屬性命名爲corename,而不是name。所以很自然的名字name不是它的名字!

0

您的屬性有問題。一個名爲'corename'的字符串應該被重命名爲'name'Xcode不能說出什麼.name意思是因爲你沒有什麼Devi得到它的東西。

我重視你的代碼看起來應該解決這個問題:

import UIKit 

class AddTaskViewController: UIViewController { 

@IBOutlet weak var textField: UITextField! 

@IBOutlet weak var isImp: UISwitch! 

override func viewDidLoad() { 
    super.viewDidLoad() 

// Do any additional setup after loading the view. 
} 

@IBAction func btnTapped(_ sender: Any) { 

let context = (UIApplication.shared.delegate as!  AppDelegate).persistentContainer.viewContext 

let task = Taskentity(context: context) 
task.corename = textField.text! // **!error!** 

task.isImportant = isImp.isOn 

(UIApplication.shared.delegate as! AppDelegate).saveContext() 

navigationController!.popViewController(animated: true) 
} 
} 
相關問題