你好,我在Swift中遇到了一個核心數據問題,它不會顯示錯誤,但是當我相信它應該在控制檯中返回'Joe'+'pass'時不會打印任何內容。任何人都可以幫忙嗎?Coredata in swift
import UIKit
import CoreData
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context:NSManagedObjectContext = appDel.managedObjectContext
var newUser = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context) as NSManagedObject
newUser.setValue("Joe", forKey: "username")
newUser.setValue("pass", forKey: "password")
do {
try context.save()
} catch let error {
print("Couldn't save user data")
print(error)
}
let request = NSFetchRequest(entityName: "Users")
request.returnsObjectsAsFaults = false
do {
var results = try context.executeFetchRequest(request)
results = results as! [NSManagedObject]
if results.count > 0 {
for results in results {
print(results)
}}
} catch let error as NSError {
print(error)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
「爲結果在結果」 - 這真的是你的代碼?因爲這不應該編譯。與此同時,在調試器的上方斷開並檢查'results',它是否包含任何內容? –
昨天我在一篇帖子中看到了這個帖子,當時我沒有得到我的錯誤修正,我對Swift非常陌生,所以試圖找到我的腳。看看這個,http://stackoverflow.com/questions/39592609/core-data-issue-swift –