http://www.codeschool.com/blog/2014/12/12/swift-playgrounds-xcplayground/

Swift, Playgrounds, and XCPlayground

Jon Friskics in Development

Swift Playgrounds are a great feature of Xcode 6 that allow you to create a single file to test out code before adding it to your app’s main codebase. Prior to Playgrounds, if you wanted to test out code, you had a couple options — you could create a new git branch off of master of the app you’re working on, or, if you’re like me, you could open a brand new project with the Single View Application template and try to build enough of a base to start testing the feature you want.

Playgrounds reduce the amount of steps between having an idea and prototyping it, but there’s certain things they can’t do right out of the box, like network requests or showing rendered views when you try to draw UIViews.

Fortunately, these features are just a module import away.

XCPlayground

XCPlayground is a new module that has a few methods to help you get the most out of Playgrounds. Adding it to your playground is as simple as adding an import statement right near the default UIKit import, like this:

import UIKit // there by default
import XCPlayground // add this in

XCP EXECUTION SHOULD CONTINUE INDEFINITELY

Let’s say you want to prototype some code that requests an API endpoint and returns some JSON data. The setup for that scenario might look like this:

// create a session object
let session = NSURLSession(
configuration: NSURLSessionConfiguration.defaultSessionConfiguration()) // make a network request for a URL, in this case our endpoint
session.dataTaskWithURL(NSURL(string: "http://localhost:8080/notes")!,
completionHandler: { (taskData, taskResponse, taskError) -> Void in // create an NSArray with the JSON response data
var jsonReadError:NSError? let jsonArray = NSJSONSerialization.JSONObjectWithData(
taskData, options: nil, error: &jsonReadError) as [AnyObject] }).resume()

The problem you’ll run into when you run this code, though, is that your playground execution happens so fast the completion handler won’t have enough time to complete before execution stops. That means that none of the code inside of the handler will run, and you won’t have that JSON response data.

The solution? Call XCPSetExecutionShouldContinueIndefinitely() at the top of your file just below the imports, and set the continueIndefinitely boolean argument to true. This tells your playground it shouldn’t stop right away, and you’ll now see the jsonArrayobject has data that’s been returned from the API.

XCP SHARED DATA DIRECTORY PATH

It’s common to prototype apps with local data first. Often the design of the API that eventually returns real data is being built alongside the app, and not having to immediately account for all of the edge cases that come with asynchronous callbacks can help developers focus on view layout and other core features.

In a normal Xcode project you can easily import files and folders directly, but playgrounds don’t offer the same support. Instead, each time a playground is opened, it’s assigned a new random container buried deep in the /var folder. That container holds a Shared Playground Data folder that is symbolically linked to/Users/HOME_FOLDER/Documents/Shared Playground Data, so anything you put in that folder will be available in your playground.

The XCPSharedDataDirectoryPath string constant always holds a reference to that shared folder, so any files you want to access in your project can be put in that Shared Playground Data folder and accessed in the playground.

A SMARTER SHARED DIRECTORY PATH

Putting all of your files in that shared /Documents subfolder is fine until you’ve got multiple playgrounds, so why not organize all of your files per-playground just like you usually do per-project? There’s no direct support out of the box for this, but you can create a helper method that makes things easier. Here’s the one I use:

func pathToFileInSharedSubfolder(file: String) -> String {
return XCPSharedDataDirectoryPath +
"/" +
NSProcessInfo.processInfo().processName +
"/" +
file
}

NSProcessInfo().processInfo() creates an object that contains tons of information about what Xcode is currently running, which right now is your playground! The processNameproperty of that process is the same as the file name of your playground, so if I haveNetworkPrototype.Playground, then processName will be NetworkPrototype. That gets appended along with a / to the end of that Shared Playground Data folder path. Finally, I append the file name string I passed into the function as an argument called file.

Now, grabbing the contents of a file inside a playground is a little easier. For example, like grabbing a JSON data object from a local JSON file:

let jsonData = NSFileManager.defaultManager().contentsAtPath(
pathToFileInSharedSubfolder("data.json"))!

Or, loading a locally stored image into a UIImageView:

let imageView = UIImageView()
imageView.image = UIImage(contentsOfFile: pathToFileInSharedSubfolder("code-school.png"))

XCP CAPTURE VALUE

Prototyping data operations is nice, but you’re also probably going to want to build up some actual views at some point. For example, take the following playground code:

let view = UIView()
view.frame = CGRectMake(0,0,320,568)
view.backgroundColor = UIColor.lightGrayColor() let imageView = UIImageView()
imageView.frame = CGRectMake(20, 20, 280, 51)
imageView.image = UIImage(contentsOfFile: pathToFileInSharedSubfolder("code-school.png"))
view.addSubview(imageView) let label = UILabel()
label.frame = CGRectMake(0, 100, 320, 30)
label.textAlignment = .Center
label.text = "Welcome!"
view.addSubview(label)

This creates a view and sets the background color to gray, adds an image view that’s sourced from a local file (see the above section), and adds a label with the text “Welcome!”. One way to see that rendered view is to click on the tiny eyeball icon in the debug panel in the playground, but even then you only get to see the view preview once before you have to collapse the panel and get back to coding.

By calling XCPCaptureValue() and passing it an identifier string and a view object, you’ll be able to see that view in the timeline, like this:

...
label.text = "Welcome!"
view.addSubview(label) XCPCaptureValue('mainView', view)

view there is that gray UIView object we created above. If you pair this withXCPExecutionShouldContinueIndefinitely like I described above, you’ll have a continuously updating UIView preview right inside of your playground!

Here’s a zip file that contains the Code School logo and a playground that has all of the code in this post: CodeSchoolProtoPlayground.zip. Let us know what you think about Swift Playgrounds, and if you’d like to see more blog posts on iOS, in the comments section below!

SHARE THIS POST

ABOUT THE AUTHOR

Jon Friskics

Content Producer and Developer at Code School. Enjoys building iOS and web apps, and then figuring out the best way to teach people how to build their own.

View Discussion

Swift, Playgrounds, and XCPlayground的更多相关文章

  1. Swift Playgrounds for mac基础知识介绍

    Swift Playgrounds是一款适用于iPad和Mac的革命性应用程序,它使Swift学习变得互动而有趣.它不需要编码知识,因此非常适合刚开始的学生.使用Swift解决难题,以掌握基本知识.S ...

  2. Swift vs. Objective-C:未来看好 Swift 的十个理由

    Swift vs. Objective-C:未来看好 Swift 的十个理由 是时候使用易入手又全面的Swif语言为iOS和mac OS X做应用开发了. 虽然编程语言不会那么容易消逝,但坚持衰落范例 ...

  3. iOS开发者知识普及,Swift 挑战 Objective-C,谁会笑到最后?

    前言: 目前全球共有超过 7 亿台 iPhone 处于活跃状态,全球约有2000万名 iOS 开发者,这造就了 iOS 作为全球第二大移动设备平台的状态. 虽然安卓系统的全球市场占有率超过 iOS 系 ...

  4. Swift Playground All In One

    Swift Playground All In One Swift 5.3 Playgrounds in Xcode Xcode 11.5 https://developer.apple.com/vi ...

  5. Swift in Action

    Swift in Action Swift Playgrounds https://apps.apple.com/us/app/swift-playgrounds/id1496833156?mt=12 ...

  6. 2016 苹果全球开发者大会(WWDC)

    纵观WWDC 2016开发者大会的全部内容,尽管本次大会没有那些新的产品发布,不过能让各位果粉的肾留到秋天,那也是苹果公司对各位果粉的关爱啊.但是对iOS开发者而言,新发布的技术还是比较不错的.主要内 ...

  7. iOS 单元测试和UI测试教程

    原文:iOS Unit Testing and UI Testing Tutorial 作者:Audrey Tam 译者:kmyhy 编写测试不是为了追求刺激,测试是为了避免你崭新的 App 变成了充 ...

  8. WWDC 2016 盛宴

    转自:http://www.jianshu.com/p/72dd8306c817 整理和维护人:pmstGitHub 链接:WWDC-2016-Feast目前只是整理官方给出的 WWDC 2016 视 ...

  9. The Swift Programming Language 中文翻译版(个人翻新随时跟新)

    The Swift Programming Language --lkvt 本人在2014年6月3日(北京时间)凌晨起来通过网络观看2014年WWDC 苹果公司的发布会有iOS8以及OS X 10.1 ...

随机推荐

  1. restAssured + TestNG测试接口,以下是一个get 请求。

    package Elaine.Test.G.APITest; import org.testng.Assert;import org.testng.annotations.BeforeTest;imp ...

  2. web3无法安装的额解决方案-----yarn命令安装web3

    凡是可以用 JavaScript 来写的应用,最终都会用 JavaScript 来写. --Atwood定律(Jeff Atwood在2007年提出) yarn命令详解 https://yarnpkg ...

  3. C++的几种字符类型

    我们在C学过了char字符类型. 在C++中,char是基本的字符类型,但却不仅仅有这一种字符类型! 类型 含义 该类型数据所占的最小比特位数 char 字符 8位(即可表示28个字符) wchar_ ...

  4. 第16讲——C++中的代码重用

    C++的一个主要目标是促进代码重用.除了我们之前学的公有继承,我们在这一讲将介绍另一种代码重用的方法——类模板.

  5. android-ViewList的通用ViewHold

    在写ViewList的时候要写Adapter的时候,经常大量的代码都是差不多的. 1 ViewHold 2 if(convertView ==null ){}else{} 3 setTag 4 FIn ...

  6. 正则表达式之旅_sed_awk

    谈谈正则表达式这个东西: 我想作为一个程序员,正则表达式大家绝对不陌生. 正则表达式好像一个有限则动机.主要作用是匹配,但是同时因为这个功能,我们可以扩展很多其他用法 像很多语言都引人了正则表达式:j ...

  7. Map.Entry遍历Map

    Map.entry是Java中的一个接口.每一个Map.entry代表一个map对象. 可以通过 Map是java中的接口,Map.Entry是Map的一个内部接口,它表示map中的一个实体(及一个k ...

  8. 机器学习基础知识笔记(一)-- 极大似然估计、高斯混合模型与EM算法

    似然函数 常说的概率是指给定参数后,预测即将发生的事件的可能性.拿硬币这个例子来说,我们已知一枚均匀硬币的正反面概率分别是0.5,要预测抛两次硬币,硬币都朝上的概率: H代表Head,表示头朝上 p( ...

  9. 制作Windows10政府版的小白教程

    制作Windows10政府版的小白教程 https://03k.org/make10entg.html 首先,宿主系统要比操作的系统新,因为低版本dism操作不了: 当然也可以单独下载ADK,提取最新 ...

  10. [USACO06NOV]玉米田Corn Fields

    题面描述 状压dp. 设\(f[i][sta]\)为第\(i\)层状态为\(sta\)的方案数. 然后每次可以枚举上一层的状态以及本层的状态,然后如果不冲突且满足地图的要求,则转移. 时间复杂度\(O ...