【问题标题】:swift compiler shows Expected declaration error? [duplicate]swift编译器显示预期的声明错误? [复制]
【发布时间】:2015-08-07 09:00:14
【问题描述】:

当这段代码用AllListViewController编写并运行时,编译器显示Expected Declaration错误:

for list in lists{
    let item = ChecklistItems()
    item.text = "Item for \(list.name))"
    list.items.append(item)
}       

【问题讨论】:

  • 你到底在问什么(我不明白你的问题/问题)?
  • 其实代码是:
  • 对于列表中的列表 { let item = ChecklistItem() item.text = "Item for (list.name)" list.items.append(item) }
  • 我编辑了 yoru 问题,它可能很快就会包含新代码,但请更准确地了解您的问题。另外,也许考虑使用您的母语的 SO 版本(有许多语言的姐妹网站)。
  • 请发布您遇到的实际错误,或者最好是屏幕截图。

标签: ios xcode swift


【解决方案1】:

我认为你的代码在课堂上的错误位置,就像this 问题一样。

所以将它移动到任何函数或viewDidLoad 方法中。

希望它会有所帮助。

【讨论】:

  • 不错的猜测。完全没看到课
  • 工作就像一个魅力!
【解决方案2】:

你有如下图所示的代码:

好像您的代码在函数之外。如果allListViewController 是您的UIViewController 类,其中编写了for 循环代码,请确保代码应位于allListViewController 类的任何函数的主体内。不能在外面。

示例:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    for list in lists{
        let item = ChecklistItems()
        item.text = "Item for (list.name))"
        list.items.append(item)
    }
}

您可以在函数体之外初始化/声明变量(将是全局变量)

【讨论】:

    【解决方案3】:

    当我们在某个函数中添加应该添加的代码然后调用时,会显示error: expected declaration

    因为只有 Property Declaration 应该直接进入 class 。 所以当我们使用任何属性时,它应该发生在某个函数中。

    这是我创建的活动指示器类的示例。

    在外面写代码时出错。

    写在函数里面就可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 2015-07-02
      • 2016-04-21
      • 1970-01-01
      相关资源
      最近更新 更多