Search code examples
swiftxcodeswift3swift-playground

Method available in project but not in playground


I'm new to Swift - using Swift 3, Xcode 8.0 beta 6, Mac OS X 10.11.6. The following line works in my project (it's a simple test case to see if the 'contains' method is working):

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

It works fine in my project, but when I cut and paste it into a playground project, it gives an error:

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

My question is, apart from why this error is being thrown, are there different frameworks/functions/methods etc. being used in playground projects from normal projects? Thanks for your help. Squire.


Solution

  • In Playgrounds you may miss some framework - you need to import framework too, to be able to access its methods:

    like

    import Foundation // responsible for strings for example
    

    or

    import UIKit // responsible for UI elements
    

    ... etc

    enter image description here