方法在项目中可用但在 playground 中不可用

Method available in project but not in playground

我是 Swift 的新手 - 使用 Swift 3,Xcode 8.0 beta 6,Mac OS X 10.11.6。以下行在我的项目中有效(这是一个简单的测试用例,用于查看 'contains' 方法是否有效):

if ("hello John".contains("John")){}

它在我的项目中运行良好,但是当我将其剪切并粘贴到 playground 项目中时,出现错误:

"Value of type 'String' has no member 'contains'".

我的问题是,除了为什么会抛出这个错误之外,游乐场项目中使用的 frameworks/functions/methods 等是否与普通项目不同? 谢谢你的帮助。 乡绅.

在 Playgrounds 中,您可能会错过一些框架 - 您也需要导入框架,才能访问其方法:

喜欢

import Foundation // responsible for strings for example

import UIKit // responsible for UI elements

...等等