简体   繁体   中英

Xcode console debugging output

I am downloading data from firebase and displaying the data onto a tableView. However, I am finding that every now and then there appears to be duplicated content in my tableView. I initially thought that I have accidentally inserted the same content in my PostService.ps.posts array. However, after I put a break point just before I reload the tableView, I typed the following command in the console and the output is weird.

print PostService.ps.posts

As shown in the output below, item [4] and item [6] has {...} which I dont know what they stand for (This is the first time I am using console command to debug). And in my tableView. I see the the data displayed in the following sequence [1],[2],[3],[3],[5],[5],[6],[7],[8],[9] (for row 0 to row 8). So it appears that row 3 (starting form 0) is getting the same data as row 2 and row 5 is the getting the same data as row 4. Weird?

I am not sure why this is happening and I have no where to start because it doesnt happen every time. I am hoping this console output would help pointing me to the correct way

  [1] = 0x000000012f4849b0 {
    _postKey = "-KQTh_WDO2IS1rYfLgnU"
    _userId = "SpmZmYwD3Td04sQZxOYeALwuMp03"
  }

  [2] = 0x0000000130e61f40 {
    _postKey = "-KQTHnE4IIeBR3tyDM3r"
    _userId = "1isMS98ZmXYrpCmGb4o5voaAqXH2"
    _image = "www.image2.com"
  }

  [3] = 0x0000000130192b40 {
    _postKey = "-KQtN4YG51HOF19FrM8s"
    _userId = "SpmZmYwD3Td04sQZxOYeALwuMp03"
    _image = "www.image3.com"
  }

  [4] = 0x0000000130192b40 {...}

  [5] = 0x0000000130269560 {
    _postKey = "-KR2On6u7dRy0GAvE1Gn"
    _userId = "SpmZmYwD3Td04sQZxOYeALwuMp03"
    _image = "www.image4.com"
  }

  [6] = 0x0000000130269560 {...}

  [7] = 0x000000013150f4c0 {
    _postKey = "-KQThGLVA-MsviGMsXOS"
    _userId = "1isMS98ZmXYrpCmGb4o5voaAqXH2"
    _image = "www.image5.com"
  }

  [8] = 0x000000012fa88890 {
    _postKey = "-KQt269uHGM99oFKuJRt"
    _userId = "SpmZmYwD3Td04sQZxOYeALwuMp03"
    _image = "www.image6.com"
  }

  [9] = 0x000000012f9bcad0 {
    _postKey = "-KQThdCAm-PlCsCnXcBZ"
    _userId = "SpmZmYwD3Td04sQZxOYeALwuMp03"
  }

All my table data are configured based on the data inside my PostService.ps.posts which is of type [Post] Inside my viewForHeaderInSection (I am using this instead of cellForRowAtIndexPath in my app), I have the following code to configure the table

    if let cell = tableView.dequeueReusableHeaderFooterViewWithIdentifier("TableSectionHeader") as? TableSectionHeader {
        let post = PostService.ps.posts[section]
        var image: UIImage?
        if let url = post.imageUrl {
            image = DiscoverVC.imageCache.objectForKey(url) as? UIImage
        }            
        cell.configureCell(post, image: image)
        cell.delegate = self

        return cell

    } else {
        return TableSectionHeader()
    }

Okay I read your question and let me help you to understand the command which you are using for printing the elements of PostService.ps.posts array.

po command prints the object's "description" method.

Try to write po PostService.ps.posts[section] inside viewForHeaderInSection , and it will print post type object.

Here you can print all the properties of post custom class object as follows:- Now write po post.imageUrl , po post.postKey , po post.userId

Cross check all of these outputs for all headers. Check if these values are same or different. If these values are different, then the issue will be in dequeuing the table cell, else if these values are same, then the issue will be in your server logic(backend).

Eventually, I came to know the reason why some elements are showing description as { ... } . It is due to the duplicate reference objects printing. In console actual object letting us to see the contents, but the duplicates objects are just showing like { ... } . In your case you have the [3] and [4] elements and [5] and [6] elements has the same reference, Hence duplicate elements showing { ... } . For this I have tried one simple example. Hope it will give some clarity on that.

import UIKit

class ViewController: UIViewController {

    class model {
        let first = "first"
        let second = "second"
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        var array = [model]()

        let obj = model()
        let obj1 = model()

        array.append(obj) // original objct
        array.append(obj) // duplicate because classes are referece types
        array.append(obj) // duplicate because classes are referece types
        array.append(obj1) // new Object

        print(array)


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

output:

(lldb) print array
([SampleDebugApp.ViewController.model]) $R0 = 4 values {
  [0] = 0x00007fed8ac93990 (first = "first", second = "second")
  [1] = 0x00007fed8ac93990 {...}
  [2] = 0x00007fed8ac93990 {...}
  [3] = 0x00007fed8ac939d0 (first = "first", second = "second")
}
(lldb)

NOTE: It happens only on the array of class object. because class are the reference type.

从我多年使用Xcode的经验来看,控制台日志在打印收集对象方面不是很可靠,只是尝试记录您怀疑的特定项目并查看在那里会发生什么;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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