简体   繁体   English

无法调用非函数类型“String”的值

[英]Cannot call value of non-function type 'String'

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'无法调用非函数类型“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 中,我有:

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
}

The error is telling you that you are trying to call a String instead of a method (struct constructor in your case). 该错误告诉您正在尝试调用String而不是方法(在您的情况下为struct构造函数)。 You've probably declared a String variable named ILT (uppercase) somewhere else and that's why it fails. 您可能在其他地方声明了一个名为ILT (大写)的String变量,这就是它失败的原因。

Your posted code works fine so the error must be somewhere else in your code. 您发布的代码工作正常,因此错误必须在代码中的其他位置。

Works: 作品:

let works = ["foo", "bar"].first(where: { ($0 == "foo") } )
let works = ["foo", "bar"].first(where: { (_ in true) } )

Fails: 失败:

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

// Cannot call value of a non-function type 'String?'

You must be sure to use the parameter ( $0 or _ in ) in the closure expression. 您必须确保在闭包表达式中使用参数$0_ in )。

Use _ in or $0 to discard or reference the parameter. 使用_ in$0放弃或引用参数。 You cannot simple move directly into the closure body and return true or you will receive this (extremely unhelpful) error. 您不能简单地直接移动到闭包体并返回true否则您将收到此(非常无用的)错误。

Wrap your let statement in if eg: 如果例如:包裹你的let语句:

if let xxx = yyy {
   ... do something
}

Had a similar issue in this code 在此代码中有类似的问题

array.first { $0 == item }

The problem was with $0 not conforming to Equatable protocol. 问题是$0不符合Equatable协议。 In my case it conformed to NSObjectProtocol and simple pointer comparison was enough, so I fixed the issue with 在我的情况下,它符合NSObjectProtocol和简单的指针比较就足够了,所以我解决了这个问题

array.first { $0 === item }

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. 我正在为我的库编写一个处理Scalars的结构,需要一个平方根。 Error was 错误是

Cannot call value of non-function type 'Vsip.Scalar' 无法调用非函数类型'Vsip.Scalar'的值

when calling sqrt. 在调用sqrt时。 Fixed it by explicitly calling sqrt as shown below. 通过显式调用sqrt来修复它,如下所示。 Hope this helps. 希望这可以帮助。

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)")
        }
    }

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: 我最终得到了类似的东西:

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

}

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闭包语法中发现其他一些难以发现的错误。

Check if you haven't missed ?检查你是否没有错过 for optionals.对于选项。

That error can be shown for a different issue in a heavy-loaded schemes, like when I forget to mark optional.该错误可以针对重负载方案中的不同问题显示,例如当我忘记标记为可选时。

services.first(where: { $0.category?.id == categoryID })

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

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