ios 无法调用非函数类型“字符串”的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35039756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:21:44  来源:igfitidea点击:

Cannot call value of non-function type 'String'

iosswiftstructviewcontroller

提问by James

I'm trying to pass the ILTItem variable into my ILTViewController, triggered by AppDelegate.swift when the user launches my app via a deeplink.

我试图将 ILTItem 变量传递到我的 ILTViewController 中,当用户通过深层链接启动我的应用程序时,由 AppDelegate.swift 触发。

The code I have errors with:

我有错误的代码:

Cannot call value of non-function type 'String'

无法调用非函数类型“字符串”的值

on the line where I define ilt.

在我定义的行上ilt

Here's the code I have at the moment:

这是我目前拥有的代码:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var ilt = ILT(homeworkID: 1234, title: "History ILT", subject: "History", teacher: "Miss A Smith", teacherCode: "asmith", studentID: 12345, description: "Description....", due: 1450137600, status: "In Progress", hasAttachments: true)
var newVC = ILTViewController()
newVC.ILTitem = ilt
appDelegate.window?.addSubview(newVC.view)

Why could this be? In my ILTViewController class I have:

为什么会这样?在我的 ILTViewController 类中,我有:

class ILTViewController: UIViewController {
  // accept the incoming ILT struct
  var ILTitem: ILT!

IlT Struct Declaration:

ILT 结构声明:

struct ILT {
    let homeworkID: Int
    let title: String
    let subject: String
    let teacher: String
    let teacherCode: String
    let studentID: Int
    let description: String
    let due: Double
    let status: String
    let hasAttachments: Bool
}

回答by redent84

The error is telling you that you are trying to call a Stringinstead of a method (struct constructor in your case). You've probably declared a Stringvariable named ILT(uppercase) somewhere else and that's why it fails.

该错误告诉您,您正在尝试调用 aString而不是方法(在您的情况下为 struct 构造函数)。您可能已经在其他地方声明了一个String名为ILT(大写)的变量,这就是它失败的原因。

Your posted code works fineso the error must be somewhere else in your code.

您发布的代码工作正常,因此错误必须在您的代码中的其他地方。

回答by pkamb

Works:

作品:

let works = ["foo", "bar"].first(where: { (
let fails = ["foo", "bar"].first(where: { (true) } )

// Cannot call value of a non-function type 'String?'
== "foo") } ) let works = ["foo", "bar"].first(where: { (_ in true) } )

Fails:

失败:

array.first { 
array.first { 
if let xxx = yyy {
   ... do something
}
=== item }
== item }

You must be sure to use the parameter($0or _ in) in the closure expression.

您必须确保在闭包表达式中使用参数($0_ in)。

Use _ inor $0to discard or reference the parameter. You cannot simple move directly into the closure body and return trueor you will receive this (extremely unhelpful) error.

使用_ in$0丢弃或引用参数。您不能简单地直接进入闭包主体并返回,true否则您将收到此(非常无用的)错误。

回答by Tim

Had a similar issue in this code

在这段代码中有类似的问题

public var sqrt: Scalar {
        switch self.type {
        case .f:
            return Scalar(sqrtf(self.realf))
        case .d:
            let x = Foundation.sqrt(self.reald)
            return Scalar(x)
        case .cf:
            return Scalar(vsip_csqrt_f(self.vsip_cf))
        case .cd:
            return Scalar(vsip_csqrt_d(self.vsip_cd))
        default:
            precondition(false, "sqrt not supported for type \(self.type)")
        }
    }

The problem was with $0not conforming to Equatableprotocol. In my case it conformed to NSObjectProtocoland simple pointer comparison was enough, so I fixed the issue with

问题是$0不符合Equatable协议。在我的情况下,它符合NSObjectProtocol并且简单的指针比较就足够了,所以我解决了这个问题

filenames.first(where: { ##代码## == filename } ) {

}

回答by Vlad

Wrap your let statement in if eg:

将您的 let 语句包裹在 if 中,例如:

##代码##

回答by JVSIP

So I had a problem with a similar error message. I am writing a structure to handle Scalars for my library and needed a square root. Error was

所以我遇到了类似错误消息的问题。我正在编写一个结构来处理我的库的标量,并且需要一个平方根。错误是

Cannot call value of non-function type 'Vsip.Scalar'

无法调用非函数类型“Vsip.Scalar”的值

when calling sqrt. Fixed it by explicitly calling sqrt as shown below. Hope this helps.

调用 sqrt 时。通过显式调用 sqrt 来修复它,如下所示。希望这可以帮助。

##代码##

回答by pkamb

In messing around with Swift's various closure syntax types + autocomplete I often find myself in a mess of variables, return types, and using too few or too many sets of ()or {}

在处理 Swift 的各种闭包语法类型 + 自动完成时,我经常发现自己陷入了一堆变量、返回类型中,并且使用了太少或太多的(){}

I ended up with something like:

我最终得到了类似的东西:

##代码##

Which was giving the error

哪个给出了错误

Cannot call value of non-function type

不能调用非函数类型的值

Solution was to remove the trailing { }, which is not correct in this form.

解决方案是删除尾随{ },这在这种形式中是不正确的。

Should just be filenames.first(where: { $0 == filename } )

应该只是 filenames.first(where: { $0 == filename } )

Check that you have not incorrectly applied a set of braces to the end of your non-function, etc., or some other hard to spot error in your current chosen Swift closure syntax.

检查您是否没有错误地将一组大括号应用到非函数等的末尾,或者在您当前选择的 Swift 闭包语法中存在其他一些难以发现的错误。