http://blog.diveinedu.cn/playground_uikit_ios/
让Playground支持UIKit框架
Xcode 6新增了Playground用来学习和测试Swift代码。它能够实时的看到代码的效果,包括变量、终端输出甚至是Cocoa框架中的UI控件,而苹果在WWDC 2014上还演示了SpriteKit框架在Playground中的使用。不过默认情况下Playground只支持Mac的Cocoa框架,如果我们想要在里面测试iOS的UIKit框架就会出错。当然,你可以先新建一个iOS的项目,然后在里面创建Playground。
其实不需要这么麻烦,Playground也是一个Bundle形式的存在,里面一共有三个文件:
- contents.xcplayground
- section-1.swift
- timeline.xctimeline
其中contents.xcplayground是这个Playground的配置文件,主要用来说明所使用的平台信息:
1 |
<? xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?> |
2 |
< playground version = '3.0' sdk = 'iphonesimulator' > |
4 |
< code source-file-name = 'section-1.swift' /> |
6 |
< timeline fileName = 'timeline.xctimeline' /> |
其中sdk说明所使用的平台,iphonesimulator表示它可以使用UIKit等iOS上的框架,而默认为macosx。
因此我们只需要在创建Playground后,将sdk修改为iphonesimulator就可以实时的测试iOS里面控件的效果了!
![Snip20140614_1](http://blog.diveinedu.net/wp-content/uploads/2014/06/Snip20140614_1-1024x658.png)