繁体   English   中英

Xcode控制台调试输出

[英]Xcode console debugging output

我正在从Firebase下载数据并将数据显示到tableView上。 但是,我发现我的tableView中有时会出现重复的内容。 我最初以为我不小心在PostService.ps.posts数组中插入了相同的内容。 但是,在刚好重新加载tableView之前设置了一个断点之后,我在控制台中键入了以下命令,并且输出很奇怪。

打印PostService.ps.posts

如下面的输出所示,项目[4]和项目[6]具有{...},我不知道它们代表什么(这是我第一次使用控制台命令进行调试)。 并在我的tableView中。 我看到了按以下顺序显示的数据[1],[2],[3],[3],[5],[5],[6],[7],[8],[9](对于第0行到第8行)。 这样看来,第3行(从0开始)从第2行获取相同的数据,第5行与第4行获取相同的数据。

我不确定为什么会这样,而且我无处可去,因为它不会每次都发生。 我希望这个控制台输出可以帮助我指出正确的方法

  [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"
  }

我的所有表数据都是根据PostService.ps.posts内部的数据类型配置的,该数据类型为[Post]在viewForHeaderInSection内部(我在应用中使用的是它而不是cellForRowAtIndexPath),我有以下代码来配置表

    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()
    }

好的,我阅读了您的问题,让我帮助您理解用于打印PostService.ps.posts数组元素的PostService.ps.posts

po命令打印对象的“描述”方法。

尝试写po PostService.ps.posts[section]viewForHeaderInSection ,并且将打印post的对象类型。

在这里,您可以按如下所示打印post自定义类对象的所有属性:-现在编写po post.imageUrlpo post.postKeypo post.userId

交叉检查所有标头的所有这些输出。 检查这些值是否相同或不同。 如果这些值不同,则问题将出在表单元格的出队中;否则,如果这些值相同,则问题将出在服务器逻辑(后端)中。

最终,我知道了某些元素将描述显示为{...}的原因 这是由于打印了重复的参考对象。 在控制台中,实际对象让我们看到内容,但是重复的对象仅显示为{...} 在您的情况下,您具有[3]和[4]元素,而[5]和[6]元素具有相同的引用,因此重复的元素显示{...} 为此,我尝试了一个简单的示例。 希望它能对此有所澄清。

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.
    }

}

输出:

(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)

注意:它仅在类对象的数组上发生。 因为类是引用类型。

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

暂无
暂无

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

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