This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension StringProtocol { | |
subscript(_ offset: Int) -> String.Element { | |
if offset >= 0 { | |
self[index(startIndex, offsetBy: offset)] | |
} else { | |
self[index(endIndex, offsetBy: offset)] | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Algorithms | |
// The ~= operator means "contains", and is used to test if a value is within a range. | |
let seq = 9...13 | |
print(seq ~= 9) // true | |
print(seq ~= 50) // false | |
// The .adjacentPairs() method lets you iterate through a sequence in pairs of current + next. | |
let pairs = ["a", "b", "c", "d"].adjacentPairs() | |
for pair in pairs { print(pair) } // ("a", "b"), ("b", "c"), ("c", "d") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/// Find specific character in String using String.Index (Unicode-compliant way) | |
/// Usage: | |
/// let word = "Banana" | |
/// let fifthCharacter = word[4] | |
/// print("The 5th character is: \(fifthCharacter)") // Prints: The 5th character is: n | |
extension StringProtocol { | |
subscript(offset: Int) -> Character { | |
self[index(startIndex, offsetBy: offset)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Changing the most recent commit message | |
1. Run 'git commit --amend' in terminal | |
2. Edit the commit message | |
3. Save and close the opened default text editor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Initialize an array with fixed number of default values | |
var digitCounts = Array(repeating: 0, count: 5) | |
// [0, 0, 0, 0, 0] | |
// Convert an array of integers to an array of strings, use 'map()' | |
let numbers: [Int] = [1, 2, 3, 4, 5] | |
let stringArray = numbers.map { String($0) } | |
// Find the maximum element in an array, can be nil | |
let numbers = [1, 2, 3, 4, 5] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension View { | |
func hostingView() -> UIView { | |
let hostingView = UIHostingController(rootView: self) | |
return hostingView.view | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum OpenURL { | |
enum Scheme { | |
case url(String) | |
case tel(String) | |
case app(String) | |
} | |
static func open(scheme: OpenURL.Scheme, completion: ((Bool) -> Void)? = nil) { | |
let application = UIApplication.shared | |
let url: URL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SwipeBackGestureViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
navigationController?.hidesBarsOnSwipe = true | |
navigationController?.interactivePopGestureRecognizer?.delegate = self | |
} | |
override func viewDidAppear(_ animated: Bool) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Using Markup | |
Single Line comment | |
//: line of markup content | |
Multi Line Comment | |
/*: | |
... | |
*/ | |
2. Headings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
enum Dough: String { | |
case basic | |
case cheeseCrust | |
} | |
enum Topping: String, CaseIterable { | |
case pepperoni | |
case sausage |
NewerOlder