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 | |
| extension NSError { | |
| static func generalParsingError(domain: String) -> Error { | |
| return NSError(domain: domain, code: 400, userInfo: [NSLocalizedDescriptionKey : NSLocalizedString("Error retrieving data", comment: "General Parsing Error Description")]) | |
| } | |
| } |
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
| extension UIViewController { | |
| func dismiss() { | |
| view.endEditing(true) | |
| dismiss(animated: true, 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
| extension String { | |
| // String extension check that itself for valid email pattern and returns boolean | |
| func isValidEmail() -> Bool { | |
| let emailRegEx = "^(?:(?:(?:(?: )*(?:(?:(?:\\t| )*\\r\\n)?(?:\\t| )+))+(?: )*)|(?: )+)?(?:(?:(?:[-A-Za-z0-9!#$%&’*+/=?^_'{|}~]+(?:\\.[-A-Za-z0-9!#$%&’*+/=?^_'{|}~]+)*)|(?:\"(?:(?:(?:(?: )*(?:(?:[!#-Z^-~]|\\[|\\])|(?:\\\\(?:\\t|[ -~]))))+(?: )*)|(?: )+)\"))(?:@)(?:(?:(?:[A-Za-z0-9](?:[-A-Za-z0-9]{0,61}[A-Za-z0-9])?)(?:\\.[A-Za-z0-9](?:[-A-Za-z0-9]{0,61}[A-Za-z0-9])?)*)|(?:\\[(?:(?:(?:(?:(?:[0-9]|(?:[1-9][0-9])|(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5]))\\.){3}(?:[0-9]|(?:[1-9][0-9])|(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5]))))|(?:(?:(?: )*[!-Z^-~])*(?: )*)|(?:[Vv][0-9A-Fa-f]+\\.[-A-Za-z0-9._~!$&'()*+,;=:]+))\\])))(?:(?:(?:(?: )*(?:(?:(?:\\t| )*\\r\\n)?(?:\\t| )+))+(?: )*)|(?: )+)?$" | |
| let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx) | |
| let result = emailTest.evaluate(with: self) | |
| return result | |
| } |
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
| extension UIImage { | |
| enum Quality: CGFloat { | |
| case worst = 0 | |
| case low = 0.25 | |
| case medium = 0.5 | |
| case high = 0.75 | |
| case best = 1 | |
| } | |
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 UICollectionViewCell { | |
| static var identifier: String { return String(describing: self) } | |
| } |
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 | |
| fileprivate let imageCache = NSCache<NSString, UIImage>() | |
| extension UIImageView { | |
| func downloadImage(url: URL) { | |
| if let cachedImage = imageCache.object(forKey: url.absoluteString as NSString) { | |
| self.image = cachedImage | |
| return |
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 ViewController: UIViewController { | |
| let imageViewOne = UIImageView() | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| imageViewOne.frame = UIScreen.main.bounds | |
| view.addSubview(imageViewOne) | |
| setImage() | |
| } |
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
| extension ImageDownloadProtocol { | |
| func downloadImage(from url: URL, completion: @escaping (UIImage?) -> Void) { | |
| let session = URLSession(configuration: .default) | |
| DispatchQueue.global(qos: .background).async { | |
| print("In background") | |
| session.dataTask(with: URLRequest(url: url)) { data, response, error in | |
| if error != nil { | |
| print(error?.localizedDescription ?? "Unknown error") | |
| } | |
| if let data = data, let image = UIImage(data: 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
| protocol ImageDownloadProtocol { | |
| func downloadImage(from url: URL, completion: @escaping (UIImage?) -> Void) | |
| } |
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 testDataStore() { | |
| // Setup | |
| // Run test | |
| waitForExpectations(timeout: 4) { error in | |
| if let error = error { | |
| XCTFail("waitForExpectationsWithTimeout error: \(error)") | |
| } | |
| } |