Playground is introduced in wwdc2014 by SWIFT. It is literally understood that playground is the playground. In swift, this "Playground" can write code while previewing the effect to realize "What you see is what you write", which makes it self-evident for programmers, compare two images:
Is it cool to simplify the process from step 6 to step 2? In addition to cool, playground can be used in actual development. It works well in two places: it is used to quickly preview the interface control effect and debug complex algorithms.
Now let's get some dry stuff,
Preview imageview
Let imagenames = [nsimagenameuser, nsimagenameuseraccounts, nsimagenameusergroup] Let images = imagenames. map {nsimage (named: $0)}; imageslet image = images [0] Let imageview = nsimageview (frame: nsrect (X: 0, Y: 0, width: 512, height: 512) imageview. image = imageimageview. imagescaling =. imagescaleproportionallyupordown
Preview tableview
Import uikitvar STR = "Hello, playground" class datasource: nsobject, uitableviewdatasource {func tableview (tableview: uitableview !, Numberofrowsinsection section: INT)-> int {return 4} func tableview (tableview: uitableview !, Cellforrowatindexpath indexpath: nsindexpath !) -> Uitableviewcell! {Let ROW = indexpath. Row let style = uitableviewcellstyle. fromraw (ROW) Let cell = uitableviewcell (style: style !, Reuseidentifier: nil) // Let cell = uitableviewcell (style :. default, reuseidentifier: nil) cell. textlabel. TEXT = "text1" If let detailtextlabel = cell. detailtextlabel {detailtextlabel. TEXT = "detail text"} return cell} Let DS = datasource () Let tableview = uitableview (frame: cgrect (X: 0, Y: 0, width: 320, height: 240), style :. plain) // Let tableview = uitableview (frame: cgrectmake (0.0, 0.0, 320.0, 240.0), style :. plain) tableview. datasource = dstableview. reloaddata ()
Data Types supported by Playground:
Limitations of playground:
1. The playground file cannot be directly used in the project, but the code can be copied and pasted into the project.
Q &
1. Failed to import uikit.
Enter in playground
Import uikit
The "no such module uikit" error occurs. The solution is to set platform to IOS (OS X by default)
Open File inspector, as shown in settings