Playground and Frameworks

Hello,


Has anybody able to import framework into playgrounds? I have tried everything and not been successful.

Replies

Seen this link?


h ttps://www.pardel.net/blog/2019/swift-3rdparty-playgrounds

I’d like to clarify your specific requirements:

  • Are you using Xcode on the Mac? Or Swift Playgrounds on iOS?

  • And what version?

  • Are you building the framework from source? Or trying to import a binary framework?

  • If you’re building from source, is the framework written in Swift? Or Objective-C? Or something else?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you for your response.


  • Are you using Xcode on the Mac? Or Swift Playgrounds on iOS? (Yes I am using Xcode on the Mac, Xcode 11 - however if this works, I works I would like to use in a swift playground too)
  • And what version? (Xcode 11)
  • Are you building the framework from source? Or trying to import a binary framework? (Building from the source)
  • If you’re building from source, is the framework written in Swift? Or Objective-C? Or something else? (In order to experiment I have tried with Swift, Objective-C using toy examples. None of them work. My application on Xcode is in Swift which calls Objective-C. I am able to package the Swift framework, using the Objective-C framework, however, I am unable to reference that in the Xcode playground)


Any help will be appreciated. Thank you again.

Thanks for all the details. I used them to go through a concrete scenario that I expected to work. I’ve detailed it below. My recommendation is that you go through this yourself, just to get something working. From there you can start tweaking to approach your final goal.

Anyway, here’s what I did:

  1. I started with Xcode 11.3 on macOS 10.15.2.

  2. I created a framework project named TestFramework from the macOS > Framework template.

  3. I added a Swift file with the following contents:

    public struct Test {
        public static func test() {
            print("test")
        }
    }

    .

  4. I built the project, just to make sure it compiles.

  5. I closed the project.

  6. I created a new playground from the macOS > Blank template.

  7. I closed that file for the moment.

  8. I created a new workspace.

  9. I added the framework project to the workspace.

  10. I added the playground to the workspace, making sure to add it to the top level of the workspace, not as a child of the framework.

  11. I choose Product > Build. This builds the framework in the context of the workspace.

  12. I changed the playground code to look like this:

    import TestFramework
    
    
    Test.test()

    .

  13. I ran the playground; it printed

    test
    , showing that the playground was able to successfully call the framework.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you.


I am able to have playground recognize it as you said. If I make changes to the framework and recompile, the playground refuses to recognize it. There is some state in Xocde that it doesnt like.

What happens if you close and re-open the workspace?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Closing the workspace and restarting doesnt work. I tried deleting "Derived Data" as well. Deleting the workspace and starting again too didnt work.

I'm having similar issues. I have a playground in my workspace that I am trying to use to test a framework from the same workspace.


Although I have access to the framework, changes after a particular point in time do not take effect. I've tried reopening the workspace, restarting xcode, cleaning the build folder, deleting derived data. Nothing changes. Even if one of these methods helped, it's no longer an efficient workflow anyway.


I'm using Xcode 11.4 beta.

eskimo's procedure only work (albeit finicky) when my playground is moved under TestFramework. Also, I had to building it for some iPhone simulator (generic iOS device didn't work). Closing xcode and booting it back up breaks this relationship, but it seems to magically link back up in a few minutes.


A cheap trick I found to get it to work after rebooting xcode - make a tiny change in your framework swift file and rebuild and wait 30 seconds or so.