Tosin Afolabi
Tosin Afolabi

Reputation: 151

ContiguousArrayStorage row unrecognized selector sent to instance

I simply add a cell & get a EXC_CRASH

tableView.beginUpdates()
tableView.insertRowsAtIndexPaths([indexPathToAdd], withRowAnimation: .Automatic)
expanded = true
accordionIndex = newAccordionIndex
tableView.endUpdates()

Datasource Delegate

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return scheme.numberOfCells
}

var numberOfCells: Int {
    let count = rowCountHandler!()
    return expanded ? count + 1 : count
}

After debugging, the error is at tableView.endUpdates() It's weird because if the cell already exists & I delete it & add it at a new indexPath, tableView.endUpdates() has no issues.

full method

func handleDoubleTap(tableView: UITableView, withAbsoluteIndex index: Int) {
        if !expanded {

            // Add a cell below the double tapped cell

            let newAccordionIndex = index + 1
            let indexPathToAdd = [NSIndexPath(forRow: newAccordionIndex, inSection: 0)]
            //expanded = true
            //accordionIndex = newAccordionIndex
            //tableView.reloadData()

            tableView.beginUpdates()
            tableView.insertRowsAtIndexPaths([indexPathToAdd], withRowAnimation: .Automatic)
            expanded = true
            accordionIndex = newAccordionIndex
            tableView.endUpdates()

        } else {

            if index == parentIndex { return }

            // Remove the current accordion cell & add a new one
            // below the double tapped cell

            let indexPathToRemove = NSIndexPath(forRow: accordionIndex, inSection: 0)

            let newAccordionIndex = index > accordionIndex ? index : index + 1
            let indexPathToAdd = NSIndexPath(forRow: newAccordionIndex, inSection: 0)

            tableView.beginUpdates()
            tableView.deleteRowsAtIndexPaths([indexPathToRemove], withRowAnimation: .Automatic)
            tableView.insertRowsAtIndexPaths([indexPathToAdd], withRowAnimation: .Automatic)
            accordionIndex = newAccordionIndex
            tableView.endUpdates()
        }
    }

Also i cant find where i call row on an array instance. I'm guessing is some sort of array.

Stack trace is below

2014-10-07 02:01:10.426 HopperBus[37542:9390828] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TtCSs23_ContiguousArrayStorage000000007A8CFA98 row]: unrecognized selector sent to instance 0x7a84d3d0'
*** First throw call stack:
(
    0   CoreFoundation                      0x004fcdf6 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x025b4a97 objc_exception_throw + 44
    2   CoreFoundation                      0x00504a75 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
    3   CoreFoundation                      0x0044d9c7 ___forwarding___ + 1047
    4   CoreFoundation                      0x0044d58e _CF_forwarding_prep_0 + 14
    5   UIKit                               0x01789afc -[UIUpdateItem isSectionOperation] + 52
    6   UIKit                               0x014c011f -[UITableView _endCellAnimationsWithContext:] + 5778
    7   UIKit                               0x014d771f -[UITableView endUpdatesWithContext:] + 51
    8   UIKit                               0x014d774d -[UITableView endUpdates] + 41
    9   HopperBus                           0x001440a1 _TFC9HopperBus17HBAccordionScheme15handleDoubleTapfS0_FTCSo11UITableView17withAbsoluteIndexSi_T_ + 993
    10  HopperBus                           0x0012dd2f _TFC9HopperBus19RouteViewController9tableViewfS0_FTCSo11UITableView23didSelectRowAtIndexPathCSo11NSIndexPath_T_ + 319
    11  HopperBus                           0x0012ddd5 _TToFC9HopperBus19RouteViewController9tableViewfS0_FTCSo11UITableView23didSelectRowAtIndexPathCSo11NSIndexPath_T_ + 101
    12  UIKit                               0x014d94fc -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1559
    13  UIKit                               0x014d96a7 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 285
    14  UIKit                               0x014de9a3 __38-[UITableView touchesEnded:withEvent:]_block_invoke + 43
    15  UIKit                               0x013f362e ___afterCACommitHandler_block_invoke + 15
    16  UIKit                               0x013f35d9 _applyBlockToCFArrayCopiedToStack + 415
    17  UIKit                               0x013f33ee _afterCACommitHandler + 545
    18  CoreFoundation                      0x0041ffbe __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
    19  CoreFoundation                      0x0041ff00 __CFRunLoopDoObservers + 400
    20  CoreFoundation                      0x0041593a __CFRunLoopRun + 1226
    21  CoreFoundation                      0x004151ab CFRunLoopRunSpecific + 443
    22  CoreFoundation                      0x00414fdb CFRunLoopRunInMode + 123
    23  GraphicsServices                    0x06b0e24f GSEventRunModal + 192
    24  GraphicsServices                    0x06b0e08c GSEventRun + 104
    25  UIKit                               0x013c9e16 UIApplicationMain + 1526
    26  HopperBus                           0x000d44fe top_level_code + 78
    27  HopperBus                           0x000d453b main + 43
    28  libdyld.dylib                       0x02d18ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Upvotes: 1

Views: 647

Answers (1)

Tosin Afolabi
Tosin Afolabi

Reputation: 151

notice

let indexPathToAdd = [NSIndexPath(forRow: newAccordionIndex, inSection: 0)] 

so [indexPathToAdd] was an array of an array.

The type checker didnt catch this becacuse it's calling an Objective-C API that doesn't have the strict type checking Swift does. It didn't catch it because insertRowsAtIndexPaths takes an [AnyObject] and [NSIndexPath] could be cast to [AnyObject] so [[NSIndexPath]] passes the test for [AnyObject]

Upvotes: 2

Related Questions