【问题标题】:App Crashes When Converting Types - Swift转换类型时应用程序崩溃 - Swift
【发布时间】:2015-03-25 21:34:28
【问题描述】:

我的 swift 应用程序有时可以完美地构建和运行,有时它会崩溃。它会崩溃 3 次中的 2 次。我从 Parse 中提取数字,我将其放入声明为 NSInteger 的数组中,因为数据被转换为 NSInteger。我尝试将数组转换为可互换的 Int/NSInteger 以及我正在提取的数据。然后我将数据发布到一个标签上,该标签被划分为单元格,我必须将其转换为字符串...

由于不同的图片和不同的数据进入单元格,我创建了另一个名为“单元格”的类,如下所示,我的 UI 已连接到该类:2 个标签和图像。单元格是一个 UITableView。

如前所述,有时它会崩溃,有时则不会……我正在从 3 个不同的类中提取数据。会不会有问题?

数据分别从viewDidLoad中查询。

我已经注释掉了下面的行,看看是否 1 是问题,但它们都是有问题的。

这是我用 Parse 中的数据填充的变量:

       var hotScores = [NSInteger]() 
       var totalHotScores = [NSInteger]()
       var images = [UIImage]()
        var imageFiles = [PFFile]()

这是崩溃的代码:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var myCell:cells =    self.tableView.dequeueReusableCellWithIdentifier("myCell") as cells

    myCell.hotScore.text = String(hotScores[indexPath.row] as NSInteger)
    myCell.totalScore.text = String(totalHotScores[indexPath.row] as NSInteger)
    imageFiles[indexPath.row].getDataInBackgroundWithBlock{
        (imageData: NSData!, error: NSError!) -> Void in
        if error == nil {

            let image = UIImage(data: imageData)

            myCell.postedImage.image = image
        }


    }
    return myCell
}

这是我在 viewDidLoad 中查询它的方式:

    var hotQuery = PFQuery(className:"Hots")
    hotQuery.limit = 10
    hotQuery.orderByDescending("numOfHots")
    hotQuery.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil {

            for object in objects {


                self.hotScores.append(object["numOfHots"] as NSInteger)


                self.tableView.reloadData()

            }

    } else {

        println(error)
    }
    }

    var imageQuery = PFQuery(className:"Images")
    imageQuery.limit = 10
    imageQuery.orderByAscending("ImageFiles")
    imageQuery.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil {

            for object in objects {

            self.imageFiles.append(object["ImageFiles"] as PFFile)


                self.tableView.reloadData()

            }

        } else {

            println(error)
        }
    }


    var totalVotesQuery = PFQuery(className:"TotalVotes")
    totalVotesQuery.limit = 10
    totalVotesQuery.orderByDescending("numOfTotalVotes")
    totalVotesQuery.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil {

            for object in objects {

                self.totalHotScores.append(object["numOfTotalVotes"] as NSInteger)
                self.tableView.reloadData()

            }
        } else {
         println(error)
        }
    }

这就是错误所说的:

  Thread 1: EXC_Bad_INSTRUCTION(CODE=EXC_1386_INVOP,SUBCDE=0X0)

最后,UI 元素被命名为:

            hotScore //as the first label
            totalScore // as the 2nd label
            postedImage // as the UIImage

非常感谢您。

【问题讨论】:

  • 请显示查询代码,以及创建/填充hotScores 和totalHotScores 的位置。
  • @TimothyWalters 刚刚更新。非常感谢
  • @TimothyWalters 嘿,你看到更新了吗?再次感谢...
  • 您能评论一下发生错误的行吗?
  • myCell.hotScore.text = String(hotScores[indexPath.row] as NSInteger) 它只针对该行。

标签: ios uitableview swift types parse-platform


【解决方案1】:

请记住,在加载图像时,单元格可能已被重新使用,啊异步代码的乐趣多线程 :)

您应该检查单元格是否仍需要您返回的图像。您可以尝试检查 imageFiles[indexPath.row] 并读取它的 URL 属性并将其与块中本地对象的 URL 进行比较。然后仅在它们仍然匹配时设置图像。

无论如何,对于你的崩溃问题,我还不知道你到底在哪一行崩溃,但我肯定会在那里添加一些 Optionals 安全检查代码,尤其是这些行:

if let image = UIImage(data: imageData) {
  myCell.postedImage?.image = image
}

我不知道postedImage 是否可选,所以我添加了“?”以防万一。不过绝对要使用if let。

从你的 cmets 看来,这条线是问题所在:

myCell.hotScore.text = String(hotScores[indexPath.row] as NSInteger)

在这种情况下,这可能意味着hotScores[indexPath.row] 在该行中没有与 NSInteger 兼容的任何内容。再次安全的救援选项:

if let hotScore = hotScores[indexPath.row] as? NSInteger {
  // safely got the hot score, use it
} else {
  // handle the issue of hotScore being invalid for this row
}

【讨论】:

  • 我认为这是因为我正在从 3 个不同的类中查询 3 个不同的东西。我只是做了一个小测试,看看这是否是问题所在,当我创建一个包含所有对象的类并仅查询该类时,即使我运行 10 次,它似乎也没有崩溃。但如果我只有 1 个类,我将不得不在不同的视图控制器中更改大量代码。但是,这最终可能是最好的做法......也将尝试您提供的此代码。非常感谢。
  • 它一直在崩溃,即使图像前面有“if let”。显示 2 个标签的错误。它甚至运行 3 或 4 次,但第 5 次它会崩溃。太奇怪了...有什么想法可以归咎于这两行吗?
  • 它不允许我用 as 做一个条件?给我一个错误说:“从'NSInteger'到'NSInteger'的条件向下转换总是成功。”如果我包装了 NSInteger,它仅适用于几个构建?作为最后的可选。但是,一旦我使用相同的语法(NSInteger?)添加了totalVotes,它在几次构建后就崩溃了。我应该将数组更改为另一种类型吗?
猜你喜欢
  • 2015-12-12
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 2014-08-05
  • 1970-01-01
相关资源
最近更新 更多