我無法弄清楚這裏有什麼問題。核心數據錯誤:'AnyObject'沒有名爲'Generator'的成員
該代碼只需將username
和password
添加到實體「用戶」,然後檢索它。
代碼工作正常,直到我在其中添加for循環。我甚至嘗試鑄造AnyObject
到NSManagedObject
(這是不是就我所知需要)
代碼:
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("Rob", forKey: "username")
newUser.setValue("pass", forKey: "password")
context.save(nil)
var request = NSFetchRequest(entityName: "Users")
request.returnsObjectsAsFaults = false
var results = context.executeFetchRequest(request, error: nil)
println(results)
if results?.count > 0 {
for result: AnyObject in results!{
println(result)
}
} else {
println("No results")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
錯誤顯示在for循環線路:for results
我已經逐字複製您的代碼放到一個典型的核心數據的應用程序,並能順利完成編譯。但這可能是有用的:http://stackoverflow.com/questions/26458209/how-to-print-out-value-from-coredata-dictionary-in-swift-anyobject-does-not-ha。 –
@MartinR謝謝..作品完美..我已經投票了.. bt不幸的是我沒有15聲望..總之..再次感謝 –