Cannot call value of non-function type 'String'

前端 未结 6 1808
情书的邮戳
情书的邮戳 2021-01-17 07:35

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

The code I have erro

相关标签:
6条回答
  • 2021-01-17 07:40

    Had a similar issue in this code

    array.first { $0 == item }
    

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

    array.first { $0 === item }
    
    0 讨论(0)
  • 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.

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

    0 讨论(0)
  • 2021-01-17 08:01

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

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

    0 讨论(0)
  • 2021-01-17 08:03

    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 {}

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

    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.

    0 讨论(0)
  • 2021-01-17 08:04

    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'

    when calling sqrt. Fixed it by explicitly calling sqrt as shown below. 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)")
            }
        }
    
    0 讨论(0)
  • 2021-01-17 08:06

    Wrap your let statement in if eg:

    if let xxx = yyy {
       ... do something
    }
    
    0 讨论(0)
提交回复
热议问题