簡體   English   中英

在 UIPickerView 上設置 selectRow() 時出現越界異常

[英]Beyond bounds exception when setting selectRow() on UIPickerView

我有一個 UIPicker 視圖,它加載了一個 Ints 數組作為其數據源。 選擇一行可以正常工作,但是當應用程序加載時,我希望它顯示從磁盤讀取的特定項目。

現在,我只是試圖將特定的行號作為原始 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

它是從零開始的,這意味着行從 0 開始到 < N(N 是行數)對於組件相同

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

它就像UITableView的行為一樣簡單,當我們為UITableViewUIPickerView提供數據源時,我們使用Int值來定義部分的數量, UITableView的行數和UIPickerView的行數。

在您使用UIPickerView的情況下,您告訴我們需要一列

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

這是主要焦點,在訪問componentrow ,它將始終從0,1,2...(N-1)開始

因此,現在您可以通過在提供一列時將默認選擇列更改為0來解決此問題

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM