在Swift 4.2迁移后无法识别的选择器发送到实例

问题描述 投票:0回答:1

我刚刚将项目从Swift 3.0迁移到Swift 4.2,然后开始出现此错误。

正在从多个类中调用同一方法,但是只有一个抛出该异常。在Swift迁移过程中,单个类中的任何更改可能导致此问题吗?

2020-03-04 09:53:42.552405-0500湖观察者[13974:1234549]-[Lake_Observer.LocationChangeController reusablePickerViewController:didChooseValue:forCtrl:]:无法识别选择器发送到实例0x1040ca800 2020-03-04 09:53:42.554410-0500Lake Observer [13974:1234549] ***由于未捕获而终止应用程序异常“ NSInvalidArgumentException”,原因:'-[Lake_Observer.LocationChangeControllerreusablePickerViewController:didChooseValue:forCtrl:]:无法识别选择器发送到实例0x1040ca800'

这是发生异常的地方。 (RecorderReusablePicker)

- (void) buttonIsPressed:(UIButton *)paramSender{

        NSMutableArray *retArray = [[NSMutableArray alloc] initWithCapacity:array.count];
        for (int i = 0; i < array.count; i++) {
            [retArray addObject:[NSNumber numberWithInteger:[genericPicker selectedRowInComponent:i]]];
        }
        [callingReference reusablePickerViewController:self didChooseValue:[[RecorderReusableResult alloc] initWithArray:retArray clearValue:NO] forCtrl:callingControlReference];
}

选择器在这里创建。 (LocationChangeController)

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {

    if (textField == editTextWaterBody) {
        let waterbodyArray: NSArray? = NSArray(objects: waterbodyNames)
        self.view.window?.rootViewController?.present(RecorderReusablePicker (dataArray: waterbodyArray as? [Any], widthArray: nil, inInitialIndexArray: [savedWaterbodyIndex], reference: self, andControlRef: editTextWaterBody, andTitle: "Please select your waterbody", showSelection: true, allowPanZoom: false), animated: true, completion: nil)
        return false
    }

    return true
}

以及从选择器返回的方法。

func reusablePickerViewController(_ reusablePickerViewController: RecorderReusablePicker!, didChooseValue retVal: RecorderReusableResult!, forCtrl outCtrl: Any!) {
    reusablePickerViewController.dismiss(animated: true, completion: nil)
}

同样,还有其他一些类具有与Picker创建和返回的方法完全相同的方法,但是只有一种会导致异常。

ios swift swift4.2
1个回答
0
投票

更改

func reusablePickerViewController

@objc func reusablePickerViewController

从Swift 3到Swift 4.2的迁移改变了实例成员如何暴露于Objective-C的规则。在Swift 3中,默认情况下它们是公开的。在Swift 4.2中,您必须显式公开它们;否则,Objective-C无法看到它们-这正是您正在发生的事情。该方法已经存在,但是Objective-C认为它不存在,因此由于无法识别的Selector异常而崩溃。

© www.soinside.com 2019 - 2024. All rights reserved.