Skip to content

Commit

Permalink
easier to read syntax for highlighted code
Browse files Browse the repository at this point in the history
  • Loading branch information
ebelinski committed Jan 30, 2018
1 parent 2023eaa commit c4a59dc
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 105 deletions.
4 changes: 2 additions & 2 deletions 404.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ title: 404
description: 404 — Page not found
---

{% highlight swift %}
```swift
if let error = error {
print(error.localizedDescription)
}
// Output: "404 - Page not found!"
{% endhighlight %}
```
20 changes: 10 additions & 10 deletions arrays-cheatsheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,48 @@

<h3>Array</h3>

{% highlight swift %}
```swift
var myArr = [1, 3]
myArray.append(5)
{% endhighlight %}
```

<h3>Loop over array</h3>

{% highlight swift %}
```swift
for element in myArr {
print("Element: \(element)")
}
/* Output:
Element: 1
Element: 3
Element: 5 */
{% endhighlight %}
```

<h3>Map</h3>

{% highlight swift %}
```swift
let squaredArr = myArr.map { $0 * $0 }
print(squaredArr)
// Output: [1, 9, 25]
{% endhighlight %}
```

<h3>Filter</h3>

{% highlight swift %}
```swift
let over2 = myArr.filter { $0 > 2 }
print(over2)
// Output: [3, 5]
{% endhighlight %}
```

</div><div class="col-sm-6">

<h3>Sorted</h3>

{% highlight swift %}
```swift
let arr = [4, 28, 2, 1, 99, 25]
let over2 = myArr.sorted()
print(over2)
// Output: [3, 5]
{% endhighlight %}
```

</div></div>
4 changes: 2 additions & 2 deletions design-patterns-cheatsheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

<h3>Model–View–Controller pattern</h3>

{% highlight swift %}
```swift
struct Book {
let title: String
let author: String
}
{% endhighlight %}
```

</div><div class="col-sm-6">

Expand Down
52 changes: 26 additions & 26 deletions swift-cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,123 +8,123 @@ description: A beautiful and clutter-free Swift 4 cheatsheet.

### Constants

{% highlight swift %}
```swift
let myInt = 5
let myString = "5.5"
{% endhighlight %}
```

### Variables

{% highlight swift %}
```swift
var myInt = 5
myInt = myInt + 5
{% endhighlight %}
```

### Type annotations

{% highlight swift %}
```swift
let myInt: Int = 5
let myString: String = "5.5"
{% endhighlight %}
```

### If statement

{% highlight swift %}
```swift
if 5 > 3 {
print("5 is more than 3")
} else {
print("5 is not more than 3")
}
// Output: "5 is more than 3"
{% endhighlight %}
```

### Optionals

{% highlight swift %}
```swift
let myInt: Int? = 5
if let unwrappedInt = myInt {
print("myInt is \(unwrappedInt)")
}
// Output: "myInt is 5"
{% endhighlight %}
```

### Enum

{% highlight swift %}
```swift
enum CompassPoint {
case north, south, east, west
}
var direction: CompassPoint = .north
{% endhighlight %}
```

### Switch statement

{% highlight swift %}
```swift
switch direction {
case .north: print("Going north!")
case .south: print("Going south...")
default: print("Going east or west.")
}
// Output: "Going north!"
{% endhighlight %}
```

{% include closecol.html %}{% include opencol.html size=6 %}

### Function with param and return

{% highlight swift %}
```swift
func square(x: Int) -> Int {
let squaredValue = x * x
return squaredValue
}
{% endhighlight %}
```

### Calling a function

{% highlight swift %}
```swift
let squareOf6 = square(x: 6)
print("Square of 6 is: \(squareOf6)")
// Output: "Square of 6 is: 36"
{% endhighlight %}
```

### Declaring a struct

{% highlight swift %}
```swift
struct MyStruct {
var myInt: Int
let myStr: String
mutating func squareMyInt() {
myInt = myInt * myInt
}
}
{% endhighlight %}
```

### Instantiating a struct

{% highlight swift %}
```swift
var obj = MyStruct(myInt: 5,
myStr: "Hi! 👋")
obj.squareMyInt()
print("\(obj.myStr) \(obj.myInt)")
// Output: "Hi! 👋 25"
{% endhighlight %}
```

### Array

{% highlight swift %}
```swift
var myArr = [1, 3]
myArr.append(5)
{% endhighlight %}
```

### Loop over array

{% highlight swift %}
```swift
var sum = 0
for element in myArr {
sum += element
}
print(sum)
// Output: "9"
{% endhighlight %}
```

{% include closecol.html closerow=true %}
24 changes: 12 additions & 12 deletions swift-closures.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,55 +20,55 @@ Closures can be defined with _closure expression syntax_, which has the general

Using **closure expression syntax**:

{% highlight swift %}
```swift
let sortedInts = [4, 30, 7, 9, 1].sorted(by: { (x: Int, y: Int) -> Bool in
return x < y
})
{% endhighlight %}
```

Because this closure is the last argument of `sorted`, the **parentheses** are optional:

{% highlight swift %}
```swift
let sortedInts = [4, 30, 7, 9, 1].sorted() { (x: Int, y: Int) -> Bool in
return x < y
}
{% endhighlight %}
```

When the argument and/or return types are known, the **type annotations** are optional:

{% highlight swift %}
```swift
let sortedInts = [4, 30, 7, 9, 1].sorted() { x, y in
return x < y
}
{% endhighlight %}
```

Also, closure parameters can be referenced by **position** instead of by name:

{% highlight swift %}
```swift
let sortedInts = [4, 30, 7, 9, 1].sorted() {$0 > $1}
{% endhighlight %}
```

### Closure as a variable

A closure can be stored as a variable and used later. Using closure expression syntax:

{% highlight swift %}
```swift
let myClosure = { (x: Int, y: Int) -> Bool in
return x > y
}
let sortedInts = [4, 30, 7, 9, 1].sorted(by: myClosure)
{% endhighlight %}
```

### Closure as a function

A function is a type of closure, so a closure can be stored as a function to be used later.

{% highlight swift %}
```swift
func myClosure(x: Int, y: Int) -> Bool {
return x > y
}
let sortedInts = [4, 30, 7, 9, 1].sorted(by: myClosure)
{% endhighlight %}
```

### Further reading

Expand Down
Loading

0 comments on commit c4a59dc

Please sign in to comment.