Can Swift playgrounds see other source files in the same project?

前端 未结 4 856
终归单人心
终归单人心 2020-12-29 02:00

I created the most simple custom class in a separate Swift file in my project:

class Foo
{
    init()
    {
        println(\"I made a foo.\")
    }
}


        
相关标签:
4条回答
  • 2020-12-29 02:04

    There's two ways to use your project's code in a Playground

    Playground's Sources Folder

    Yes, in Xcode 6.3 Beta 3 (and hopefully, into the future):

    Playgrounds are now represented within Xcode as a bundle with a disclosure triangle that reveals Resources and Sources folders when clicked. These folders contain additional content that is easily accessible from your playground’s main Swift code. To see these folders, choose View > Navigators > Show Project Navigator (or just hit Command-1).

    Open up a new playground and hit cmd + 1 to see the left pane, then drag files into the source folder to use within the playground.

    Note:

    The files in the source folder are compiled to a framework which means if you want classes, functions, etc. to be accessible in the playground, they must be explicitly marked as public.

    public class VisibleClass {
    }
    
    class InvisibleClass {
    }
    

    Source: release blog

    Compile Project Into Framework

    1. Move project to workspace if it isn't already. (File -> Save as Workspace) will do the trick
    2. Add framework target to your project
    3. Build framework
    4. Make sure files you want to access are added to your framework target
    5. Add Playground to workspace (NOT the project)
    6. Use @testable import YourFrameworkName
    7. Access code in playground

    I made a write up here that goes into a bit more detail if you want to check it out.

    0 讨论(0)
  • 2020-12-29 02:05

    They cannot. Playgrounds are self-contained. This will hopefully change in the future.

    Edit: As of Xcode 6.3, Playgrounds can now contain supporting code. They still cannot see other code in the same project, but code can be added to the support folder of a Playground that can be used from within the playground. See the Swift blog for more info.

    0 讨论(0)
  • 2020-12-29 02:07

    Yes. I started by just adding a class file in the Sources directory. I made everything public:

    • class
    • init
    • members

    After much trying, nothing worked. The XCode crashed and after reopening it all worked like a charm.

    Using Sources/Dna.swift in Playground

    0 讨论(0)
  • 2020-12-29 02:27

    In Xcode 10's Project Navigator:

    1. Add the source code file to the playground's Sources folder.
    2. Drag the file from the playground's Sources folder to the desired location in the project (you should see the little "plus in a circle" icon appear.
    3. End the drag and then in the Add File dialog uncheck "copy if needed"

    The source file now "lives" in the playground package; the Project refers to it (you can verify that with the File Inspector).

    I tried it the other way around: file lives in project folder with reference in playground's Sources folder but it didn't work; I ended up with two copies of the source code file.

    0 讨论(0)
提交回复
热议问题