简体   繁体   English

在SWIFT中的uitableview中打印核心数据数据库

[英]Print a Core Data database in uitableview in SWIFT

I have a tableview HomeViewController connected to my database (coredata) with a Post entity. 我有一个通过Post实体连接到我的数据库(coredata)的tableview HomeViewController。

The table view displays correctly. 表格视图正确显示。 The title of each post is displayed in the cells. 每个帖子的标题显示在单元格中。 But I don't understand something. 但是我不明白。

When I print post.valueForKey("title"), I have the title. 当我打印post.valueForKey(“ title”)时,我有标题。 But if I print just println(post) nothing is printed. 但是,如果我只打印println(post),则什么也不会打印。 Why ? 为什么呢

class HomeViewController: UITableViewController, UITableViewDelegate {

    private var posts = [Post]()

    override func viewWillAppear(animated: Bool) {
        let fetchRequest = NSFetchRequest(entityName:"Post")
        var sorter: NSSortDescriptor = NSSortDescriptor(key: "date" , ascending: false)
        fetchRequest.sortDescriptors = [sorter]

        let fetchResultsPosts = CoreDataManager.sharedManager.managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [Post]
        println(CoreDataManager.sharedManager.managedObjectContext!) //NOT EMPTY
        println(fetchResultsPosts) //NOT EMPTY


        if let results = fetchedResults {
            posts = results //PRINT optional ([,  ,  ,  ])
        } 
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath) as UITableViewCell

        let post = posts[indexPath.row] as Post
        println(post) // PRINT NOTHING
        println(post.valueForKey("title")) //PRINT Optional(Le titre de mon post)

        cell.textLabel.text = post.valueForKey("title") as String?


        return cell
    }

}

In the console : 在控制台中:

--->isam.HomeViewController - viewWillAppear<---
Optional(<NSManagedObjectContext: 0x7faa12649150>)
<NSFetchRequest: 0x7faa126919e0> (entity: Post; predicate: ((null)); sortDescriptors: ((
    "(date, descending, compare:)"
)); type: NSManagedObjectResultType; )
Optional([, , , ])
--->isam.HomeViewController - tableView(_:cellForRowAtIndexPath:)<---

Optional(Le titre de mon post)
--->isam.HomeViewController - tableView(_:cellForRowAtIndexPath:)<---

I need to understand why, because I have to pass the Post for the next view controller (when user presses a cell). 我需要了解原因,因为我必须将Post传递给下一个视图控制器(当用户按下单元格时)。

If you print with println you need to provide a string. 如果使用println打印,则需要提供一个字符串。

println(post.description)

I would not worry about it. 我不会担心。 The post seems to have been retrieved successfully because you can access its attributes. 该帖子似乎已成功检索,因为您可以访问其属性。 Just pass the object on to your next controller if you need to. 如果需要,只需将对象传递给下一个控制器。

In Core Data, objects are fetched quite efficiently, including a method called "faulting". 在Core Data中,对象的获取非常有效,包括称为“故障转移”的方法。 That means that the attributes of the fetched objects are not fetched right away but only when they are needed. 这意味着获取的对象的属性不会立即获取,而仅在需要时才获取。 All this is happening behind the scenes, so you do not have to care about it. 所有这些都是在幕后发生的,因此您不必在意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM