Realm + Xcode Playgrounds?

Since Xcode 12 playgrounds can now import Swift packages I thought that it would open some possibilities for Realm. First use case I wanted to test is creating a database (to bundle with my other project) inside of Playground. Here is what I’ve done to see If I could even use Realm from Playground:

  1. Created new Xcode storyboard project.

  2. Added Playground at projects root level.

  3. Added Realm with Swift Dependency Manager.

  4. Added this code to the Playground:

    import UIKit
    import RealmSwift

    print(Realm.Configuration.defaultConfiguration.fileURL!)

  5. Built project.

  6. Tried to run Playground.

What I’m gettin is those errors:
Error 1:
error: MyPlayground.playground:4:7: error: cannot find 'Realm' in scope print(Realm.Configuration.defaultConfiguration.fileURL!)

Error 2 (I think it’s not critical?)
Swift package target 'Realm' is linked as a static library by 'RealmPlayground124' and 'RealmSwift'. This will result in duplication of library code.

The steps I tried are based on this article.

Did I do something wrong? Is that issue happening on my machine only or is it replicable? Is this an issue with Xcode or Realm? (I’ve tried exact same thing with SwiftyJSON and it worked in Playground)

(Tried this both on Xcode 12.4 and 12.5 beta with Realm 10.5.2)

We got a similar issue reported Cannot import RealmSwift in Playground · Issue #7117 · realm/realm-swift · GitHub. The team is looking into it. Please follow the issue on GitHub to get updates.

In Xcode 13.2 you can create a Swift Playgrounds App project and add Realm via SPM. Although this works on a Mac it will give warnings stating This Swift Playgrounds project depends on 3 targets containing non-Swift source code, and will therefore not be buildable in Swift Playgrounds. meaning it won’t work on an iPad. The issue you are encountering here should be fixed by SPM : Missing required modules: 'Realm.Private', 'Realm' · Issue #7266 · realm/realm-swift · GitHub and will allow Realm to be used in Playgrounds inside Xcode projects.