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

Jam*_*mes 23 struct viewcontroller ios swift

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

我有错误的代码:

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

在我定义的行上ilt.

这是我目前的代码:

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)
Run Code Online (Sandbox Code Playgroud)

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

class ILTViewController: UIViewController {
  // accept the incoming ILT struct
  var ILTitem: ILT!
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

red*_*t84 22

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

您发布的代码工作正常,因此错误必须在代码中的其他位置.

  • 我建议您使用相关代码打开一个新问题,最好是一个自包含的示例项目来重现该问题. (3认同)

Vla*_*lad 5

将 let 语句包含在 if 中,例如:

if let xxx = yyy {
   ... do something
}
Run Code Online (Sandbox Code Playgroud)