Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

The code I have errors with:

Cannot call value of non-function type 'String'

on the line where I define 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:

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

IlT Struct Declaration:

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
}
like image 897
James Avatar asked Jan 27 '16 14:01

James


3 Answers

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.

like image 146
redent84 Avatar answered Oct 15 '22 19:10

redent84


Wrap your let statement in if eg:

if let xxx = yyy {
   ... do something
}
like image 4
Vlad Avatar answered Oct 15 '22 19:10

Vlad


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.

like image 3
pkamb Avatar answered Oct 15 '22 17:10

pkamb