简体   繁体   English

Swift3.0:不能调用非功能类型的值'((Selector!)-> Bool)!'

[英]Swift3.0: Can not call value of non-functional type'((Selector!)->Bool)!'

I have to migrate my swift 2.x code to Swift 3.0. 我必须将Swift 2.x代码迁移到Swift 3.0。 In this case, I found an issue "Can not call value of non-functional type ((Selector!)-> Bool)!". 在这种情况下,我发现了一个问题“无法调用非功能类型的值((Selector!)-> Bool)!”。 I googled but not found any good solution which solved my problem. 我用谷歌搜索,但没有找到解决我问题的任何好的解决方案。

I have one class, which is written in Objective-C : 我有一门课,用Objective-C编写:

@interface AEngineBool : AEngineData
 @property BOOL value; 
 @end

And one another view controller class, I access this using respondToSelector like this 另一个视图控制器类,我像这样使用reactToSelector访问

 let param: AnyObject = params[0]
 if param.responds(#selector(AEngineBool.value)) {
    let iType: Bool = param.value
// Something
}

and getting an error on param.response() point, which I mention above. 并在我上面提到的param.response()点上出现错误。

If I changed the if (param.responds(to: #selector(getter: AEngineBool.value))), then I am getting an error Ambiguous use for 'value(forKey:)'. 如果我更改了if(param.responds(to:#selector(getter:AEngineBool.value))),那么我会得到错误的“ value(forKey :)”用法。

Ok, it looks like you have 3 problems: 好的,看来您有3个问题:

  1. are using the wrong signature for responds() responds()使用了错误的签名
  2. value is a getter and it should be labelled as so, and value是一个吸气剂,应该这样标记,并且
  3. the 'value' property name is ambiguous. “值”属性名称不明确。

See this: 看到这个:

if param.responds(to: #selector(getter: AEngineBool.value)){
     let iType = param.value // gives error 'value' is ambiguous
     // something?
}

But if i create another BOOL property on your original class, that has a more unique name: 但是,如果我在您的原始类上创建另一个BOOL属性,则该属性将具有更唯一的名称:

@interface AEngineBool : NSObject
@property BOOL value;
@property BOOL uniquePropertyName;
@end

Then this is all good: 那么这一切都很好:

if param.responds(to: #selector(getter: AEngineBool.uniquePropertyName)){
      let iType = param.uniquePropertyName
      // something?
}

So i think value is just to general of a word and collides with value(forKey:) and others... 所以我认为value只是一般性的一句话,并与value(forKey :)和其他冲突...

在此处输入图片说明

also can't figure out the error given by XCode, but i would code around it. 也无法弄清楚XCode给出的错误,但是我会围绕它进行编码。 Would this work for your implementation? 这对您的实施有用吗?

    let param: AnyObject = params[0]
    if param is AEngineBool {
        let iType: Bool = param.value
        // something?
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM