How to reference Swift Playground itself?
Unfortunately, controls in playgrounds are not interactive.
I tried getting around the non-interactivity of playground controls the other day by creating and displaying an NSWindow
for a Mac playground, however, the result was that the window simply flashed behind the playground.
Best that I can tell after some probing into NSProcess
, NSBundle
, NSApplication
and watching the system Activity Monitor, the playground actually compiles your code, loads it into a stub app, executes, the playground captures the results and displays them then the stub app exits or waits around for the next execution. This means by the time you see the results, the code is no longer executing.
If you're curious, you can try the code below in a playground to see what I mean. If you open Activity Monitor and filter on PlaygroundStub_OSX
you can see the process that runs the code launch. It is oftentimes in a "Not responding" state. I've also included a screen capture of the results of the probing portion of the code.
import Cocoa
var app = NSApplication.sharedApplication()
app.hidden
var procInfo = NSProcessInfo.processInfo()
procInfo.processName
procInfo.arguments
var bundle = NSBundle.mainBundle()
bundle.bundlePath
var window = NSWindow(contentRect: NSRect(x: 30, y: 30, width: 400, height: 400), styleMask: NSTitledWindowMask, backing: .Buffered, defer: false)
var view = NSView(frame: NSRect(x: 0, y: 0, width: 300, height: 300))
var textField = NSTextField(frame: NSRect(x: 30, y: 30, width: 100, height: 20))
textField.stringValue = "Test"
view.addSubview(textField)
var button = NSButton(frame: NSRect(x: 30, y: 60, width: 100, height: 30))
button.highlight(true)
var buttonCell:NSButtonCell = button.cell() as NSButtonCell
buttonCell.bezelStyle = NSBezelStyle.RoundedBezelStyle
view.addSubview(button)
window.contentView.addSubview(view)
window.makeKeyAndOrderFront(nil)
UPDATE
I was able to get a semi-functional OS X window in a Swift playground and placed the code in a github repo. I'm not sure if an interactive UIKit based view is possible yet.
How to use `SELF` in swift playground?
There is KVO playground on gitHub, try this one https://github.com/rectalogic/KVOPlayground
or you might interested in this one:
How to reference Swift Playground itself?
Can Swift playgrounds see other source files in the same project?
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.
Swift playgrounds with UIImage
- Open the .playground file in Finder.
- Create a folder called Resources next to it.
- Add any images you want to this folder.
- In the playground press opt-cmd-1 to open the File Inspector. You should see the playground on the right. If you don't have it selected, press cmd-1 to open the Project Navigator and click on the playground file.
- Under 'Resource Path' choose 'Relative To Playground'
- Click the folder icon underneath and choose the Resources folder created earlier.
You should now have a bundle that you can use with the standard NSImage(named:"filename_without_extension"):
Note: Because Xcode will frequently overwrite the .playground folder, I recommend using this method so the resources folder isn't getting constantly deleted and re-created.
Does swift playground support UIKit?
YES, it does!
File: New > File... > iOS > Source > Playground
import UIKit
let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 100))
lbl.text = "Hello StackOverflow!"
Then, save the file. (Or manually run it.) This will trigger the Playground to interpret UI related things. At this point, the word "UILabel" should appear on the right-hand side.
Now, to actually view what you've done, you've got to click on the "Quick View" eye on the right, or the white circle to open it in Assistant Editor:
Here's a screenshot of some basic things with UIImage working, etc.
(EDIT: minor text update to current CGRect syntax -- But, screenshots still show old syntax.)
Related Topics
Draw a Hole in a Rectangle with Spritekit
iOS Facebook Sdk: Login Doesn't Return Email Despite Permissions Granted
How to Detect Absent Network Connection When Setting Firestore Document
Format Realtime Stopwatch Timer to the Hundredth Using Swift
How to Retrieve a Random Object from Firebase Using a Sequential Id
How to Make Keyboard Dismiss When I Press Out of Searchbar on Swift
How to Conform to Nscopying and Implement Copywithzone in Swift 2
Swift: How to Call Cckeyderivationpbkdf from Swift
Method' Is Ambiguous for Type Lookup in This Context, Error in Alamofire
Store a Closure as a Variable in Swift
Set the Size and Position of All Windows on the Screen in Swift
How to Get Random Element from a Set in Swift
Why I Can Change/Reassigned a Constant Value That Instantiated from a Class
Why am I Allowed Method Access Less Restrictive Than Class Access
Implicit Return in a Closure Causing an Error
How to Assign an Optional Binding Parameter in Swiftui
Swift Function with Args... Pass to Another Function with Args
Differencebetween Swift 2.0 Do-Try-Catch and Regular Java/C#/C++ Exceptions