playgroundbook 0.6.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -45
  3. data/lib/renderer/chapter_collator.rb +24 -1
  4. data/lib/renderer/page_parser.rb +24 -2
  5. data/lib/renderer/page_writer.rb +20 -1
  6. data/lib/renderer/playgroundbook_renderer.rb +25 -5
  7. data/lib/version.rb +1 -1
  8. metadata +2 -46
  9. data/.gitignore +0 -50
  10. data/.rspec +0 -2
  11. data/.rubocop.yml +0 -112
  12. data/.ruby-version +0 -1
  13. data/Changelog.md +0 -37
  14. data/CodeOfConduct.md +0 -50
  15. data/Community.md +0 -52
  16. data/Gemfile +0 -13
  17. data/Gemfile.lock +0 -102
  18. data/Guardfile +0 -14
  19. data/Rakefile +0 -8
  20. data/playgroundbook.gemspec +0 -21
  21. data/spec/fixtures/Starter.playgroundbook/Contents/Chapters/Chapter1.playgroundchapter/Manifest.plist +0 -15
  22. data/spec/fixtures/Starter.playgroundbook/Contents/Chapters/Chapter1.playgroundchapter/Pages/Page1.playgroundpage/Contents.swift +0 -8
  23. data/spec/fixtures/Starter.playgroundbook/Contents/Chapters/Chapter1.playgroundchapter/Pages/Page1.playgroundpage/Manifest.plist +0 -12
  24. data/spec/fixtures/Starter.playgroundbook/Contents/Chapters/Chapter1.playgroundchapter/Pages/Page2.playgroundpage/Contents.swift +0 -8
  25. data/spec/fixtures/Starter.playgroundbook/Contents/Chapters/Chapter1.playgroundchapter/Pages/Page2.playgroundpage/Manifest.plist +0 -12
  26. data/spec/fixtures/Starter.playgroundbook/Contents/Manifest.plist +0 -20
  27. data/spec/fixtures/assets/file.jpeg +0 -0
  28. data/spec/fixtures/book.yml +0 -6
  29. data/spec/fixtures/test_chapter.playground/Contents.swift +0 -17
  30. data/spec/fixtures/test_chapter.playground/contents.xcplayground +0 -4
  31. data/spec/fixtures/wrapper/destination/swift_at_artsy_1.swift +0 -200
  32. data/spec/fixtures/wrapper/source/Swift-at-Artsy.playground/Contents.swift +0 -199
  33. data/spec/fixtures/wrapper/source/Swift-at-Artsy.playground/contents.xcplayground +0 -4
  34. data/spec/fixtures/wrapper/source/swift_at_artsy_1.md +0 -183
  35. data/spec/linter/chapter_linter_spec.rb +0 -30
  36. data/spec/linter/chapter_manifest_linter_spec.rb +0 -40
  37. data/spec/linter/contents_linter_spec.rb +0 -18
  38. data/spec/linter/cutscene_page_linter_spec.rb +0 -14
  39. data/spec/linter/cutscene_page_manifest_linter_spec.rb +0 -63
  40. data/spec/linter/manfiest_linter_spec.rb +0 -71
  41. data/spec/linter/page_linter_spec.rb +0 -19
  42. data/spec/linter/page_manifest_linter_spec.rb +0 -43
  43. data/spec/linter/playgroundbook_lint_spec.rb +0 -38
  44. data/spec/linter/root_manifest_linter_spec.rb +0 -35
  45. data/spec/renderer/chapter_collator_spec.rb +0 -70
  46. data/spec/renderer/contents_manfiest_generator_spec.rb +0 -41
  47. data/spec/renderer/glossary_generator_spec.rb +0 -54
  48. data/spec/renderer/page_processor_spec.rb +0 -86
  49. data/spec/renderer/page_writer_spec.rb +0 -70
  50. data/spec/renderer/playgroundbook_renderer_spec.rb +0 -122
  51. data/spec/spec_helper.rb +0 -85
  52. data/spec/wrapper/markdown_wrapper_spec.rb +0 -90
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- ruby-2.1.1
data/Changelog.md DELETED
@@ -1,37 +0,0 @@
1
- # Current Master
2
-
3
- # 0.6.0
4
-
5
- - Adds basic support for Swift Package Manager dependencies. See [#50](https://github.com/playgroundbooks/playgroundbook/pull/50).
6
-
7
- # 0.5.0
8
-
9
- - Embeds images inside the local Playground when converting from markdown. See [#30](https://github.com/ashfurrow/playgroundbook/pull/30).
10
-
11
- # 0.4.0
12
-
13
- - Support converting a random Markdown file into a playground. See [#29](https://github.com/ashfurrow/playgroundbook/pull/29).
14
-
15
-
16
- # 0.3.0
17
-
18
- - Glossary support. See [#18](https://github.com/ashfurrow/playgroundbook/issues/18).
19
- - Removes newlines surrounding markdown blocks. See [#20](https://github.com/ashfurrow/playgroundbook/issues/20).
20
-
21
- # 0.2.1
22
-
23
- - Support for cover images. See [#16](https://github.com/ashfurrow/playgroundbook/issues/16).
24
-
25
- # 0.2.0
26
-
27
- - Book rendering.
28
- - Book linting moved to subcommand.
29
-
30
- # 0.1.0
31
-
32
- - Adds support for linting manifest file optional values. See [#3](https://github.com/ashfurrow/playground-book-lint/issues/3). - @rpowelll
33
- - Adds support for playground cutscene linting. See [#9](https://github.com/ashfurrow/playground-book-lint/issues/9). - @rpowelll
34
-
35
- # 0.0.1
36
-
37
- - Initial release.
data/CodeOfConduct.md DELETED
@@ -1,50 +0,0 @@
1
- # Contributor Code of Conduct
2
-
3
- As contributors and maintainers of this project, and in the interest of
4
- fostering an open and welcoming community, we pledge to respect all people who
5
- contribute through reporting issues, posting feature requests, updating
6
- documentation, submitting pull requests or patches, and other activities.
7
-
8
- We are committed to making participation in this project a harassment-free
9
- experience for everyone, regardless of level of experience, gender, gender
10
- identity and expression, sexual orientation, disability, personal appearance,
11
- body size, race, ethnicity, age, religion, or nationality.
12
-
13
- Examples of unacceptable behavior by participants include:
14
-
15
- * The use of sexualized language or imagery
16
- * Personal attacks
17
- * Trolling or insulting/derogatory comments
18
- * Public or private harassment
19
- * Publishing other's private information, such as physical or electronic
20
- addresses, without explicit permission
21
- * Other unethical or unprofessional conduct
22
-
23
- Project maintainers have the right and responsibility to remove, edit, or
24
- reject comments, commits, code, wiki edits, issues, and other contributions
25
- that are not aligned to this Code of Conduct, or to ban temporarily or
26
- permanently any contributor for other behaviors that they deem inappropriate,
27
- threatening, offensive, or harmful.
28
-
29
- By adopting this Code of Conduct, project maintainers commit themselves to
30
- fairly and consistently applying these principles to every aspect of managing
31
- this project. Project maintainers who do not follow or enforce the Code of
32
- Conduct may be permanently removed from the project team.
33
-
34
- This code of conduct applies both within project spaces and in public spaces
35
- when an individual is representing the project or its community.
36
-
37
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
- reported by contacting a project maintainer at [ash@ashfurrow.com](mailto:ash@ashfurrow.com) or [orta.therox@gmail.com](mailto:orta.therox@gmail.com). All
39
- complaints will be reviewed and investigated and will result in a response that
40
- is deemed necessary and appropriate to the circumstances. Maintainers are
41
- obligated to maintain confidentiality with regard to the reporter of an
42
- incident.
43
-
44
-
45
- This Code of Conduct is adapted from the [Contributor Covenant][homepage],
46
- version 1.3.0, available at
47
- [http://contributor-covenant.org/version/1/3/0/][version]
48
-
49
- [homepage]: http://contributor-covenant.org
50
- [version]: http://contributor-covenant.org/version/1/3/0/
data/Community.md DELETED
@@ -1,52 +0,0 @@
1
- # Community Continuity Guidelines v2.0.0
2
-
3
- As the creators, and maintainers of this project, we want to ensure that the project lives and continues to grow. Not blocked by any singular person's computer time. One of the simplest ways of doing this is by encouraging a larger set of shallow contributors. Through this, we hope to mitigate the problems of a project that needs updates but there's no-one who has the power to do so.
4
-
5
- #### Ownership
6
-
7
- If you get a merged Pull Request, regardless of content (typos, code, doc fixes), then you're eligible for push access to this organization. This is checked for on pull request merges and an invite to the organization is sent via GitHub.
8
-
9
- Offhand, it's easy to imagine that this would make code quality suffer, but in reality it offers fresh perspectives to the codebase and encourages ownership from people who are depending on the project. If you are building a project that relies on this codebase, then you probably have the skills to improve it and offer valuable feedback.
10
-
11
- Everyone comes in with their own perspective on what a project could/should look like, and encouraging discussion can help expose good ideas sooner.
12
-
13
- #### Why do we give out push access?
14
-
15
- It can be overwhelming to offered the chance to wipe the source code for a project. Don't worry, we don't let you push to master. All code is peer-reviewed, and we have the convention that someone other than the submitter should merge non-trivial pull requests.
16
-
17
- As an organization contributor, you can merge other people's pull requests, or other contributors can merge yours. You won't be assigned a pull request, but you're welcome to jump in and take a code review on topics that interest you.
18
-
19
- This project is not continuously deployed, there is space for debate after review too. Offering everyone the chance to revert, or make an amending pull request. If it feels right, merge.
20
-
21
- #### How can we help you get comfortable contributing?
22
-
23
- It's normal for a first pull request to be a potential fix for a problem, and moving on from there to helping the project's direction can be difficult. We try to help contributors cross that barrier by offering good first step issues. These issues can be fixed without feeling like you're stepping on toes. Ideally, these are non-critical issues that are well defined. They will be purposely avoided by mature contributors to the project. To make space for others.
24
-
25
- We aim to keep all project discussion inside GitHub issues. This is to make sure valuable discussion is accessible via search. If you have questions about how to use the library, or how the project is running - GitHub issues are the goto tool for this project.
26
-
27
- #### Our expectations on you as a contributor
28
-
29
- To quote [@alloy](https://github.com/alloy) from [this issue](https://github.com/Moya/Moya/issues/135):
30
-
31
- > Don't ever feel bad for not contributing to open source.
32
-
33
- We want contributors to provide ideas, keep the ship shipping and to take some of the load from others. It is non-obligatory; we’re here to get things done in an enjoyable way. :trophy:
34
-
35
- The fact that you'll have push access will allow you to:
36
-
37
- - Avoid having to fork the project if you want to submit other pull requests as you'll be able to create branches directly on the project.
38
- - Help triage issues, merge pull requests.
39
- - Pick up the project if other maintainers move their focus elsewhere.
40
-
41
- It's up to you to use those superpowers or not though 😉
42
-
43
- If someone submits a pull request that's not perfect, and you are reviewing, it's better to think about the PR's motivation rather than the specific implementation. Having braces on the wrong line should not be a blocker. Though we do want to keep test coverage high, we will work with you to figure that out together.
44
-
45
- #### What about if you have problems that cannot be discussed in a public issue?
46
-
47
- Both [Ash Furrow](https://github.com/ashfurrow) and [Orta Therox](https://github.com/orta) have contactable emails on their GitHub profiles, and are happy to talk about any problems.
48
-
49
- #### Where can I get more info about this document?
50
-
51
- The original source of this document can be found at https://github.com/moya/contributors.
52
-
data/Gemfile DELETED
@@ -1,13 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gem "rake"
4
- gem "guard-rspec"
5
- gem "mocha"
6
- gem "bacon"
7
- gem "mocha-on-bacon"
8
- gem "prettybacon"
9
- gem "fakefs"
10
- gem "rubocop", "~> 0.41.2", require: false
11
-
12
- require "pp" # https://github.com/defunkt/fakefs/issues/99
13
- gemspec
data/Gemfile.lock DELETED
@@ -1,102 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- playgroundbook (0.4.0)
5
- colored (~> 1.2)
6
- cork (~> 0.1)
7
- plist (~> 3.2)
8
-
9
- GEM
10
- remote: https://rubygems.org/
11
- specs:
12
- ast (2.3.0)
13
- bacon (1.2.0)
14
- coderay (1.1.1)
15
- colored (1.2)
16
- cork (0.2.0)
17
- colored (~> 1.2)
18
- diff-lcs (1.2.5)
19
- fakefs (0.9.0)
20
- ffi (1.9.10)
21
- formatador (0.2.5)
22
- guard (2.14.0)
23
- formatador (>= 0.2.4)
24
- listen (>= 2.7, < 4.0)
25
- lumberjack (~> 1.0)
26
- nenv (~> 0.1)
27
- notiffany (~> 0.0)
28
- pry (>= 0.9.12)
29
- shellany (~> 0.0)
30
- thor (>= 0.18.1)
31
- guard-compat (1.2.1)
32
- guard-rspec (4.7.2)
33
- guard (~> 2.1)
34
- guard-compat (~> 1.1)
35
- rspec (>= 2.99.0, < 4.0)
36
- listen (3.0.7)
37
- rb-fsevent (>= 0.9.3)
38
- rb-inotify (>= 0.9.7)
39
- lumberjack (1.0.10)
40
- metaclass (0.0.4)
41
- method_source (0.8.2)
42
- mocha (1.1.0)
43
- metaclass (~> 0.0.1)
44
- mocha-on-bacon (0.2.2)
45
- mocha (>= 0.13.0)
46
- nenv (0.3.0)
47
- notiffany (0.1.0)
48
- nenv (~> 0.1)
49
- shellany (~> 0.0)
50
- parser (2.3.1.2)
51
- ast (~> 2.2)
52
- plist (3.2.0)
53
- powerpack (0.1.1)
54
- prettybacon (0.0.2)
55
- bacon (~> 1.2)
56
- pry (0.10.3)
57
- coderay (~> 1.1.0)
58
- method_source (~> 0.8.1)
59
- slop (~> 3.4)
60
- rainbow (2.1.0)
61
- rake (11.2.2)
62
- rb-fsevent (0.9.7)
63
- rb-inotify (0.9.7)
64
- ffi (>= 0.5.0)
65
- rspec (3.5.0)
66
- rspec-core (~> 3.5.0)
67
- rspec-expectations (~> 3.5.0)
68
- rspec-mocks (~> 3.5.0)
69
- rspec-core (3.5.1)
70
- rspec-support (~> 3.5.0)
71
- rspec-expectations (3.5.0)
72
- diff-lcs (>= 1.2.0, < 2.0)
73
- rspec-support (~> 3.5.0)
74
- rspec-mocks (3.5.0)
75
- diff-lcs (>= 1.2.0, < 2.0)
76
- rspec-support (~> 3.5.0)
77
- rspec-support (3.5.0)
78
- rubocop (0.41.2)
79
- parser (>= 2.3.1.1, < 3.0)
80
- powerpack (~> 0.1)
81
- rainbow (>= 1.99.1, < 3.0)
82
- ruby-progressbar (~> 1.7)
83
- unicode-display_width (~> 1.0, >= 1.0.1)
84
- ruby-progressbar (1.8.1)
85
- shellany (0.0.1)
86
- slop (3.6.0)
87
- thor (0.19.1)
88
- unicode-display_width (1.1.0)
89
-
90
- PLATFORMS
91
- ruby
92
-
93
- DEPENDENCIES
94
- bacon
95
- fakefs
96
- guard-rspec
97
- mocha
98
- mocha-on-bacon
99
- playgroundbook!
100
- prettybacon
101
- rake
102
- rubocop (~> 0.41.2)
data/Guardfile DELETED
@@ -1,14 +0,0 @@
1
- guard :rspec, cmd: "bundle exec rspec" do
2
- require "guard/rspec/dsl"
3
- dsl = Guard::RSpec::Dsl.new(self)
4
-
5
- # RSpec files
6
- rspec = dsl.rspec
7
- watch(rspec.spec_helper) { rspec.spec_dir }
8
- watch(rspec.spec_support) { rspec.spec_dir }
9
- watch(rspec.spec_files)
10
-
11
- # Ruby files
12
- ruby = dsl.ruby
13
- dsl.watch_spec_files_for(ruby.lib_files)
14
- end
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
- require "rubocop/rake_task"
4
-
5
- RSpec::Core::RakeTask.new(:spec)
6
- RuboCop::RakeTask.new
7
-
8
- task default: :spec
@@ -1,21 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path("../lib", __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "version"
5
-
6
- Gem::Specification.new do |s|
7
- s.name = "playgroundbook"
8
- s.version = Playgroundbook::VERSION
9
- s.licenses = ["MIT"]
10
- s.summary = "Lints/renders Swift Playground books."
11
- s.description = "Tooks for Swift Playground books on iOS, a renderer and a linter."
12
- s.authors = ["Ash Furrow"]
13
- s.homepage = "https://github.com/ashfurrow/playground-book-lint"
14
- s.email = "ash@ashfurrow.com"
15
- s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
16
- s.require_paths = ["lib"]
17
- s.executables = ["playgroundbook"]
18
- s.add_runtime_dependency "plist", "~> 3.2"
19
- s.add_runtime_dependency "colored", "~> 1.2"
20
- s.add_runtime_dependency "cork", "~> 0.1"
21
- end
@@ -1,15 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
- <plist version="1.0">
4
- <dict>
5
- <key>Version</key>
6
- <string>1.0</string>
7
- <key>Name</key>
8
- <string>Sample Chapter</string>
9
- <key>Pages</key>
10
- <array>
11
- <string>Page1.playgroundpage</string>
12
- <string>Page2.playgroundpage</string>
13
- </array>
14
- </dict>
15
- </plist>
@@ -1,8 +0,0 @@
1
- /*
2
- Copyright (C) 2016 Apple Inc. All Rights Reserved.
3
- See LICENSE.txt for this sample’s licensing information.
4
-
5
- This is an example playground page.
6
- */
7
-
8
- print("Hello World")
@@ -1,12 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
- <plist version="1.0">
4
- <dict>
5
- <key>Version</key>
6
- <string>1.0</string>
7
- <key>Name</key>
8
- <string>Example Page</string>
9
- <key>LiveViewMode</key>
10
- <string>HiddenByDefault</string>
11
- </dict>
12
- </plist>
@@ -1,8 +0,0 @@
1
- /*
2
- Copyright (C) 2016 Apple Inc. All Rights Reserved.
3
- See LICENSE.txt for this sample’s licensing information.
4
-
5
- This is a second example page.
6
- */
7
-
8
- print("Hello World")
@@ -1,12 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
- <plist version="1.0">
4
- <dict>
5
- <key>Version</key>
6
- <string>1.0</string>
7
- <key>Name</key>
8
- <string>Second Example Page</string>
9
- <key>LiveViewMode</key>
10
- <string>HiddenByDefault</string>
11
- </dict>
12
- </plist>
@@ -1,20 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
- <plist version="1.0">
4
- <dict>
5
- <key>Version</key>
6
- <string>1.0</string>
7
- <key>ContentVersion</key>
8
- <string>1.0</string>
9
- <key>Name</key>
10
- <string>Starter</string>
11
- <key>ContentIdentifier</key>
12
- <string>com.example.apple-samplecode.Starter</string>
13
- <key>DeploymentTarget</key>
14
- <string>ios10.0</string>
15
- <key>Chapters</key>
16
- <array>
17
- <string>Chapter1.playgroundchapter</string>
18
- </array>
19
- </dict>
20
- </plist>
Binary file
@@ -1,6 +0,0 @@
1
- name: Testing book
2
- identifier: com.ashfurrow.example
3
- resources: assets
4
- cover: file.jpeg
5
- chapters:
6
- - name: test_chapter
@@ -1,17 +0,0 @@
1
- import UIKit
2
-
3
- public var str = "Hello, playground"
4
-
5
- public func sharedFunc() {
6
- print("This should be accessible to all pages.")
7
- }
8
-
9
- //// Page 1
10
-
11
- str = "Yo, it's page 1."
12
- sharedFunc()
13
-
14
- //// Page 2
15
-
16
- sharedFunc()
17
- str = "Page 2 awww yeah."
@@ -1,4 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2
- <playground version='5.0' target-platform='ios'>
3
- <timeline fileName='timeline.xctimeline'/>
4
- </playground>
@@ -1,200 +0,0 @@
1
- import Foundation
2
- /*:
3
- (Note: a screen recording of a presentation of this material is [available on YouTube](https://github.com/artsy/Swift-at-Artsy/tree/master/Beginners/Lesson%20One).)
4
-
5
- Hey! So we're going to be looking at Swift from the perspective of learning programming. This means you get to ask awkward questions and I struggle to verbalise concepts that are the programming equivalent of muscle memory. It'll be fun!
6
-
7
- To start off we need to get to a point where we can write any code. We're going to assume that you already have a copy of Xcode 7 installed. With that, let's get started!
8
-
9
- Open Xcode and you'll get this Welcome dialogue.
10
-
11
- ![Xcode Welcome Screen](img/welcome.png)
12
-
13
- We'll be using _playgrounds_ to learn Swift. Click "Get started with a playground", give it a name, and make sure the platform is set to **OS X**.
14
-
15
- ![Creating a new playground](img/newplayground.png)
16
-
17
- Awesome. Xcode will create an open the playground for you. It'll look something like this.
18
-
19
- ![Empty Playground](img/emptyplayground.png)
20
-
21
- The large pane on the left is where we're going to write code. On the right is where the results of our code are displayed. You can see that there's already one line of code written for us, `var str = "Hello, playground"`, and the results pane says `"Hello, playground"`. Neat.
22
-
23
-
24
- Normally for things I work with, the code we are writing gets transformed from something we, as humans, kind-of understand to something a machine understands via a compiler. Let's stick with the idea that it's basically a translator from human to machine. Normally its a big upfront conversion, for example Eigen can take 5 minutes to compile.
25
-
26
- We're going to use Playgrounds, which is more like a back and forth conversation with the compiler, so it's much faster to write code and test things out. These are called REPLs.
27
-
28
- What this means is whatever you type will be run by Xcode and the results will be shown on the right. When Xcode is compiling code (whenever you change text), the top bar of the window will change to say "Running" and will show a little spinner.
29
-
30
- Let's start by saying what _isn't_ code. You see the the first line, how it is greyed out? Well that's a comment. Comments are annotations in the code that are not ran by the compiler. We'll be using comments occasionally to skip bits of code, or to remind us what something is.
31
-
32
- OK let's write some code, let's try typing some things.
33
-
34
- */
35
-
36
- 1 + 2
37
-
38
- 1 / 2
39
- 1.0 / 2.0
40
-
41
- "Hello, Artsy"
42
- /*:
43
-
44
- You can see the results in the results pane:
45
-
46
- ![Results](img/results.png)
47
-
48
- In programming, we want to work with abstractions. So let's make our first variable. A variable holds a value that you can refer to later, you can name them (almost) whatever you want.
49
-
50
- Variables are themselves an abstract concept, and it's normal for beginners to take time getting comfortable with them. In essence, a variable is a way to store something in a place that you can refer to later. There are a few good reasons to do this, but the most common one is that we need to refer to the same value more than once.
51
-
52
- On an artist's Artsy page, we show their name more than once. It's useful to be able to store the artists name in a variable _once_ and then refer to that variable multiple times to show the user an artist's page.
53
-
54
- We're going to make a simple calculator with 3 variables. What I'd like to do is define a variable called `a` and `b` then add them together to make `c`. This is a simple example, but it's about foundations.
55
-
56
- */
57
-
58
- var a = 2
59
- var b = 3
60
-
61
- var c = a + b
62
- /*:
63
-
64
- In this case, you can see that we're referring back to `a` and `b` as variables, instead of the numbers `2` and `3`. This is a simple example, and with more experience, you'll get more comfortable.
65
-
66
- Variables all have a _type_. Some variables are numbers, and some are strings, and some are other things, too! Numbers are often `Int`, or an integer (basically a round number: 1, 2, 3, -500, 401). Swift is a **strongly-typed language**, which means that types matter a lot. Swift won't let you confuse one type for another. Even though we haven't _told_ Swift what the type of our variables are, the compiler figures it out automatically.
67
-
68
- In Swift, `:` specifies a type. So the following two lines are equivalent.
69
-
70
- */
71
-
72
- var myString = "Hello, Artsy"
73
- var myString: String = "Hello, Artsy"
74
- /*:
75
-
76
- In this case we're dealing with a collection of letters, that combined, we call a string. This is common terminology in most programming languages, think of it as a string of letters.
77
-
78
- */
79
-
80
- var myString: String = 1
81
- /*:
82
-
83
- Swift will complain and say that `'Int' is not convertible to 'String'`. This is Swift's compile-time type checking. It prevents bugs from happening in apps at runtime by giving you errors before-hand. Swift's primary goal is safety against crashes in apps.
84
-
85
- When we're writing code, very often we want to print out some useful information for someone to read. We're going to use this to show that some of our code is running. So lets add a print statement saying what the value of `c` is:
86
-
87
- */
88
-
89
- print(c)
90
- /*:
91
-
92
- With `print` working, we can start doing some logic. We want to start checking how big our `c` is, and just like in English, we'll use an `if` statement:
93
-
94
- */
95
-
96
- if c > 6 {
97
- print("Pretty big c you got there.")
98
- }
99
- /*:
100
-
101
- Let's unpack what we're seeing here. `if [something]` - what's happening here is that the bit after the `if` is checking to see if `c` is greater than the number (`Int`) `6`. If it is, then it will run the code inside the braces.
102
-
103
- `if` statements are fundamentally about doing some code only under certain circumstances. For example, Artsy only shows the "Inquire on Artwork" button **if** that artwork can be inquired upon. There are lots of different places in our code where it's useful to do something only if a condition is met.
104
-
105
- It is common to use a left margin / indentation to indicate that something is inside a braced section. It's not enforced though.
106
-
107
- */
108
-
109
- if c > 6 {
110
- print("Pretty big c you got there.")
111
- }
112
- /*:
113
-
114
- */
115
-
116
- if c > 6 {
117
- print("Pretty big c you got there.")
118
- }
119
- /*:
120
-
121
- */
122
-
123
- if c > 6 { print("Pretty big c you got there.") }
124
- /*:
125
-
126
- We've got one more thing to go over, and that is loops. Loops repeat a section of code. Computers are really good at doing the same thing over and over, and loops are how programmers tell computers to do that. Repeating code is really useful in the Artsy's artwork pages, specifically in the related artworks section. As programmers, we tell the computer how to add _one_ artwork to the page, and then tell the computer to do that over and over for all the related artworks.
127
-
128
- Let's make a nice simple loop, one that reads like English. For _a_ number in _zero to c_.
129
-
130
- */
131
-
132
- for number in 0...c {
133
- print("hi")
134
- }
135
- /*:
136
-
137
- It has the same behavior that we saw in the `if` example. It will run the thing inside the braces as many times as the value of `c`. Each time it will print out `"hi"`. I think we can expand on this one little bit. The word `number` here is actually a variable, let's make it sound all mathematical and call it `x` instead.
138
-
139
- */
140
-
141
- for x in 0...c {
142
- print("hi")
143
- }
144
- /*:
145
-
146
- Now we know it's a variable, lets print it!
147
-
148
- */
149
-
150
- for x in 0...c {
151
- print(x)
152
- }
153
- /*:
154
-
155
- For loops, often just called "loops", are foundational to programming. In this example above, we've looped over all the numbers from `0` to `c`, but you can loop over other things, too. Say we're making an app at Artsy that shows a bunch of artworks – we might loop over all the artworks to show them on the screen.
156
-
157
- If you hit the little triangle in the bottom left you can see the printed output!
158
-
159
- ----------------
160
-
161
- So let's recap.
162
-
163
- * We've learned a little bit about variables, they are named values that make it easier for us to think about what something represents vs what it is.
164
-
165
- * Then we looked at some `String`s, and showed that you can't change something from a string to a number once you've start using it as a `String`,
166
-
167
- * We then printed some information.
168
-
169
- * After that we looked at adding an `if` statement to add some logic to our code.
170
-
171
- * Finally we wrapped up with a `for in` loop.
172
-
173
- If you're keen, feel free to keep playing in the playground. Try changing things in the loops to see what happens. Is it what you expected to happen? Why or why not?
174
-
175
- What do you think this code would do?
176
-
177
- */
178
-
179
- for number in 0...15 {
180
- if number > 5 {
181
- print("This is greater than five")
182
- }
183
- }
184
- /*:
185
-
186
- What about this?
187
-
188
- */
189
-
190
- for number in 0...15 {
191
- if number > 5 {
192
- print("This is greater than five")
193
- } else {
194
- print("This is less than five")
195
- }
196
- }
197
- /*:
198
-
199
- You can also look at the free Stanford Swift Course: https://www.youtube.com/playlist?list=PLxwBNxx9j4PW4sY-wwBwQos3G6Kn3x6xP
200
- */