在Uipickerview上设置Selectrow()时,超出了异常

发布于 2025-02-13 02:26:11 字数 14808 浏览 1 评论 0原文

我有一个Uipicker视图,并加载了INT数组作为其数据源。选择一行正常工作,但是当应用程序加载时,我想让它显示从磁盘读取的特定项目。

现在,我只是试图将特定的行号作为RAW INT传递,但在加载其抛出范围错误时。我的猜测是,它正在尝试在加载数据源之前选择该行,但我不确定如何解决此问题。

我的主要课程看起来像这样:

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]
    }
    
}

一旦我可以选择一个任意的硬编码行,然后我将其切换到动态的东西,但是现在我只想将其设置为选择给定的行。 错误的错误是:

 *** 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞梦里开花 2025-02-20 02:26:12

它与uitaiteView行为一样简单,而我们在提供uitableviewuipickerview的数据源时,我们使用int> int定义各节数的值,排为uitaiteView和列,行,uipickerview

在您使用Uipickerview的情况下,您告诉您需要一列

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

,这是主要重点,访问组件 and 它将一直启动来自0,1,2 ...(N-1)

,因此,现在您可以通过更改默认选择列为0来解决此问题,

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

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
旧竹 2025-02-20 02:26:12

它基于零,这意味着行以0至&lt开头; n(n是行的数量)对于组件相同

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

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)  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文