http://rshankar.com/xcode-6-and-playground/

Playground is an interactive work environment that allows you to see the values in the sidebar for the written code. As and when you make changes to your code the sidebar reflects the changed result. Listed below are some examples written using Swift language in Playground

Sum of n numbers

1
2
3
4
5
6
var sum = 0
 
for i in 0...10 {
    sum += i
}
sum

Fibonacci Series

1
2
3
4
5
var fibonacci = 0
var temp1 = 1
var temp2 = 0
 
println(fibonacci)
1
2
3
4
5
6
for j in 0...10 {
    temp2 = fibonacci
    fibonacci += temp1
    temp1 = temp2
    println(fibonacci)
}

In the below screenshot, you can observe that the sidebar displaying the values for the variables.

Similarly the console output displays the message written using println statements And by clicking the Value History option, the timeline feature is displayed for that expression. Console Output, Timeline can be accessed using the Assistant Editor.

Pin the Result

Playground allows to pin the result to the editor window using the Show result option. The playground also allows users the take look at the values using the Quick Look option as shown in the below screenshot. Quick Look can display colours, Strings (plain and attributed), Images, Views, Arrays and Dictionaries, Points, rects, sizes, Bezier Paths, URLs/WebView, Classes and Structs.

Adding images to playground

We can add images to playground by following the below mentioned steps

Step 1: Click View menu and select Show File Inspector and Utilities

Step 2: Under File Inspector, click Full Path option to access location of the playground file.

Step 3: Right click on the file and select Show Package Contents

Step 4: Create a folder with name as Resources

Step 5: Copy the required image file under Resources folder. For this demo, I have copied a file named as funny_image of type PNG.

Go to your Playground file and add the following line of code to access the file.

1
2
3
// Add image to playground.
 
let image = UIImage(named: "funny_image”)

Quick look option should now display the image as shown below.

Playground Utilities

  • XCPShowView – Show live views in timeline.
  • XCPSetExecutionShouldContinueIndefinitely. – Allows execution to continue even after reaching playground’s top level code.
  • XCPCaptureValue – Manually capture values.
  • XCPSharedDataDirectoryPath – Retrieves the directory path which contains the shared data between all playgrounds

In order use the above mentioned playground utilities, we need to import XCPlayground module. Let us see couple of examples for this module.

XCPSHOWVIEW

Add the following code to Playground, which creates a UIView with background colour as Red. And by using XCPShowView, we will add this view to the playground timeline.

1
2
3
4
5
6
7
// Add image to playground.
 
let image = UIImage(named: "funny_image")
import XCPlayground
let demoView = UIView(frame: CGRectMake(0, 0, 250, 250))
demoView.backgroundColor = UIColor.redColor()
XCPShowView("MyView", demoView)

XCPShowView accepts two parameters, an identifier for the View and the actual View itself.

XCPSETEXECUTIONSHOULDCONTINUEINDEFINITELY

Let us see an example of Asynchronous call by calling a web service that returns your IP details.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Making Asynchronous call in Playground
 
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)
 
let url = NSURL(string: "http://www.telize.com/geoip")
 
NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
 
    if error == nil {
        var error:NSError?
        if let result = data {
            if let dict = NSJSONSerialization.JSONObjectWithData(result, options:       NSJSONReadingOptions.AllowFragments, error: &error) as? NSDictionary {
            println(dict)
            } else {
                println("Error Processing data")
            }
        }
    } else {
        println(error.localizedDescription)
    }
}).resume()

You need to make sure the execution is continued until you receive the information from callback method. This is done by adding XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true). The service result will be printed to your console as shown below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"area_code" = 0;
asn = AS24560;
"continent_code" = AS;
country = India;
"country_code" = IN;
"country_code3" = IND;
"dma_code" = 0;
ip = "182.65.61.59";
isp = "Bharti Airtel Ltd., Telemedia Services";
latitude = 20;
longitude = 77;
offset = 5;
timezone = "Asia/Kolkata";
}

Playground Limitations

  • Playground cannot be used for performance testing.
  • Does not support User Interaction.
  • Does not support On-device execution.
  • Cannot use your app or framework code.
  • Does not support custom entitlements.

XCode Playground Overview的更多相关文章

  1. Xcode playground markdown常用语法

    //: **Bold** *Italic* /*: # h1 ## h2 ### h3 #### h4 h5 h6 same as h3 --- --- --- * blank seperate li ...

  2. How to make an HTTP request in Swift

    from: http://stackoverflow.com/questions/24016142/how-to-make-an-http-request-in-swift You can use N ...

  3. Functor and Monad in Swift

    I have been trying to teach myself Functional Programming since late 2013. Many of the concepts are ...

  4. Xcode在playground的quick look框中显示对象自定义视图

    对于一般对象,playground中默认的quick look显示已经够用,比如简单的字符串,Int,或简单的自定义Class等等. 不过对于有些情况,我们需要自定义对象在playground中的显示 ...

  5. Xcode的playground中对于SpriteKit物理对象的更新为何无效

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 为了便于SpriteKit中物理行为的调试,我们可以借助于Xc ...

  6. Xcode打开playground运行很慢或者无法输出的解决办法

    学习Swift最快捷的方法就是创建playground,但有时后运行很慢或者一直显示Running,无法输出. 解决办法 关闭Xcode 在终端里执行2段代码 rm -rf ~/Library/Dev ...

  7. Xcode 打开playground文件的时候提示-Unable to find execution service for selected run destination

    解决办法: step 1: 关闭Xcode (快捷键cmd + q) step 2:在terminal里面运行如下语句 rm -rf ~/Library/Developer/CoreSimulator ...

  8. 对Xcode菜单选项的详细探索(干货)

    本文调研Xcode的版本是 7.1,基本是探索了菜单的每一个按钮.虽然从xcode4一直用到了xcode7,但是一般都只是用了一些基础的功能,说来也惭愧.在一次偶然的机遇突然发现了“显示调用层级”的选 ...

  9. 【转】对 Xcode 菜单选项的详细探索(干货)

    http://www.cocoachina.com/ios/20151204/14480.html 本文调研Xcode的版本是 7.1,基本是探索了菜单的每一个按钮.虽然从xcode4一直用到了xco ...

随机推荐

  1. 领扣[LeetCode]从零开始[使用C++][1,10]

    0.序 以后不做后端开发是不是就用不到C++了?真香.话不多说,我已经躺倒在第一题上了.不贴题目了,持续更新. 1.两数之和 原文:https://www.cnblogs.com/grandyang/ ...

  2. 链上链下交互 以太坊Dapp接口开发

    主要是指的是用NodeJs调用 提供接口供前端使用 用户查询和转账 以太坊Dapp项目 众筹项目 功能需求 路人 查看所有众筹项目, 2 @ OK 根据众筹项目的address获取该众筹的详情 (参与 ...

  3. 使用SetOperations(无序)操作redis

    方法 c参数 s说明 Long add(K key, V... values); K key:集合key V... values:key对应的值 向集合中添加一个或多一个元素 Long remove( ...

  4. MySQL初始4--去重

    更新表中的字段: update 表名 set 字段=新值,… where 条件: UPDATE语法可以用新值更新原有表行中的各列.SET子句指示要修改哪些列和要给予哪些值.WHERE子句指定应更新哪些 ...

  5. lintcode-130-堆化

    130-堆化 给出一个整数数组,堆化操作就是把它变成一个最小堆数组. 对于堆数组A,A[0]是堆的根,并对于每个A[i],A [i * 2 + 1]是A[i]的左儿子并且A[i * 2 + 2]是A[ ...

  6. Java IO 小结

    Java IO 的学习需要明白流设计的体系结构,这样才能在实际需要的时候,通过API文档查阅,快速实现功能.

  7. (age|name|sex)+ 脱离顺序控制 并且能添加多个

    (age|name|sex)+  脱离顺序控制 并且能添加多个

  8. AGC018C Coins (set)

    题目大意: 给出n个人,每个人手里都有xi个金牌,yi个银牌,ci个铜牌. 你需要选出X个人,拿走他们手里的金牌,选出Y个人,拿走他们手里的银牌,选出Z个人,拿走他们手里的铜牌 X+Y+Z = n.并 ...

  9. 洛谷 [FJOI2014]最短路径树问题 解题报告

    [FJOI2014]最短路径树问题 题目描述 给一个包含\(n\)个点,\(m\)条边的无向连通图.从顶点\(1\)出发,往其余所有点分别走一次并返回. 往某一个点走时,选择总长度最短的路径走.若有多 ...

  10. BZOJ 3545 / 洛谷 P4197 Peaks 解题报告

    P4197 Peaks 题目描述 在\(\text{Bytemountains}\)有\(N\)座山峰,每座山峰有他的高度\(h_i\).有些山峰之间有双向道路相连,共\(M\)条路径,每条路径有一个 ...