如何使用Swift Playgrounds制作东西

by Harshita Arora

通过Harshita Arora

如何使用Swift Playgrounds制作东西 (How to make something with Swift Playgrounds)

Just a few days ago, I finished my WWDC 2018 scholarship submission. It was so much fun creating Alice in codeLand. This was my first year applying for WWDC scholarships, and I hope to get in!

就在几天前,我完成了WWDC 2018奖学金提交。 在codeLand中创建Alice真是太有趣了。 这是我申请WWDC奖学金的第一年,我希望能加入!

Alice in codeLand is a three-in-one. It’s a hacker/coding simulator that looks like the Mac OS terminal that you can play with offline. It’s also a funny story of a hacker named Alice who tried gaining unauthorized access to Apple’s WWDC portal after succeeding at hacking into toothtube.com (a video sharing platform for toothpaste reviews) and macaroonsarethebest.com (a social platform for macaroon lovers like myself). And finally, it’s a technical demonstration of SQL injections (a hacking technique) for people to learn about information security and the step-by-step process for SQL injections after finding out if a website is vulnerable or not.

codeLand中的Alice是三合一。 这是一个黑客/编码模拟器,看起来像可以离线使用的Mac OS终端。 这也是一个有趣的故事,一个名叫爱丽丝(Alice)的黑客在成功入侵到tubetube.com(一个用于牙膏评论的视频共享平台)和macaroonsarethebest.com(一个针对像我这样的蛋白杏仁饼干爱好者的社交平台)之后,试图获得对Apple WWDC门户的未经授权的访问。 。 最后,这是SQL注入的技术演示(一种黑客技术),人们可以在确定网站是否易受攻击之后,了解信息安全和SQL注入的分步过程。

Since 2017, Apple has put forth the challenge to scholarship applicants of creating an interactive experience in Swift Playgrounds.

自2017年以来,Apple向奖学金申请者提出了在Swift Playgrounds中创建互动体验的挑战。

When I started out, I was pretty confused. I’d never made a thing in Playgrounds before, as I’ve always worked with Xcode projects (that let you create the real iOS apps). I had only ever used Playgrounds when I started learning Swift to learn and code basic programming concepts. The instructors in the my online courses were using it, but those courses were only two weeks long, and I never created any graphics or animations in them.

当我开始的时候,我很困惑。 我以前从未在Playgrounds上做过任何事,因为我一直都在处理Xcode项目(可让您创建真正的iOS应用程序)。 当我开始学习Swift来学习和编码基本编程概念时,我才使用过Playgrounds。 我在线课程中的讲师正在使用它,但是这些课程只有两周之久,而且我从未在其中创建任何图形或动画。

So it took some time to get familiar with Playgrounds. In hindsight, I could have saved half the time had I known where to look for the right answers and how to get started working with them.

因此,花了一些时间来熟悉Playgrounds。 事后看来,如果我知道在哪里寻找正确的答案以及如何开始使用它们,我可以节省一半的时间。

So this post is for all the future WWDC scholarship applicants, because it seems Apple will continue to give this task for the scholarship every year. Even if you have nothing to do with WWDC scholarships, I think Swift Playgrounds is an awesome tool for building and prototyping something quickly.

因此,该职位适用于所有将来的WWDC奖学金申请者,因为苹果似乎每年都会继续为这项奖学金提供这项任务。 即使您与WWDC奖学金无关,我认为Swift Playgrounds还是一个很棒的工具,可以快速构建和制作原型。

Note: This post assumes you have a background in Swift and iOS frameworks like UIKit. If you’ve built iOS apps before, that’d be a huge bonus!

注意:本文假定您具有Swift和iOS框架(如UIKit)的背景。 如果您以前曾构建过iOS应用程序,那将是一笔不小的数目!

Let’s start!

开始吧!

那么什么是Swift Playgrounds,为什么要打扰呢? (So what are Swift Playgrounds, and why bother?)

Swift Playgrounds is an iPad app introduced by Apple in 2014 that helps you learn to code in Swift. It’s also a tool in Xcode to create Playgrounds. You can run Playgrounds created in Xcode on an iPad and vice-versa.

Swift Playgrounds是Apple在2014年推出的iPad应用程序,可帮助您学习Swift编码。 它也是Xcode中用于创建Playgrounds的工具。 您可以在iPad上运行以Xcode创建的Playground,反之亦然。

The difference between the iPad app and the Xcode tool is that on the iPad, the Playgrounds you create can access features like the camera, touch screen, and so on. But while running a Playground on Xcode, you can’t do that. More about Swift Playgrounds here.

iPad应用程序和Xcode工具之间的区别在于,在iPad上,您创建的Playgrounds可以访问相机,触摸屏等功能。 但是在Xcode上运行Playground时,您无法做到这一点。 更多关于Swift Playgrounds的信息 。

While the concepts and examples in this post use Xcode, the Playgrounds can be created and run on the iPad app as well.

尽管本文中的概念和示例使用Xcode,但Playgrounds也可以在iPad应用程序上创建和运行。

一些需要学习的概念 (Some concepts to learn)

1.如何运行/玩游乐场,并开始使用它们 (1. How to run/play Playgrounds, and getting started with them)

You can download open-sourced Playgrounds here, here, and here. In order to play them, click on Assistant Editor in Xcode and select the “Live View” if it’s not automatically selected.

您可以在此处 , 此处和此处下载开源的Playgrounds。 为了播放它们,请在Xcode中单击助手编辑器,如果未自动选择“实时显示”,请选择它。

A live view is where the output or the results of your code are displayed. When you create some object in your code that you want to be displayed in the live view, you need to assign the Live View property of the Playground page to that object.

实时视图是显示代码的输出或结果的位置。 当您在代码中创建要在实时视图中显示的对象时,需要将“操场”页面的“实时视图”属性分配给该对象。

Now, here’s the code to assign the Live View property of the Playground Page to a UIView object. You can also assign either a class or a view controller.

现在,这是将Playground Page的Live View属性分配给UIView对象的代码。 您也可以分配一个类或一个视图控制器。

You’d have to import the framework PlaygroundSupport for that, and UIKit as well.

为此,您必须导入框架PlaygroundSupport和UIKit。

let view = UIView(frame: CGRect(x: 0, y:0, width: 1024, height: 768)
PlaygroundPage.current.live = view

In the navigation pane, you’ll see there’s two folders: Sources (for all the auxiliary code) and Resources (for all image and audio assets).

在导航窗格中,您将看到两个文件夹:Sources(用于所有辅助代码)和Resources(用于所有图像和音频资产)。

From my experience, dragging a Swift file from the Object library doesn’t work in Playgrounds for some reason. Instead, right-click on the Sources folder and click on “New file” to create a new Swift file.

根据我的经验,出于某种原因,从对象库中拖动Swift文件在Playgrounds中不起作用。 相反,右键单击Sources文件夹,然后单击“ New file”以创建一个新的Swift文件。

2.如何创建视图? (2. How do you create a view?)

Playgrounds don’t have Storyboards. You can create a view (UIView) of any size (max 1024 x 768) programmatically.

游乐场没有情节提要。 您可以通过编程创建任意大小(最大1024 x 768)的视图(UIView)。

I created one in the example above.

我在上面的示例中创建了一个。

3.什么是PlaygroundSupport? (3. What is PlaygroundSupport?)

PlaygroundSupport is a framework for doing things like accessing a playground page and managing its execution, managing live views, and sharing and accessing persistent data.

PlaygroundSupport是一个框架,用于执行诸如访问游乐场页面并管理其执行,管理实时视图以及共享和访问持久数据的操作。

Basically, you need to import this framework to be able to assign the live view property of the playground page to an object you created.

基本上,您需要导入此框架,以便能够将游乐场页面的实时视图属性分配给您创建的对象。

4.使用标记创建丰富的文档 (4. Creating Rich Documentation with Markup)

Swift Playgrounds lets you create beautiful documentation (that is easier to read than the regular comments) using a language called Markup.

Swift Playgrounds可让您使用称为标记的语言来创建精美的文档(比常规注释更易于阅读)。

The basic syntax for Markup for rich documentation is as follows:

用于丰富文档的标记的基本语法如下:

  1. Use “//: stuff” for single comments, which if you notice, means just an additional “:” after the // (which is used for regular comments).

    对单个注释使用“ //:stuff”,如果您注意到,则表示在//后仅加一个“:”(用于常规注释)。
  2. Use “/*: stuff */” for multi-line comments.

    对多行注释使用“ / *:stuff * /”。
  3. Prefix a line with a hashtag, for example, “#stuff” to create a heading.

    在带有前缀标签的行之前添加前缀,例如“ #stuff”以创建标题。
  4. Wrap text inside an asterisk, for example, “*stuff*” to display in italics.

    在星号内包裹文本,例如“ * stuff *”以斜体显示。
  5. Wrap text inside two asterisks, for example, “**stuff**” to display in bold.

    在两个星号(例如“ ** stuff **”)内换行,以粗体显示。

After you’ve written the content in the syntax, it’s in the raw markup format. And to display it in the rendered markup format (the real rich documentation), go to the Editor, and under “Playground Settings,” select “Render Documentation.”

用语法写完内容后,它就是原始标记格式。 并以渲染的标记格式(真正的丰富文档)显示它,请转到编辑器,然后在“操场设置”下选择“渲染文档”。

Read more about Markup here.

在此处阅读有关Markup的更多信息。

通过建立一个简单的游乐场开始 (Getting started by building a simple Playground)

Alright, let’s get our hands dirty by writing some code! Let’s create a hacker simulator like hackertyper.com! :-D

好吧,让我们通过编写一些代码来弄脏我们的手! 让我们创建一个像hackertyper.com这样的黑客模拟器! :-D

Let’s start with creating our background for the hacker simulator. Then using the shouldChangeTextIn method, we’ll change the text displayed in the text view to strings from an array with the code we want to be displayed when the user enters any text. This essentially creates a hacker simulator that makes code appear on the screen after pressing any keys.

让我们开始为黑客模拟器创建背景。 然后,使用shouldChangeTextIn方法 ,我们将在文本视图中显示的文本更改为具有数组的字符串,该数组具有我们要在用户输入任何文本时显示的代码。 这实际上创建了一个黑客模拟器,可以在按任意键后使代码显示在屏幕上。

import PlaygroundSupportimport UIKit
let arrayOfStrings = ["Alices-MacBook-Pro:~ Alice$", "override func viewDidLoad() {", "super.viewDidLoad()", "makeBackgroundGradient()", "addGradientToPortfolio()", "addGradientToTopBar()", "addGradientToTopBar()", "setupPopup()", "addTradeButton.titleLabel?.minimumScaleFactor = 0.5;", "addTradeButton.titleLabel?.minimumScaleFactor = 0.5;"]
var variableThatChanges : Int = 0
let frameOfMainView = CGRect(x: 0, y: 0, width: 1024, height: 768)
class MainViewController: UIViewController, UITextViewDelegate {
override func viewDidLoad() {
let view = UITextView(frame: CGRect(x: 0, y:0, width: 1024, height: 768))
self.view.frame = frameOfMainViewview.backgroundColor = .blackview.textColor = .greenview.font = UIFont(name: "Courier", size: 20)view.isEditable = trueview.delegate = self self.view.addSubview(view)
}
func textView(_ textView: UITextView,
shouldChangeTextIn range: NSRange,
replacementText text: String) -> Bool {
if variableThatChanges == (arrayOfStrings.count - 1)
{
variableThatChanges = 0
}
let text = textView.text ?? ""
textView.text = text + "\n" + arrayOfStrings[variableThatChanges]
variableThatChanges += 1
return false
}
}
let master = MainViewController()
master.preferredContentSize = frameOfMainView.size
PlaygroundPage.current.liveView = master

Here’s the code above. Copy-paste it in a Playground. Run the live view, and you’ll see a black background. Click anywhere on the background, and you’ll see a keyboard pop out in the UI. Press any keys on it and it’ll show you the code from the strings.

这是上面的代码。 将其复制粘贴到操场上。 运行实时视图,您将看到黑色背景。 单击背景上的任意位置,您会在用户界面中看到一个弹出的键盘。 按下任何键,它将显示字符串中的代码。

There you go — you’ve made a basic hacker simulator in under 50 lines of code! Now it’s your turn to go get creative with Playgrounds! ;)

随您去吧-您已经用不到50行代码制作了一个基本的黑客模拟器! 现在该轮到您在Playgrounds上发挥创意了! ;)

一些很棒的资源可以查看更多信息 (Some awesome resources to check out to learn more)

  1. Apple documentation.

    Apple文档 。

  2. WWDC session videos. Check out this, this, this, and this, in that order.

    WWDC会议视频。 按此顺序检查this , this , this和this 。

  3. Playgrounds in depth at RWDevCon 2017.

    在RWDevCon 2017上的游乐场。

  4. The Kaseys video

    Kaseys视频

  5. Code tutsplus tutorial

    代码tutsplus教程

  6. WWDC 2017 and WWDC 2018 submissions. Also, awesome Swift Playgrounds to check out and get ideas from.

    WWDC 2017和WWDC 2018提交。 此外, 很棒的Swift Playgrounds可以签出并获得想法。

  7. A short chapter from a great book.

    一本好书中的一小章。

That’s it for now! Follow me on Medium or Twitter. Download an app I created! Check out an awesome startup I’m advising! :)

现在就这样! 在Medium或Twitter上关注我。 下载我创建的应用 ! 看看我建议的一家很棒的创业公司 ! :)

翻译自: https://www.freecodecamp.org/news/how-to-make-something-with-swift-playgrounds-33e560b84184/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/394469.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2018-2019 20165208 网络对抗 Exp3 免杀原理与实践

目录 2018-2019 20165208 网络对抗 Exp3 免杀原理与实践实验内容基础问题回答实践过程记录任务一:正确使用免杀工具或技巧任务二:通过组合应用各种技术实现恶意代码免杀任务三:用另一电脑实测,在杀软开启的情况下,可运…

k均值例子 数据挖掘_人工智能、数据挖掘、机器学习和深度学习的关系

一、人工智能人工智能是计算机科学的一个分支,它企图了解智能的实质,并生产出一种新的能以人类智能相似的方式做出反应的智能机器。实际应用比如:机器视觉,指纹识别,人脸识别,视网膜识别,虹膜识…

hive中sql使用英文分号

hql只要遇见分号则认识是语句的EOF,所以对于分号,需要用“\“转义。 例如: insert overwrite table test_json_map select {"accountid":"1_:\;11"}, t.map_col from t where dt 2017-08-08 limit 1; 或者用”\073&qu…

软件系统换服务器地址,天正软件客户端修改服务器地址

天正软件客户端修改服务器地址 内容精选换一换如果IP经过NAT/WAF,则只能获取到NAT/WAF转化后的IP地址,无法获取到NAT/WAF前的IP地址。如果客户端为容器,只能获取到容器所在主机的IP地址,无法获取容器的IP。四层监听器(TCP/UDP)开启…

orcale可视化建立用户_建立动态可视化的新方法

orcale可视化建立用户by Sushrut Shivaswamy通过Sushrut Shivaswamy 建立动态可视化的新方法 (A new way of building dynamic visualisations) The Flux architecture gained popularity after Facebook adopted it. It’s a way of managing the state of React components …

leetcode剑指 Offer 47. 礼物的最大价值(动态规划)

在一个 m*n 的棋盘的每一格都放有一个礼物,每个礼物都有一定的价值(价值大于 0)。你可以从棋盘的左上角开始拿格子里的礼物,并每次向右或者向下移动一格、直到到达棋盘的右下角。给定一个棋盘及其上面的礼物的价值,请计…

atoi()函数

原型&#xff1a;int atoi &#xff08;const char *nptr&#xff09; 用法&#xff1a;#include <stdlib.h> 功能&#xff1a;将字符串转换成整型数&#xff1b;atoi()会扫描参数nptr字符串&#xff0c;跳过前面的空格字符&#xff0c;直到遇上数字或正负号才开始做…

python socket.error: [Errno 24] Too many open files

以openwrt AR9331开发板为例&#xff0c;socket连接到1019个就报错 “python socket.error: [Errno 24] Too many open files” 1.查看开发板socket默认连接个数rootTijio:~# ulimit -m1024 2.修改socket连接个数&#xff0c;以root用户运行以下命令rootTijio:~# ulimit -HSn 1…

选中下拉列表显示全部数据_小白都能学会的多级下拉列表,让你的Excel效率提升百倍...

私信回复关键词【工具】&#xff0c;获取Excel高效小工具合集&#xff01;让你的Excel效率开挂~你有没有遇到过这样的场景&#xff1f;收集上来的各部门工作进度表&#xff0c;里面的答案五花八门。即使在表头上进行提示规范&#xff0c;手动输入也十分低效。有没有什么办法能够…

你不知道的javascript(上卷)----读书笔记

<!DOCTYPE html> <html><head><meta charset"utf-8"><title>你不知道的javascript&#xff08;上卷&#xff09;</title> </head><body><script type"text/javascript">/*//9、this 的全面解析this…

lightgbm 数据不平衡_不平衡数据下的机器学习(下)

本文从不平衡学习的基础概念和问题定义出发&#xff0c;介绍了几类常见的不平衡学习算法和部分研究成果。总体来说&#xff0c;不平衡学习是一个很广阔的研究领域&#xff0c;但受笔者能力和篇幅的限制&#xff0c;本文仅对其中部分内容做了简单概述&#xff0c;有兴趣深入学习…

netty实现高性能文件服务器,通用文件服务组件(Netty实现版本)

本文所述文件服务组件在笔者此前一篇文章中已有阐述(基于netty的文件上传下载组件)&#xff0c;不过本文将基于之前这个实现再次进行升级改造&#xff0c;利用基于注解的方式进行自动装配。1. 简介1.1 Netty简介Netty是一个异步事件驱动的网络应用程序框架&#xff0c;用于快速…

leetcode343. 整数拆分(动态规划)

给定一个正整数 n&#xff0c;将其拆分为至少两个正整数的和&#xff0c;并使这些整数的乘积最大化。 返回你可以获得的最大乘积。 示例 1: 输入: 2 输出: 1 解释: 2 1 1, 1 1 1。 解题思路 组成整数两个数可以进一步拆分&#xff0c;所以可以运用到动态规划&#xff0c…

爱前端2018全栈大前端_启动2018年前端工具调查

爱前端2018全栈大前端by Ashley Watson-Nolan通过阿什利沃森-诺兰 启动2018年前端工具调查 (Launching the Front-End Tooling Survey 2018) The Front-End Tooling Survey is back to gather input and shed some light on the tooling habits across the web development in…

PHP 小数点保留两位【转】

最近在做统计这一块内容&#xff0c;接触关于数字的数据比较多&#xff0c; 用到了三个函数来是 数字保留小数后 N 位&#xff1b; 接下来简单的介绍一下三个函数&#xff1a; 1、number_format echo number_format("5000000")."<br>"; echo number_…

华为杯数学建模2020获奖名单_我校在2020年全国大学生数学建模竞赛中再获佳绩(内附获奖名单)...

# 近日&#xff0c;从全国大学生数学建模竞赛组委会获悉&#xff0c;我校在2020年全国大学生数学建模竞赛中获得5项国家二等奖。国家奖获奖数量在全国农林院校中并列排名第二。在黑龙江省高校中位居第三名。1你的喜悦&#xff0c;我们的欣慰# 2020年全国大学生数学建模竞赛于9月…

Python 面试总结

公司面试&#xff1a; 1&#xff0c;说说项目都用到了什么技术&#xff1f; 2&#xff0c;mysql索引的种类&#xff1f; 3&#xff0c;索引建多有什么不好&#xff1f; 4&#xff0c;mysql的引擎有什么&#xff1f; 5&#xff0c;redis是单线程还是多线程的? 6, redis的持久化…

医疗器械监管系统总结

算算时间毕业也两个多月了&#xff0c;心也慢慢塌下来了&#xff0c;少了些许的浮躁&#xff0c;增加了些许的淡定&#xff0c;同时也添加了一些不好的习气&#xff0c;“混”&#xff0c;混日子的混&#xff0c;生活慢慢的缺少了激情&#xff0c;伴随着和她渐渐的疏远。昨天听…

fn映射 mac 键盘_【新鲜评测】高颜值、低延迟、多模式跨平台办公神器-米物蓝牙键盘...

首先&#xff0c;要例行感谢糖纸众测&#xff0c;感谢麻麻酱给我这次评测机会。大家可以微信搜索【糖纸】小程序免费体验更多科技产品。小米旗下生态链公司米物最近出了一款蓝牙双模键盘&#xff0c;它作为一个85全键键盘&#xff0c;可以完成蓝牙和USB转换&#xff0c;以及PC和…

leetcode413. 等差数列划分(动态规划)

数组 A 包含 N 个数&#xff0c;且索引从0开始。数组 A 的一个子数组划分为数组 (P, Q)&#xff0c;P 与 Q 是整数且满足 0<P<Q<N 。 如果满足以下条件&#xff0c;则称子数组(P, Q)为等差数组&#xff1a; 元素 A[P], A[p 1], …, A[Q - 1], A[Q] 是等差的。并且 …