Very basic question, but: why can't I put import
statements in the sources folder of a Playground?
It seems that import
statements would be ideal candidates for what to put in sources, because not only do they take some time to compile, but more importantly, they never change.
After I tried to do this, I found that it was as though I hadn't imported the Frameworks at all. Is it an issue of scope? If so, are there any workarounds?
==========
Clarification: I can put import
statements in Sources, but they can't be accessed by the rest of the Playground.
Xcode builds the files in a playground's (or playground page's) Sources
folder as a separate module and then imports those modules silently in the actual playground pages.
By default, modules don't export the symbols from their own imports. That's why an import
statement in a file in the Sources
folder has no effect on the playground page.
You can, however, take advantage of the undocumented @_exported
attribute to change this behavior. This works for me:
// In file in Sources folder
@_exported import UIKit
// In playground page
let v = UIView() // builds without having to import UIKit
Note that @_exported
is unsupported. The syntax may change in the future, or it might go away entirely (though I doubt that).
3 Likes