This file contains hidden or 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
| // ### Con's of the UITableView's `register` and `dequeue` methods | |
| // - You have to use String for `reusableIdentifier`. | |
| // - The interfaces for nib and class are different. | |
| // - When you dequeue cells, you have to cast them as custom classes. | |
| protocol Reusable: class { | |
| static var reusableIdentifier: String { get } | |
| } | |
| extension Reusable { |
This file contains hidden or 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 Dog { | |
| private (set) var name = "" | |
| func change(name: String) { | |
| self.name = name | |
| } | |
| } | |
| let dog = Dog() | |
| print(dog.name) // This is ok |
This file contains hidden or 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
| // Somthing like this: | |
| var current = 1 | |
| while current * current <= 100 { | |
| current += 1 | |
| } | |
| result = current * current | |
| // To this: | |
| let result = (1...) | |
| .lazy |
This file contains hidden or 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
| /////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
| // The key questions we ask ourselves in these cases is: does the nil value occur due to a programmer error? // | |
| /////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
| // Somthing like this: | |
| let url: URL? = URL(string: "https://gist.github.com") | |
| // To this: | |
| let url: URL = URL(string: "https://gist.github.com")! |
This file contains hidden or 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
| // Somthing like this: | |
| func parseElement(name: String, text: String) { | |
| let trimmed = text.trimmingCharacters(in: .whitespaces) | |
| if name == "trk" { | |
| let t = trimmed | |
| // process element | |
| } else if name == "ele" { | |
| guard let elevation = Int(trimmed) else { return } | |
| } else { | |
| // no need to trim text |
This file contains hidden or 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
| // Create a dispatch group | |
| let userListDispatchGroup = DispatchGroup() | |
| // Get List of Users | |
| userListDispatchGroup.enter() | |
| Alamofire.request("https://httpbin.org/get?users").validate().responseJSON { response in | |
| switch response.result { | |
| case .success: | |
| print("Get User Successful") |
This file contains hidden or 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 | |
| // MARK: UserDefaults extension | |
| extension UserDefaults { | |
| struct SomeApp: UserDefaultable { | |
| enum UserDefaultKey: String { | |
| case someItem | |
| } | |
| private init() {} |
This file contains hidden or 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 | |
| // MARK: UserDefaults extension | |
| extension UserDefaults { | |
| struct SomeApp: UserDefaultable { | |
| enum UserDefaultKey: String { | |
| case someItem | |
| } | |
| private init() {} |
This file contains hidden or 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
| //////////////////////// | |
| // MARK: Array | |
| //////////////////////// | |
| // map function | |
| extension Array { | |
| func map<T>(_ transform: (Element) throws -> T) rethrows -> [T] { | |
| var result: [T] = [] | |
| result.reserveCapacity(count) | |
This file contains hidden or 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 UIKit | |
| extension UIView { | |
| @IBInspectable var borderColor: UIColor? { | |
| get { | |
| return layer.borderColor.map { UIColor(cgColor: $0) } | |
| } | |
| set { | |
| layer.borderColor = newValue?.cgColor |