【发布时间】:2017-10-11 08:23:04
【问题描述】:
有很多同名的问题,但不知何故,我觉得每个案例都不一样,因为我的问题仍未解决,我不知道为什么,而且 Swift 也没有足够的解决方案。所以这是我的场景:
我使用CarbonKit 在单个视图控制器中显示 4 个视图控制器。 FourthViewController 使用 AlamoFire 发送 get 调用。它成功加载数据并重新加载tableView。然而,当我拖动这个表格视图时,我的应用程序崩溃了,我得到了EXC_BREAKPOINT,有时也得到了EXC_BAD_ACCESS。
这是我尝试过的:
- 通过搜索,我开始了解僵尸。所以我启用了它,当应用程序给我 EXC 时,我得到了这个:
-[CALayer removeAllAnimations]:消息发送到释放的实例
所以当我检查时,我可能没有在这堂课中使用过任何动画。但考虑到它在 CarbonKit 内部,他们可能与它有关,但我无法弄清楚。
- 我从 xCode 运行 Product > Analyze,大多数蓝色图标都在 FBSDK 上,与此类无关,因此根本无法调试。
以下是截图: 和 这是 Instruments 9.0 僵尸消息给我的信息: 和
我在 xCode 的控制台中得到了这个: 如果您需要更多信息,我会更新我的问题。
更新
这是我使用 Alamofire 从网络获取数据时的代码。现在这个调用成功运行,我可以向下滚动查看表中加载的所有数据。但是当我向上滚动时,它会因上述错误而崩溃。
let headers = [
"security-token": "MY_SECURITY_CODE_HERE",
"Content-Type": "application/x-www-form-urlencoded"
]
let url = "URL_GOES_HERE"
//print(url)
Alamofire.request(url, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
if let result = response.result.value as? [String: AnyObject] {
switch response.result {
case .success:
if result["status"] as! Int == 0
{
self.clearAllNotice()
let alert = UIAlertController(title: "Error",
message: "\(result["msg"] ?? "Our Server is not responding at the moment. Try again later!" as AnyObject)",
preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "OK",
style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true)
}
else
{
self.clearAllNotice()
for docReviewDict in (result["data"] as! [AnyObject]) {
let docReview = DoctorReview(dict: docReviewDict as! [String : AnyObject])
self.mainDoctorReviewsArray.append(docReview)
}
self.tableView.reloadData()
}
case .failure(let error):
self.clearAllNotice()
let alert = UIAlertController(title: "Something is wrong",
message: error.localizedDescription,
preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "OK",
style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true)
}
} else {
print("Somethins is wrong with the URL")
}
}
最新:
根据我所做的评论:
DispatchQueue.main.async {
self.tableView.reloadData()
}
但我得到的结果仍然相同
线程 1:EXC_BAD_ACCESS(代码=1,地址=0xb4c4beb8)
当我在表格视图上向下拖动时,在 let cell = tableView.dequeueReusableCell(withIdentifier: "ReviewCell") as! ReviewsTableViewCell 行上。
单元的实现
类名是ReviewsViewController,其中有一个@IBOutlet fileprivate var tableView: UITableView!。
然后在viewDidLoad():
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView()
然后我有表视图委托和数据源:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mainDoctorReviewsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ReviewCell") as! ReviewsTableViewCell //WHEN THE APP IS CRASHED AS I DRAG THE TABLE VIEW IT CRASH ON THIS LINE. As this line is highlighted in red with message "Thread 1: EXC_BREAKPOINT (code=1, subcode=0x186ddd61c)"
cell.userPhoto.layer.cornerRadius = cell.userPhoto.frame.width / 2;
cell.userPhoto.layer.masksToBounds = true
cell.userName.text = mainDoctorReviewsArray[indexPath.row].name
cell.starRating.settings.fillMode = .precise
if let rate = mainDoctorReviewsArray[indexPath.row].rating
{
cell.starRating.rating = Double(rate)!
}
else {
cell.starRating.rating = 0
}
cell.desc.text = mainDoctorReviewsArray[indexPath.row].feedback
cell.dateOfReview.text = mainDoctorReviewsArray[indexPath.row].dates
cell.timeOfReview.text = mainDoctorReviewsArray[indexPath.row].times
return cell
}
在单元格中只有:
import UIKit
import Cosmos
class ReviewsTableViewCell: UITableViewCell {
@IBOutlet var timeOfReview: UILabel!
@IBOutlet var dateOfReview: UILabel!
@IBOutlet var desc: UITextView!
@IBOutlet var starRating: CosmosView!
@IBOutlet var userName: UILabel!
@IBOutlet var userPhoto: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
tableView的截图在本题上方。
这是加载 4 个 ViewControllers 的父类:dropbox.com/s/d5sh4ej1ssv6873/… 这是具有 tableview 的类,滚动时应用程序崩溃:dropbox.com/s/3kiju9hn3uewzar/ReviewsViewController.swift?dl=0
【问题讨论】:
-
检查您是否使用了 assign 而不是 strong/weak 作为任何属性的访问说明符。它可能会导致僵尸。
-
@PuneetSharma 不在我的代码中,但在我搜索
assign时,我在我安装的许多 pod 中都得到了这个。 -
可能是UI相关的任务,不在主线程上执行。
-
您能通过 Alamofire 发布通话代码吗?
-
@user3581248 请查看我的问题的更新部分。