-
-
Notifications
You must be signed in to change notification settings - Fork 117
Swift samples #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| //: [For-in](@previous) | ||
|
|
||
| let flag = true | ||
|
|
||
| while (true) { | ||
| print("Hello") | ||
| if flag { break } | ||
| print("World") | ||
| } | ||
|
|
||
| print("There") | ||
|
|
||
|
|
||
| //: [Continue](@next) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| //: [Break](@previous) | ||
|
|
||
| var i = 0 | ||
| while (i < 10) { | ||
| i += 1 | ||
| print("Hello") | ||
| if i == 5 { continue } | ||
| print("World") | ||
| } | ||
|
|
||
| //: [forEach](@next) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| //: [Repeat-While](@previous) | ||
|
|
||
| //: offset - index of element | ||
| let arr = [7, 10, 1, 5, 2] | ||
|
|
||
| for i in arr.enumerated() { | ||
| print(i.offset, i.element) | ||
| } | ||
|
|
||
|
|
||
| //: [For-in](@next) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| //: [Enumerated](@previous) | ||
|
|
||
| let arr = [7, 10, 1, 5, 2] | ||
| for i in arr { | ||
| print(i) | ||
| } | ||
|
|
||
| //: [Break](@next) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| //: stride(from:to:by:) iterating over collection. C-style loop has been removed | ||
| for i in stride(from: 0, to: 10, by: 1) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose it would be better to show how to use decimal loop bounds and increment. E. g. |
||
| print(i) | ||
| } | ||
|
|
||
|
|
||
|
|
||
| //: [While](@next) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| //: [Continue](@previous) | ||
|
|
||
| let a = [7, 10, 1, 5, 2] | ||
|
|
||
| a.forEach { x in | ||
| print (x) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I supposed that space before |
||
| } | ||
|
|
||
| a.forEach { | ||
| print($0) | ||
| } | ||
|
|
||
| [7,10,1,5].forEach { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would advise to add spaces between elements. |
||
| print($0) | ||
| } | ||
|
|
||
|
|
||
| //: [Map](@next) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| //: [ForEach](@previous) | ||
|
|
||
| let a = [7, 10, 1, 5, 2] | ||
|
|
||
| a.map { | ||
| $0 * 2 | ||
| } | ||
|
|
||
| a.map { x in | ||
| x * 2 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| //: [While](@previous) | ||
|
|
||
| //: Note: Do-While has been removed. Repeat-While instead. | ||
| var i = 0 | ||
| repeat { | ||
| i += 1 | ||
| print(i) | ||
| } while(i < 10) | ||
|
|
||
|
|
||
| //: [Enumerated](@next) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| //: [For](@previous) | ||
| var a = 0 | ||
| while (a < 10) { | ||
| a += 1 | ||
| print(a) | ||
| } | ||
| //: [Repeat-While](@next) | ||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
| <playground version='6.0' target-platform='macos' display-mode='raw'> | ||
| <pages> | ||
| <page name='for'/> | ||
| <page name='while'/> | ||
| <page name='repeat-while'/> | ||
| <page name='enumerated'/> | ||
| <page name='for-in'/> | ||
| <page name='Break'/> | ||
| <page name='Continue'/> | ||
| <page name='forEach'/> | ||
| <page name='map'/> | ||
| </pages> | ||
| </playground> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Styling mark: I don't like single line control flow statements, but it's up to you.