用于参数传递的Swift类型转换,EXC_BAD_ACCESS(代码= 1,地址= 0x14)

时间:2014-12-28 06:42:27

标签: ios swift

在我的代码中,我有一句话:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!){
    if segue.identifier == "seguetotable"{
        var tableView: TableViewController = segue.destinationViewController as TableViewController
        var callBack: TableViewCallBack = self
        tableView.delegate = callBack;
        NSLog("segueprepared")
    }
}

如您所见,我正在尝试使用委托方法传递参数。它在行:

处抛出EXC_BAD_ACCESS(代码= 1,地址= 0x14)错误
tableView.delegate = callBack;

我搜索了google和stackoverflow,这似乎是与内存管理相关的错误。但我无法弄清楚为什么。 代表定义为

class TableViewController: UITableViewController {
    var items: [Thumbnail]=[]
    var delegate: TableViewCallBack? = nil

其中TableViewCallBack是定义为

的协议
protocol TableViewCallBack {
    func updateImage(items: [Thumbnail]);
}

回溯如下:

  • 线程#1:tid = 0xd6a8a,0x02004ab8 libswiftCore.dylib swift_dynamicCastClassUnconditional + 56, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0) <ul> <li>frame #0: 0x02004ab8 libswiftCore.dylib swift_dynamicCastClassUnconditional + 56 帧#1:0x000914d0 FootprintinWorld FootprintinWorld.ViewController.prepareForSegue (segue=0x7bf66130, sender=Some, self=0x7bff37b0)(ObjectiveC.UIStoryboardSegue, sender : Swift.ImplicitlyUnwrappedOptional<Swift.AnyObject>) -> () + 880 at ViewController.swift:53 frame #2: 0x000917b6 FootprintinWorld @ objc FootprintinWorld.ViewController.prepareForSegue(FootprintinWorld.ViewController)(ObjectiveC.UIStoryboardSegue,sender:Swift.ImplicitlyUnwrappedOptional) - &gt; ViewController.swift上的()+ 86:0 帧#3:0x010d0b37 UIKit -[UIStoryboardSegueTemplate _perform:] + 199 frame #4: 0x010d0bc5 UIKit - [UIStoryboardSegueTemplate执行:] + 116 帧#5:0x01c927cd libobjc.A.dylib -[NSObject performSelector:withObject:withObject:] + 84 frame #6: 0x00ab523d UIKit - [UIApplication sendAction:to:from:forEvent:] + 99 帧#7:0x00e25840 UIKit -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 139 frame #8: 0x01c927cd libobjc.A.dylib - [NSObject performSelector:withObject:withObject:] + 84 帧#9:0x00ab523d UIKit -[UIApplication sendAction:to:from:forEvent:] + 99 frame #10: 0x00ab51cf UIKit - [UIApplication sendAction:toTarget:fromSender:forEvent:] + 64 帧#11:0x00be8e86 UIKit -[UIControl sendAction:to:forEvent:] + 69 frame #12: 0x00be92a3 UIKit - [UIControl _sendActionsForEvents:withEvent:] + 598 帧#13:0x00be850d UIKit -[UIControl touchesEnded:withEvent:] + 660 frame #14: 0x00b0560a UIKit - [UIWindow _sendTouchesForEvent:] + 874 帧#15:0x00b060e5 UIKit -[UIWindow sendEvent:] + 791 frame #16: 0x00acb549 UIKit - [UIApplication sendEvent:] + 242 帧#17:0x00adb37e UIKit _UIApplicationHandleEventFromQueueEvent + 20690 frame #18: 0x00aafb19 UIKit _ UIApplicationHandleEventQueue + 2206 帧#19:0x001c41df CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15 frame #20: 0x001b9ced CoreFoundation __ CFRunLoopDoSources0 + 253 帧#21:0x001b9248 CoreFoundation __CFRunLoopRun + 952 frame #22: 0x001b8bcb CoreFoundation CFRunLoopRunSpecific + 443 帧#23:0x001b89fb CoreFoundation CFRunLoopRunInMode + 123 frame #24: 0x0403424f GraphicsServices GSEventRunModal + 192 帧#25:0x0403408c GraphicsServices GSEventRun + 104 frame #26: 0x00ab38b6 UIKit UIApplicationMain + 1526 帧#27:0x0009a93e在AppDelegate.swift上的FootprintinWorld top_level_code + 78 at AppDelegate.swift:12 frame #28: 0x0009a97b FootprintinWorld main + 43:0 帧#29:0x023e9ac9 libdyld.dylib`start + 1
更新 该bug已被发现。错误是由行:

引起的
var tableView: TableViewController = segue.destinationViewController as TableViewController

虽然我仍然不知道为什么xcode告诉我,坏的exec后来发生了两行......

原因是我将TableViewController嵌入到导航控制器中。因此,应用程序无法将导航控制器转换为TableViewController(即我的基于UITableViewController的自定义子类)。为了解决这个问题,我只是从导航控制器中打开TableViewController。事实证明,如果TableViewController被来自导航控制器的segue推动,那么它将自动具有导航栏按钮的区域。因此在这种情况下不需要另外的导航控制器。

1 个答案:

答案 0 :(得分:0)

检查一下:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let controller = segue.destinationViewController as NameOFYourController
}

你错了,你肯定有一个UITableViewController的子类。把它当作那个控制器。希望这有助于.. :))