我有以下课程:
import UIKit
class HeaderTableViewController: UITableViewController {
var nonDefaultTitle : String?
var data: [String: [String]] = [:]
var headers : [String] = []
var bodys : [[String]] = []
init(displayData: [String: [String]]) {
super.init(style: .Grouped)
data = displayData
for key in data.keys {
headers.append(key)
}
for arr in data.values {
bodys.append(arr)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.backgroundColor = UIColor.whiteColor()
tableView.registerNib(UINib(nibName: "DefaultCustomCell", bundle: nil), forCellReuseIdentifier: "DefaultCustomCell")
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 50
tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
tableView.estimatedSectionHeaderHeight = 100;
let nib = UINib(nibName: "HeaderTableHeaderView", bundle: nil)
tableView.registerNib(nib, forHeaderFooterViewReuseIdentifier: "HeaderTableHeaderView")
if let ttle = nonDefaultTitle {
self.title = ttle
}
else {
self.title = "Talking Tips"
}
}
}
//
// MARK: Helpers
//
extension HeaderTableViewController {
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return headers.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let body = bodys[section] as [String]
return body.count
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = self.tableView.dequeueReusableHeaderFooterViewWithIdentifier("HeaderTableHeaderView")
let header = cell as? HeaderTableHeaderView
header!.titleLabel.text = headers[section]
return cell
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("DefaultCustomCell", forIndexPath: indexPath) as! DefaultCustomCell
cell.titleLabel.text = bodys[indexPath.section][indexPath.row]
cell.selectionStyle = .None
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { }
}
//
// MARK: The table headers
//
class HeaderTableHeaderView: UITableViewHeaderFooterView {
@IBOutlet weak var titleLabel: UILabel!
}
//
// MARK: Class that handles the HeaderTableLargeHeaderView (sorry for the terrible naming conventions)
//
class HeaderTableLargeHeaderView: UIView {
// Our title label with autolayout constraints
@IBOutlet weak var titleLabel: UILabel!
// Convience method to load for xib
class func instanceFromNib() -> UIView {
return UINib(nibName: "HeaderTableLargeHeaderView", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! UIView
}
override func layoutSubviews() {
super.layoutSubviews()
}
}
非常直接。如果我使用Diawi通过Xcode或AdHoc安装,它运行正常。
但是,如果我通过TestFlight或通过AppStore安装,它每次都会崩溃....我在这里做错了吗?我的设备日志给了我这个:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000000
Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [0]
Triggered by Thread: 0
Filtered syslog:
None found
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 UIKit 0x0000000196387a4c __66-[UISectionRowData refreshWithSection:tableView:tableViewRowData:]_block_invoke + 424
1 UIKit 0x00000001963879f0 __66-[UISectionRowData refreshWithSection:tableView:tableViewRowData:]_block_invoke + 332
2 UIKit 0x0000000196347368 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 2712
3 UIKit 0x00000001963467e4 -[UITableViewRowData rectForFooterInSection:heightCanBeGuessed:] + 536
4 UIKit 0x0000000196346570 -[UITableViewRowData heightForTable] + 60
5 UIKit 0x00000001963463a4 -[UITableView _updateContentSize] + 220
6 UIKit 0x000000019634c918 -[UITableView setContentInset:] + 252
7 UIKit 0x0000000196320438 -[UIViewController _setNavigationControllerContentInsetAdjustment:] + 428
8 UIKit 0x000000019632017c -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 464
9 UIKit 0x0000000196322214 -[UINavigationController _layoutViewController:] + 160
10 UIKit 0x0000000196322094 -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 416
11 UIKit 0x0000000196408a58 -[UINavigationController _startCustomTransition:] + 2076
12 UIKit 0x0000000196320d74 -[UINavigationController _startDeferredTransitionIfNeeded:] + 676
13 UIKit 0x00000001963209dc -[UINavigationController __viewWillLayoutSubviews] + 64
14 UIKit 0x0000000196320940 -[UILayoutContainerView layoutSubviews] + 188
15 UIKit 0x0000000196265738 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1196
16 QuartzCore 0x000000019372e40c -[CALayer layoutSublayers] + 148
17 QuartzCore 0x00000001937230e8 CA::Layer::layout_if_needed(CA::Transaction*) + 292
18 QuartzCore 0x0000000193722fa8 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 32
19 QuartzCore 0x000000019369fc64 CA::Context::commit_transaction(CA::Transaction*) + 252
20 QuartzCore 0x00000001936c70d0 CA::Transaction::commit() + 512
21 UIKit 0x000000019625ae08 _afterCACommitHandler + 324
22 CoreFoundation 0x00000001903cd7dc __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
23 CoreFoundation 0x00000001903cb40c __CFRunLoopDoObservers + 372
24 CoreFoundation 0x00000001903cb89c __CFRunLoopRun + 1024
25 CoreFoundation 0x00000001902fa048 CFRunLoopRunSpecific + 444
26 GraphicsServices 0x0000000191d7d198 GSEventRunModal + 180
27 UIKit 0x00000001962d3818 -[UIApplication _run] + 684
28 UIKit 0x00000001962ce550 UIApplicationMain + 208
29 Care Com 0x00000001000db12c 0x1000b4000 + 160044
30 libdyld.dylib 0x000000018f2dc5b8 start + 4
所以我已经能够通过将我的运行方案从调试切换到发布来追踪问题。这是崩溃的错误:
malloc: *** mach_vm_map(size=8589934592) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
所以我找到了我认为的解决方案但是我不确定它为什么会起作用。我在这一行上得到了上述错误:
bodys[section].count
如果我转过来:
Optimization Level: Fast, Single-File Optimization [-0]
到
Optimization Level: None [-Onone]
所以当通过Xcode和TestFlight安装时,上述修复工作正常,但是当从应用程序商店下载时崩溃...仍然打开并寻找修复程序。
我尝试将init
方法更改为:
init(displayData: [String: [String]]) {
super.init(style: .Grouped)
//data = displayData
// I get a bad access error thrown when trying to access displayData
print(displayData)
for key in displayData.keys {
headers.append(key)
}
for arr in displayData.values {
bodys.append(arr)
}
}
并重新启用优化,在尝试使用init参数时会给我EXC_BAD_ACCESS
。
我还运行了没有优化的仪器来检查泄漏和内存分配,这就是我得到的。这是通过TestFlight和Xcode正常运行的版本,但在通过AppStore安装时崩溃。红色箭头是我推到生产中崩溃的视图时。
以下是我实例化和推送视图的方式:
func testPush() {
let data = createTestData()
let tableHeaderVC = HeaderTableViewController(displayData: data as! [String : [String]])
self.navigationController?.pushViewController(tableHeaderVC, animated: true)
}
func createTestData() -> [String : [String]] {
let firstSection = ["Row one", "Row two"]
let secondSection = ["Row one", "Row two"]
return ["First" : firstSection, "Second" : secondSection]
}
此外,在看了这个问题后,我想也许这是一个自定义初始化程序的问题。所以我删除了它并在推送之前设置了变量...在优化时仍然得到相同的错误。