Skip to content

Instantly share code, notes, and snippets.

@boazFrenkel
boazFrenkel / AlamofireOnMainQueue.swift
Last active February 24, 2020 08:24
AlamofireOnMainQueue
Alamofire.request("Some URL", parameters: ["Some": "Param"])
.response(
responseSerializer: DataRequest.jsonResponseSerializer(),
completionHandler: { response in
/*
You are now running on the main queue
*/
//Then you parse to objects
//Do any manipulation if needed
@boazFrenkel
boazFrenkel / AlamofireOnBackgroundQueue.swift
Last active February 21, 2020 14:11
Alamofire 4 get the response on a specific queue
let queue = DispatchQueue(label: "com.response-queue", qos: .utility, attributes: [.concurrent])
Alamofire.request("Some URL", parameters: ["Some": "Param"])
.response(
queue: queue,
responseSerializer: DataRequest.jsonResponseSerializer(),
completionHandler: { response in
/*
You are now running on the queue you created. So you
can parse the response to model objects or do any
other handling if necessary.
struct QuotesResponse : Decodable {
//This is our daynamic key structure
struct QuotesKey : CodingKey {
var stringValue: String
init(stringValue: String) {
self.stringValue = stringValue
}
var intValue: Int? { return nil }
init?(intValue: Int) { return nil }
//we know what is "inside" this key (container)
struct Quote : Codable {
var time: String
var open: String
var high: String
var low: String
var close: String
var volume: String
}
@boazFrenkel
boazFrenkel / CodableEnumCodingKeys.swift
Last active February 2, 2020 22:02
common customization is the use of an enum to represent coding keys when they diverge from the Swift naming.
struct Toy : Codable {
let id: Int
let availableAt: String
let ageRange: String?
enum CodingKeys: String, CodingKey {
case id
case availableAt = "available_at"
case ageRange = "range"
}
@boazFrenkel
boazFrenkel / UICollectionView in a UITableViewCell both with dynamic heights
Created July 11, 2018 12:42
example UICollectionView with dynamic height inside a UITableViewCell
/**************
My cell has a label and under it a collection view with varying number of buttons in different sizes
So I'm letting Autolayout set the position of things and then taking all heights together for the cell height size
*************/
class ButtonCollectionCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var collectionViewDistanceFromCellBottomConstraint: NSLayoutConstraint!
@IBOutlet weak var distanceBetweenCollectionToLabelConstraint: NSLayoutConstraint!
@IBOutlet weak var buttonsCollectionView: UICollectionView!