Skip to content

Instantly share code, notes, and snippets.

@gmoraleda
gmoraleda / Car.swift
Last active October 3, 2018 09:35
Car.swift
internal var oldImageUrl: URL? {
return URL(string: carImageUrl ?? "")
}
internal var newImageUrl: URL? {
let urlString = Constants.URL.images
.replacingOccurrences(of: "{modelIdentifier}", with: modelIdentifier ?? "")
.replacingOccurrences(of: "{color}", with: color ?? "")
return URL(string: urlString)
init(date: Date, frame: CGRect) {
super.init(frame: frame)
// Do stuff with Date
}
func setup(date: Date) {
self.date = date
updateInterface() // Setup view with strings, colours, etc.
}
Alamofire.request(requestURL, method: .get, parameters: parameters, encoding: URLEncoding.default).responseData { response in
if let data = response.result.value {
let decoder = JSONDecoder()
do {
let apiResponse = try decoder.decode(ApiResponse.self, from: data)
// Do stuff
} catch {
NSLog("Error while decoding")
// Error handling
}
struct ApiResponse: Codable {
let errorCode: Int
let name: String
let age: Int
}
enum ErrorCode: Int, Error, Codable {
case noError = 0
case noConnection = 401
case userNotFound = 404
// ...
var localizedDescription: String? {
switch self {
case .noError:
return nil
struct ApiResponse: Codable {
let errorCode: ErrorCode
let name: String
let age: Int
}
struct ApiResponse: Codable {
let errorCode: ErrorCode
let name: String
let age: Int
private enum CodingKeys: String, CodingKey {
case errorCode = "errorcode"
case name = "firstname"
case age = "userage"
}
let myCollectionView = UICollectionView()
// ... things go on until I have a frame and a layout...
myCollectionView = UICollectionView(frame: myFrame, collectionViewLayout: myCollectionViewLayout)
lazy var collectionView = setupCollectionView()
private func setupCollectionView() -> UICollectionView {
let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 0
layout.itemSize = CGSize(width: 50, height:50)
// Perform any other customizing to your layout/UICollectionView
return UICollectionView(frame: .zero, collectionViewLayout: layout)