在 Swift 中使用未解析的标识符 'present'(Apple 教程)

Use of unresolved identifier 'present' in Swift (Apple tutorial)

我只是按照 Swift 和 Xcode 的官方 Apple 介绍教程制作了一个 FoodTracker 应用程序。 我遇到了这个错误(见标题)。

这是Xcode7.2.1 我导入了 UIKit

这是出现问题的代码。我有所有 class 协议和代表以及一切。

  @IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
    nameTextField.resignFirstResponder()
    // UIImagePickerController is a view controller that lets a user pick media from their library.
    let imagePickerController = UIImagePickerController()
    // Only allow photos to be picked, not taken.
    imagePickerController.sourceType = .PhotoLibrary
    // Make sure ViewController is notified when the user picks an image.
    imagePickerController.delegate = self
    present(imagePickerController, animated: true, completion: nil)
}

我非常感谢任何帮助,因为我刚刚开始学习这门语言!

确保您已将 class 添加到当前目标。您可以通过 canvas 中打开的任何代码编辑器或情节提要旁边的右侧窗格进行确认!您可以从 'file inspector' 确认。选中 'membership targets' 选中当前目标的复选框,如果未选中,则将您的 class 添加到其中!

在你的情况下(你使用的是 xcode 7 -> swift 2)它必须是:

presentViewController(imagePickerController, animated: true, completion: nil)

现在您正在使用 swift3 语法。