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 HomeViewController: UIViewController { | |
private let user: User | |
private let apiService: ApiService | |
private lazy var name = UILabel() | |
init(user: User, apiService: ApiService) { | |
self.user = user | |
self.apiService = apiService | |
super.init(nibName: nil, bundle: nil) | |
} |
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
typealias CompletionBlock = (Data?) -> Void | |
protocol NetworkService { | |
func make(request: URLRequest, completionHandler: @escaping CompletionBlock) | |
} | |
extension URLSession: NetworkService { | |
func make(request: URLRequest, completionHandler: @escaping CompletionBlock) { | |
let task = self.dataTask(with: request) { data, _, _ in | |
completionHandler(data) |
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
struct User { | |
let name: String | |
} | |
class UserController { | |
static let shared = UserController() | |
var currentUser: User? | |
private init() { | |
//don't forget to make this private |
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 | |
typealias UserDataSource = UITableViewDiffableDataSource<UsersViewController.Section, User> | |
typealias UserSnapshot = NSDiffableDataSourceSnapshot<UsersViewController.Section, User> | |
class UsersViewController: UITableViewController { | |
var users = [User]() | |
var datasource: UserDataSource! | |
func configureDataSource() { |
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
collectionView?.performBatchUpdates({ | |
self.collectionView?.deleteItemsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0), NSIndexPath(forRow: 3, inSection: 0)]) | |
self.collectionView?.reloadItemsAtIndexPaths([NSIndexPath(forRow: 1, inSection: 0), NSIndexPath(forRow: 6, inSection: 0)]) | |
self.collectionView?.insertItemsAtIndexPaths([NSIndexPath(forRow: 5, inSection: 0), NSIndexPath(forRow: 7, inSection: 0)]) | |
self.collectionView?.insertItemsAtIndexPaths([NSIndexPath(forRow: 2, inSection: 0), NSIndexPath(forRow: 4, inSection: 0)]) | |
}, completion: nil) |
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
let downloadOperation = MyDownloadOperation() | |
let filterOperation = MyFilterOperation() | |
filterOperation.addDependency(downloadOperation) |
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 MainViewController: UIViewController { | |
var imageRecords: [ImageRecord] = [] | |
let pendingOperations = PendingOperations() | |
} | |
extension MainViewController: UITableViewDataSource { | |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) |
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
func fetchImageDataSource() { | |
let request = URLRequest(url: "https://your-server-url-to-fetch-endpoints") | |
let task = URLSession(configuration: .default).dataTask(with: request) { data, response, error in | |
self.imagesRecord.append(record0...<recordn) | |
DispatchQueue.main.async { | |
self.tableView.reloadData() | |
} | |
} | |
task.resume() | |
} |
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 CustomOperation: Operation { | |
// MARK: - Types | |
//Step 1: define oppration states | |
enum State { | |
case isReady, isExecuting, isFinished | |
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 ImageDownloadHelper: SwiftOperation { | |
//1 | |
let imageRecord: ImageRecord | |
//2 | |
init(_ imageRecord: ImageRecord) { | |
self.imageRecord = imageRecord | |
} | |
//3 |