Swift - Playground

文章目录

  • [Swift - Playground](#Swift - Playground)
    • [1. 新建Playground](#1. 新建Playground)
    • [2. View](#2. View)
    • [3. 图片](#3. 图片)
    • [4. ViewController](#4. ViewController)
    • [5. Playground - 多Page](#5. Playground - 多Page)
    • [6. 注释](#6. 注释)
      • [6.1 Playground的注释支持markup语法(与markdown相似)](#6.1 Playground的注释支持markup语法(与markdown相似))
      • [6.1.1 语法](#6.1.1 语法)

Swift - Playground

Playground可以快速预览代码效果,是学习语法的好帮手

1. 新建Playground


  • Command + Shift + Enter:运行整个Playground

  • Shift + Enter:运行截止到某一行代码

    let a = 10
    let b = 20
    var c = a + b
    c += 10
    c += 20
    print(c)

2. View

复制代码
import UIKit
import PlaygroundSupport

let view = UIView()
view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
view.backgroundColor = UIColor.red
PlaygroundPage.current.liveView = view

3. 图片

复制代码
let imageView = UIImageView(image: UIImage(named: "g"))
PlaygroundPage.current.liveView = imageView

4. ViewController

复制代码
let vc = UITableViewController()
vc.view.backgroundColor = UIColor.lightGray
PlaygroundPage.current.liveView = vc

5. Playground - 多Page

Playground可以新建很多个Page,写demo很方便

或者

例如这样:

文件中的结构是这样的:

6. 注释

复制代码
// 单杠注释


/*
 多行注释
 */


/*
 1
 /*
  多行注释嵌套
  */
 2
 */

6.1 Playground的注释支持markup语法(与markdown相似)

6.1.1 语法

复制代码
markup语法
## 二级标题
### 三级标题

// 单行markup
//: # 一级标题

// 多行markup
/*:
 # 学习Swift
 ## 基础语法
 - 变量
 - 常亮
 ## 面相对象
 - 类
 - 属性
 - 方法
 ## 汇编分析
 
 ## 链接
 [苹果官方链接](https://www.apple.com)
 
 ## 无序列表
 - Frist Item
 - Secound Item
 
 ## 有序列表
 1. Frist Item
 2. Secound Item
 
 ## 笔记
 > This is a note
 ---
 
 ## 图片
 ![Logo](logo.png "Local image")
 
 ## 粗体/斜体
 这是**Bold**,这是*Italic*
 */
 
 ## 下一页/上一页
 //: [下一页](@next)
 //: [下一页](@previous)

效果:

  • 开启markup渲染效果:Editor -> Show Rendered Markup
  • 注意:Markup只在Playground中有效

@oubijiexi

相关推荐
敲代码的鱼哇6 小时前
跳转原生系统设置插件 支持安卓/iOS/鸿蒙UTS组件
android·ios·harmonyos
在下历飞雨6 小时前
Kuikly基础之状态管理与数据绑定:让“孤寡”计数器动起来
ios·harmonyos
在下历飞雨6 小时前
Kuikly基础之Kuikly DSL基础组件实战:构建青蛙主界面
ios·harmonyos
鹏多多.7 小时前
flutter-使用fluttertoast制作丰富的高颜值toast
android·前端·flutter·ios
他们都不看好你,偏偏你最不争气10 小时前
【iOS】多界面传值
ios
MaoJiu1 天前
Flutter混合开发:在iOS工程中嵌入Flutter Module
flutter·ios
2501_915921431 天前
小团队如何高效完成 uni-app iOS 上架,从分工到工具组合的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
2501_916008891 天前
uni-app iOS 文件管理与 itools 配合实战,多工具协作的完整流程
android·ios·小程序·https·uni-app·iphone·webview
Digitally1 天前
如何将视频从 iPhone 转移到 Mac
macos·ios·iphone
2501_916007471 天前
uni-app iOS 文件调试常见问题与解决方案:结合 itools、克魔、iMazing 的实战经验
android·ios·小程序·https·uni-app·iphone·webview