Home > Enterprise >  Beyond bounds exception when setting selectRow() on UIPickerView
Beyond bounds exception when setting selectRow() on UIPickerView

Time:07-05

I have a UIPicker view and which loads an array of Ints as its data source. Selecting a row works fine, however when the app loads I want to have it display a specific item that is read from disk.

Right now I'm just trying to pass in a specific row number as a raw int but at load its throwing an out of bounds error. My guess is that it's trying to select the row before it has loaded its data source but I'm not sure how to fix this.

My main class looks like this:

class ViewController: UIViewController {
    let durations = Array(15...40)
    override func viewDidLoad() {
        super.viewDidLoad()
        durationPickerOut.dataSource  = self
        durationPickerOut.delegate = self
        durationPickerOut.selectRow(1, inComponent: 1, animated: false) // This is throwing the error
    }

// MARK extensions
extension ViewController: UIPickerViewDataSource{
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return durations.count
    }
}
extension ViewController: UIPickerViewDelegate{
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        String(durations[row])
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        cycleDuration = durations[row]
    }
    
}

Once I can select an arbitrary hard coded row then I'll switch it to something dynamic but right now I just want to be able to set it to select a given row. The error thrown is this:

 *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001803f3d70 __exceptionPreprocess   236
    1   libobjc.A.dylib                     0x000000018019814c objc_exception_throw   56
    2   CoreFoundation                      0x00000001804793b4 -[__NSCFString characterAtIndex:].cold.1   0
    3   CoreFoundation                      0x00000001802ebf38 -[__NSArrayM getObjects:range:]   0
    4   UIKitCore                           0x0000000184d1e384 -[UIPickerView _selectRow:inComponent:animated:notify:]   120
    5   MyApp               0x000000010263aff8 $s015MyApp_Privacy_A014ViewControllerC11viewDidLoadyyF   2496
    6   MyApp               0x000000010263b7a4 $s015MyApp_Privacy_A014ViewControllerC11viewDidLoadyyFTo   36
    7   UIKitCore                           0x00000001846342ec -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled]   104
    8   UIKitCore                           0x0000000184638898 -[UIViewController loadViewIfRequired]   1020
    9   UIKitCore                           0x0000000184638c64 -[UIViewController view]   28
    10  UIKitCore                           0x0000000184dca34c -[UIWindow addRootViewControllerViewIfPossible]   176
    11  UIKitCore                           0x0000000184dc9b7c -[UIWindow _updateLayerOrderingAndSetLayerHidden:actionBlock:]   224
    12  UIKitCore                           0x0000000184dca9a4 -[UIWindow _setHidden:forced:]   252
    13  UIKitCore                           0x0000000184ddb3b4 -[UIWindow _mainQueue_makeKeyAndVisible]   52
    14  UIKitCore                           0x0000000185021a20 -[UIWindowScene _makeKeyAndVisibleIfNeeded]   200
    15  UIKitCore                           0x00000001841cc594  [UIScene _sceneForFBSScene:create:withSession:connectionOptions:]   1428
    16  UIKitCore                           0x0000000184d8a408 -[UIApplication _connectUISceneFromFBSScene:transitionContext:]   1268
    17  UIKitCore                           0x0000000184d8a8b0 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:]   312
    18  UIKitCore                           0x000000018485dbb4 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:]   360
    19  FrontBoardServices                  0x0000000186166718 -[FBSScene _callOutQueue_agent_didCreateWithTransitionContext:completion:]   412
    20  FrontBoardServices                  0x0000000186192620 __94-[FBSWorkspaceScenesClient createWithSceneID:groupID:parameters:transitionContext:completion:]_block_invoke.180   100
    21  FrontBoardServices                  0x0000000186174d00 -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:]   232
    22  FrontBoardServices                  0x000000018619223c __94-[FBSWorkspaceScenesClient createWithSceneID:groupID:parameters:transitionContext:completion:]_block_invoke   312
    23  libdispatch.dylib                   0x00000001029f9b94 _dispatch_client_callout   16
    24  libdispatch.dylib                   0x00000001029fce88 _dispatch_block_invoke_direct   256
    25  FrontBoardServices                  0x00000001861b8074 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__   40
    26  FrontBoardServices                  0x00000001861b7f4c -[FBSSerialQueue _targetQueue_performNextIfPossible]   176
    27  FrontBoardServices                  0x00000001861b80a4 -[FBSSerialQueue _performNextFromRunLoopSource]   24
    28  CoreFoundation                      0x0000000180362234 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__   24
    29  CoreFoundation                      0x0000000180362134 __CFRunLoopDoSource0   204
    30  CoreFoundation                      0x0000000180361520 __CFRunLoopDoSources0   348
    31  CoreFoundation                      0x000000018035ba18 __CFRunLoopRun   744
    32  CoreFoundation                      0x000000018035b218 CFRunLoopRunSpecific   572
    33  GraphicsServices                    0x000000018c25f60c GSEventRunModal   160
    34  UIKitCore                           0x0000000184d88a98 -[UIApplication _run]   992
    35  UIKitCore                           0x0000000184d8d634 UIApplicationMain   112
    36  libswiftUIKit.dylib                 0x00000001b6946224 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF   100
    37  MyApp               0x000000010263fa04 $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ   104
    38  MyApp               0x000000010263f98c $s015MyApp_Privacy_A011AppDelegateC5$mainyyFZ   44
    39  MyApp               0x000000010263fa88 main   28
    40  dyld                                0x0000000102975cd8 start_sim   20
    41  ???                                 0x00000001027bd0f4 0x0   4336636148
    42  ???                                 0x464b800000000000 0x0   5065282943396610048
)
libc  abi: terminating with uncaught exception of type NSException
dyld4 config: DYLD_ROOT_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot DYLD_LIBRARY_PATH=/Users/squidgylabs/Library/Developer/Xcode/DerivedData/MyApp_Privacy_MyApp-cnetykaxqecqzpatuvwawzyuqfwr/Build/Products/Debug-iphonesimulator:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libBacktraceRecording.dylib:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libMainThreadChecker.dylib:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib DYLD_FRAMEWORK_PATH=/Users/squidgylabs/Library/Developer/Xcode/DerivedData/MyApp_Privacy_MyApp-cnetykaxqecqzpatuvwawzyuqfwr/Build/Products/Debug-iphonesimulator
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
terminating with uncaught exception of type NSException
CoreSimulator 802.6.1 - Device: iPhone 13 Pro (EC065371-1DB1-48A9-A629-365188E36AAE) - Runtime: iOS 15.5 (19F70) - DeviceType: iPhone 13 Pro
(lldb)  *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001803f3d70 __exceptionPreprocess   236
    1   libobjc.A.dylib                     0x000000018019814c objc_exception_throw   56
    2   CoreFoundation                      0x00000001804793b4 -[__NSCFString characterAtIndex:].cold.1   0
    3   CoreFoundation                      0x00000001802ebf38 -[__NSArrayM getObjects:range:]   0
    4   UIKitCore                           0x0000000184d1e384 -[UIPickerView _selectRow:inComponent:animated:notify:]   120
    5   MyApp               0x000000010263aff8 $s015MyApp_Privacy_A014ViewControllerC11viewDidLoadyyF   2496
    6   MyApp               0x000000010263b7a4 $s015MyApp_Privacy_A014ViewControllerC11viewDidLoadyyFTo   36
    7   UIKitCore                           0x00000001846342ec -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled]   104
    8   UIKitCore                           0x0000000184638898 -[UIViewController loadViewIfRequired]   1020
    9   UIKitCore                           0x0000000184638c64 -[UIViewController view]   28
    10  UIKitCore                           0x0000000184dca34c -[UIWindow addRootViewControllerViewIfPossible]   176
    11  UIKitCore                           0x0000000184dc9b7c -[UIWindow _updateLayerOrderingAndSetLayerHidden:actionBlock:]   224
    12  UIKitCore                           0x0000000184dca9a4 -[UIWindow _setHidden:forced:]   252
    13  UIKitCore                           0x0000000184ddb3b4 -[UIWindow _mainQueue_makeKeyAndVisible]   52
    14  UIKitCore                           0x0000000185021a20 -[UIWindowScene _makeKeyAndVisibleIfNeeded]   200
    15  UIKitCore                           0x00000001841cc594  [UIScene _sceneForFBSScene:create:withSession:connectionOptions:]   1428
    16  UIKitCore                           0x0000000184d8a408 -[UIApplication _connectUISceneFromFBSScene:transitionContext:]   1268
    17  UIKitCore                           0x0000000184d8a8b0 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:]   312
    18  UIKitCore                           0x000000018485dbb4 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:]   360
    19  FrontBoardServices                  0x0000000186166718 -[FBSScene _callOutQueue_agent_didCreateWithTransitionContext:completion:]   412
    20  FrontBoardServices                  0x0000000186192620 __94-[FBSWorkspaceScenesClient createWithSceneID:groupID:parameters:transitionContext:completion:]_block_invoke.180   100
    21  FrontBoardServices                  0x0000000186174d00 -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:]   232
    22  FrontBoardServices                  0x000000018619223c __94-[FBSWorkspaceScenesClient createWithSceneID:groupID:parameters:transitionContext:completion:]_block_invoke   312
    23  libdispatch.dylib                   0x00000001029f9b94 _dispatch_client_callout   16
    24  libdispatch.dylib                   0x00000001029fce88 _dispatch_block_invoke_direct   256
    25  FrontBoardServices                  0x00000001861b8074 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__   40
    26  FrontBoardServices                  0x00000001861b7f4c -[FBSSerialQueue _targetQueue_performNextIfPossible]   176
    27  FrontBoardServices                  0x00000001861b80a4 -[FBSSerialQueue _performNextFromRunLoopSource]   24
    28  CoreFoundation                      0x0000000180362234 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__   24
    29  CoreFoundation                      0x0000000180362134 __CFRunLoopDoSource0   204
    30  CoreFoundation                      0x0000000180361520 __CFRunLoopDoSources0   348
    31  CoreFoundation                      0x000000018035ba18 __CFRunLoopRun   744
    32  CoreFoundation                      0x000000018035b218 CFRunLoopRunSpecific   572
    33  GraphicsServices                    0x000000018c25f60c GSEventRunModal   160
    34  UIKitCore                           0x0000000184d88a98 -[UIApplication _run]   992
    35  UIKitCore                           0x0000000184d8d634 UIApplicationMain   112
    36  libswiftUIKit.dylib                 0x00000001b6946224 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF   100
    37  MyApp               0x000000010263fa04 $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ   104
    38  MyApp               0x000000010263f98c $s015MyApp_Privacy_A011AppDelegateC5$mainyyFZ   44
    39  MyApp               0x000000010263fa88 main   28
    40  dyld                                0x0000000102975cd8 start_sim   20
    41  ???                                 0x00000001027bd0f4 0x0   4336636148
    42  ???                                 0x464b800000000000 0x0   5065282943396610048
)
libc  abi: terminating with uncaught exception of type NSException
dyld4 config: DYLD_ROOT_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot DYLD_LIBRARY_PATH=/Users/squidgylabs/Library/Developer/Xcode/DerivedData/MyApp_Privacy_MyApp-cnetykaxqecqzpatuvwawzyuqfwr/Build/Products/Debug-iphonesimulator:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libBacktraceRecording.dylib:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libMainThreadChecker.dylib:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib DYLD_FRAMEWORK_PATH=/Users/squidgylabs/Library/Developer/Xcode/DerivedData/MyApp_Privacy_MyApp-cnetykaxqecqzpatuvwawzyuqfwr/Build/Products/Debug-iphonesimulator
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
terminating with uncaught exception of type NSException
CoreSimulator 802.6.1 - Device: iPhone 13 Pro (EC065371-1DB1-48A9-A629-365188E36AAE) - Runtime: iOS 15.5 (19F70) - DeviceType: iPhone 13 Pro
(lldb) MyApp

CodePudding user response:

It is as simple as UITableView behaviour, while we are supplying the data source for both UITableView and UIPickerView we use Int value which defines the number of sections, rows to the UITableView and Columns, row to the UIPickerView.

In your case working with UIPickerView, you told one column is required

extension ViewController: UIPickerViewDataSource{
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return durations.count
    }
}

Here is the main focus, while accessing the component and row it will alway starts from 0,1,2...(N-1)

So, now you can fix this by changing the default selecting column to 0 as you are supplying one column

durationPickerOut.selectRow(1, inComponent: 0, animated: false) //<<<<! FIX

CodePudding user response:

It's zero based , meaning row starts with 0 to < N (N is the number of rows) same for component

durationPickerOut.selectRow(0, inComponent: 0, animated: false)  
  • Related