尝试在 Swift 中使用 Core Data。找到了这个例子:
http://www.sep.com/sep-blog/2014/06/23/a-c-developer-learns-swift-part-1-core-data/
使用两个字符串字段创建实体“ Person ” - lastname 和 firstname 。创建 UITableViewController ( MainTableViewController ) 以在屏幕上显示记录。创建 UIViewController ( DetailViewController ) 以添加新记录。为实体数据创建了我自己的类 (AddrBook)。
无法显示主类 - MainTableViewController 中实体中包含的记录。
我的类(class) AddrBook.swift :
import UIKit
import CoreData
@objc(AddrBook)
class AddrBook: NSManagedObject {
@NSManaged var lastname:String
@NSManaged var firstname:String
}
UIViewController 添加新记录。 DetailViewController.swift :
import UIKit
import CoreData
class DetailViewController: UIViewController {
@IBOutlet var lastNameField : UITextField = nil
@IBOutlet var firstNameField : UITextField = nil
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func saveButtonPressed(sender : AnyObject) {
let appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let context:NSManagedObjectContext = appDelegate.managedObjectContext
let projectEntity = NSEntityDescription.entityForName("Person", inManagedObjectContext: context)
var newPerson = AddrBook(entity: projectEntity, insertIntoManagedObjectContext: context)
newPerson.lastname = lastNameField.text
newPerson.firstname = firstNameField.text
context.save(nil)
self.dismissViewControllerAnimated(true, completion: nil)
}
}
理论上,这门课一切顺利。必须添加条目。
主类 MainTableViewController.swift 。显示记录。试图通过 NSLog 获取它们:
import UIKit
import CoreData
class MainTableViewController: UITableViewController {
init(style: UITableViewStyle) {
super.init(style: style)
}
init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
let request = NSFetchRequest(entityName: "Person")
request.returnsObjectsAsFaults = false
let appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let context:NSManagedObjectContext = appDelegate.managedObjectContext
var results:NSArray = context.executeFetchRequest(request, error: nil)
for currentPerson in results as AddrBook[] {
NSLog("\(currentPerson.lastname)")
NSLog("\(currentPerson.firstname)")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return 1
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
return 0
}
}
表示表达式有错误
for currentPerson in results as AddrBook[] {
错误:
无法将表达式的类型 'AddrBook[]' 转换为类型 'AddrBook[]'
我究竟做错了什么?
对于 LombaX:
override func viewDidLoad() {
super.viewDidLoad()
let request = NSFetchRequest(entityName: "Person")
request.returnsObjectsAsFaults = false
let appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let context:NSManagedObjectContext = appDelegate.managedObjectContext
var results : AddrBook[]? = context.executeFetchRequest(request, error: nil) as? AddrBook[]
NSLog("\(results)")
if let array = results // check for nil and unwrap
{
for currentPerson in array as AddrBook[] {
NSLog("\(currentPerson.lastname)")
NSLog("\(currentPerson.firstname)")
}
}
// var results:NSArray = context.executeFetchRequest(request, error: nil)
/*for currentPerson in results as AddrBook[] {
NSLog("\(currentPerson.lastname)")
NSLog("\(currentPerson.firstname)")
}*/
}
输出 NSLog - 2014-06-24 21:25:41.243 Lesson-12-swift[1651:136375] nil
变量 结果 为零:-(
Dropbox 项目链接: https://www.dropbox.com/s/q42rw5fw470timi/lesson-12-swift.zip
最佳答案
首先,检查您是否在数据模型中填充了类:
作为 ProjectName.AddrBook
(对于 swift 类,您甚至必须指定项目名称)。 (注意:仅当您在类(class)之前没有使用前缀 @objc(AddrBook) 时才需要这样做,但我看到您使用了它,所以这不是问题)。
或者
像 AddrBook 一样在类部分中的这张图片中,右上角
此外,像这样改变你的 Actor 阵容:
// since executeFetchRequest can return nil, cast it as an optional array of [AddrBook]
// note: the first [AddrBook]? Can be omitted
var results : [AddrBook]? = context.executeFetchRequest(request, error: nil) as? [AddrBook]
if let array = results // check for nil and unwrap
{
for currentPerson in array as [AddrBook] {
// print
}
}
或者,不那么明确并且不检查 nil
var results = context.executeFetchRequest(request, error: nil)
for currentPerson in results as [AddrBook] {
// print
}
关于core-data - Swift 中的核心数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24392166/