美文网首页Xcode
Swift-PlayGround简介

Swift-PlayGround简介

作者: _成 | 来源:发表于2017-07-04 17:10 被阅读267次

    Playground是啥?

    ​ Swift Playground是苹果发布于2016年6月14日的一款轻量级的交互编程应用,特点:

    1. 实时预览代码的效果
    2. 可进行快速的原型开发和试错

    基础部分

    模块划分

    首先,我们先来熟悉下Playground的IDE,和其他工程一样,Playground也支持模块划分!


    屏幕快照 2017-05-26 上午12.19.20.png

    ​ 通常工程划分如下:

    1. MyPlayground

      运行上文提到的代码的地方

    2. Sources 目录

    源文件文件夹,存放类,通常情况下,我们直接在 Playground 上面写代码,编译器会实时编译我们代码,并将结果显示出来。但会产生一个问题,如果我们写了一个函数,或者自定义了一个 view,这部分代码一般情况下是不会变的,而编译器却会一次又一次地去编译这些代码,最终的结果就是导致效率的低下。如果放到此目录下的源文件会被编译成模块(module)并自动导入到 Playground 中,并且这个编译只会进行一次(或者我们对该目录下的文件进行修改的时候),而非每次编译。 这将会大大提高代码执行的效率。

    1. Resources

      资源文件夹,图片,plist等资源文件存放目录

    使用Playground

    打开XCode->Get started with a playground,新建一个Playground,当我们把鼠标移动到辅助窗口的“hello playground”上面的时候,这时候的实时效果如下

    屏幕快照 2017-05-26 上午1.06.48.png

    注意到最右侧的两个按钮,其中:

    屏幕快照 2017-05-26 上午1.07.48.png

    ​ 1表示Quick Look。点击的时候可以预览这个变量的值或者视图(View/ViewController)的效果。

    ​ 2表示Show Result。选中后,代码的下一行inline的显示对应的值。

    实时预览视图效果,需打开Assistant editor

    ass.jpg

    关于实时编译

    想重新运行动画只需要改变playground中的任何一处即可,也可以点击在playground底部的按钮,如果不想playground进行自动编译,则长按按钮 选择 Manually Run

    屏幕快照 2017-05-26 上午12.14.15.png

    实时编译效果预览

    添加一段代码到playground中,并运行代码

    //动画
    let containerView = UIView(frame: CGRect(x:0, y:0, width:700, height:700));
    
    containerView.backgroundColor = UIColor.white;
    
    var square = UIView.init(frame: CGRect(x: 0, y: 0, width: 50, height: 50));
    square.backgroundColor = UIColor.blue;
    
    containerView.addSubview(square)
    
    UIView.animate(withDuration: 2.0) {
        square.backgroundColor = UIColor.red
        square.frame = CGRect(x: 200, y: 400, width: 100, height: 100)
    };
    
    PlaygroundPage.current.liveView = containerView;
    

    效果如下图

    QQ20170607-172850-HD.gif

    结合项目实际调试

    在Swift项目进行代码调试有如下两种方法:

    1. 在Swift项目中添加playground,并复制对应代码到playground下,或者将.swift文件添加到Resource目录下,并且对其进行调用

    2. 先创建当前Swift工程的framework,然后再创建playground,并且对当前的framework进行引用

      屏幕快照 2017-06-07 下午5.36.23.png

      若然不勾选动态库的引用,playground则无法导入并且读取项目中的代码

    屏幕快照 2017-06-07 下午5.35.12.png

    但是最重要的问题在于,如果需要调试的代码部分不是直接放在Playground内都必须声明为Public

    public class UCSTestVC: UITableViewController
    {
        override public func viewDidLoad() {
            tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        }
    }
    

    3.网络请求调试

    支持 Markdown

    Playground 支持原生 markdown语法,如果你之前用过markdown,你会发现这些语法非常熟悉。可以有以下两种方式书写marup语言块,就是在普通注释后面加一个冒号。

    一行markup语言

    //: 开启一行markup
    

    markup块

    /*:
    
    */
    

    先看一个常见的用法

    //: [Previous](@previous)
    
    /*:
     # Usage 
     
     ## Unordered List
     - First Item
     - Secound Item
     
     ## ordered List
      1. First Item
      2. Secound Item
     
     ## Note
     > This is a note
     ---
     
     ## Image
     ![](image.png "Local image")
     
     ## Link
     * [How about a link](https://github.com/LeoMobileDeveloper)
     
     ## Bold/italic
     So my name is **Leo**,you notice that Leo is bold, and this word *italic* is italic.
     
     [Go back to Main Page](MainPage)
    */
    

    这时候,选择Editor -> Show Rendered Markup,即可看到运行效果,如下

    ![](pic/屏幕快照 2017-06-07 下午5.57.10.png)

    页面跳转

    Playground 支持三种方式的页面跳转:
    1. 上一页
    2. 下一页
    3. 跳转到指定页

    页面顺序都是根据它们在项目文件中的排序来决定的。

    上一页与下一页的语法:

    //: ["Go to Next Page"](@next)
    
    //: ["Go to Previous Page"](@previous)
    

    与 markdown 中的超链接类似,方括号中的代码要显示的文字,而小括号则代表跳转的目的地。

    指定页跳转:

    //: ["Go to The End"](PageName)
    

    PageName 代表目标页面的名称,如果页面名称中有空格,则需要使用%20来代替,这是 ASCII 中空格的符号。如下:

    //: ["Go to The End"](Last%20Page)
    

    引用

    1. 导入playground到Swift项目工程目录下

      https://useyourloaf.com/blog/adding-playgrounds-to-xcode-projects/

    2. 调试三方库插件 && XCTest

      https://github.com/JohnSundell/TestDrive

      https://github.com/Liquidsoul/XCTestPlayground

    3. swift-summary 官方Playground总结

    4. 2048游戏

    相关文章

      网友评论

        本文标题:Swift-PlayGround简介

        本文链接:https://www.haomeiwen.com/subject/dysxhxtx.html