- EF.6: Write unit tests to ensure that software works as intended
- EF.10.f: Use Codable and JSONDecoder to parse JSON data
- EF.10.g: Use GCD to appropriately dispatch tasks
- EF.10.h: Use URLSession to get data from online
- IOS.3.e: Use a search bar to filter data in a Table View.
- iOS.3.s: Load images smoothly, without flickering , and displaying a spinner when needed
- EF.4.e: Use frequent, descriptive, small commits
- EF.8.a: Create an App using MVC design
- IOS.1: Write clean, readable Swift code
- IOS.2.g: Auto_Layout
- IOS.3.d: Load Data into a TableView
- LF.5: Use functions to keep code DRY
- iOS.3.p: Create Custom TableView Cells
- iOS.3.q: Segue to a DetailViewController
- iOS.3.r: Create a TableView with Sections
Assignment | Standards |
---|---|
Stocks and Contacts |
|
TV Shows |
|
Assessment | Standards |
---|---|
6.1 Elements / 6.3 Elements |
|
Network Helper - wrapper for URLSession
import Foundation
enum HTTPMethod: String {
case get = "GET"
case post = "POST"
}
class NetworkHelper {
// MARK: - Static Properties
static let manager = NetworkHelper()
// MARK: - Internal Properties
func performDataTask(withUrl url: URL,
andHTTPBody body: Data? = nil,
andMethod httpMethod: HTTPMethod,
completionHandler: @escaping ((Result<Data, AppError>) -> Void)) {
var request = URLRequest(url: url)
request.httpMethod = httpMethod.rawValue
request.httpBody = body
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
urlSession.dataTask(with: request) { (data, response, error) in
DispatchQueue.main.async {
guard let data = data else {
completionHandler(.failure(.noDataReceived))
return
}
guard let response = response as? HTTPURLResponse, (200...299) ~= response.statusCode else {
completionHandler(.failure(.badStatusCode))
return
}
if let error = error {
let error = error as NSError
if error.domain == NSURLErrorDomain && error.code == NSURLErrorNotConnectedToInternet {
completionHandler(.failure(.noInternetConnection))
return
} else {
completionHandler(.failure(.other(rawError: error)))
return
}
}
completionHandler(.success(data))
}
}.resume()
}
// MARK: - Private Properties and Initializers
private let urlSession = URLSession(configuration: URLSessionConfiguration.default)
private init() {}
}
AppError - handles error throughout the app
import Foundation
enum AppError: Error {
case unauthenticated
case invalidJSONResponse
case couldNotParseJSON(rawError: Error)
case noInternetConnection
case badURL
case badStatusCode
case noDataReceived
case notAnImage
case other(rawError: Error)
}