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
final class MockApiClient: Api { | |
private static let delay = 4 | |
private static var requestCount = 0 | |
func fetchMovies(completion: @escaping (Bool, FilmsData?) -> Void) { | |
switch MockApiClient.requestCount { | |
case 0: | |
DispatchQueue.main.async { | |
completion(false, nil) |
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
final class MockApiClient: Api { | |
private static let delay = 5 | |
func fetchMovies(completion: @escaping (Bool, FilmsData?) -> Void) { | |
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(MockApiClient.delay)) { | |
// mock your data as before, but now we have a simulated 5-second delay | |
} | |
} | |
} |
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
final class MockApiClient: Api { | |
private static let delay = 5 | |
func fetchMovies(completion: @escaping (Bool, FilmsData?) -> Void) { | |
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(MockApiClient.delay)) { | |
// mock your data as before, but now we have simulated delay too | |
} | |
} | |
} |
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
final class MockApiClient: Api { | |
func fetchMovies(completion: @escaping (Bool, FilmsData?) -> Void) { | |
let filePath = "films" | |
MockApiClient.loadJsonDataFromFile(filePath, completion: { data in | |
if let json = data { | |
do { | |
let films = try JSONDecoder().decode(FilmsData.self, from: json) | |
completion(true, films) | |
} | |
catch _ as NSError { |
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
final class ApiClient: Api { | |
func fetchMovies(completion: @escaping (Bool, FilmsData?) -> Void) { | |
HTTPClient.get(from: URL(string: "https://swapi.co/api/films")!) { (json, error) in | |
if error == nil { | |
do { | |
let data = try JSONSerialization.data(withJSONObject: json as Any, options: []) | |
if let string = String(data: data, encoding: String.Encoding.utf8)?.data(using: .utf8) { | |
let filmsData = try JSONDecoder().decode(FilmsData.self, from: string) | |
completion(true, filmsData) |
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
let apiService: Api = { | |
if CommandLine.arguments.contains("-mockApi") { | |
return MockApiClient() | |
} else { | |
return ApiClient() | |
} | |
}() |
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
final class ApiClient: Api { | |
func fetchMovies(completion: @escaping (Bool, FilmsData?) -> Void) { | |
// reach the actual API | |
} | |
} | |
final class MockApiClient: Api { | |
func fetchMovies(completion: @escaping (Bool, FilmsData?) -> Void) { | |
// mock API data | |
} |
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
protocol Api { | |
func fetchMovies(completion: @escaping (Bool, FilmsData?) -> Void) | |
} |
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
ls -l | awk '{print $9}' | sed 's/\(.*\)\..*/\1/' > ~/Desktop/imagelist.txt |
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
//: Playground - noun: a place where people can play | |
import UIKit | |
var result:Double? | |
var operandStack = Array<Double>() | |
operandStack.append(2) | |
operandStack.append(5) |
NewerOlder