【发布时间】: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