Skip to content

Instantly share code, notes, and snippets.

View backslash-f's full-sized avatar
💻
Keep calm and Swift

Fernando Fernandes backslash-f

💻
Keep calm and Swift
View GitHub Profile
@backslash-f
backslash-f / iOSInstalledFonts.swift
Last active August 29, 2015 14:07
List installed fonts in iOS
for familyName in UIFont.familyNames() {
println("Font family name: \(familyName)")
println("Font names under family: \(UIFont.fontNamesForFamilyName(familyName as String))")
}
@backslash-f
backslash-f / ResponseMocker.swift
Last active November 23, 2016 04:04
JSON to AnyObject from a .json file
import Foundation
/// Mocks network responses. Useful to be used with Unit/UI tests.
class ResponseMocker {
/// Returns a mocked `JSON` object encapsulated in an `AnyObject`,
/// representing a mocked endpoint response.
///
/// - returns: A mocked `JSON` object encapsulated in an `AnyObject`,
/// representing a Notice root endpoint response.
@backslash-f
backslash-f / TodayChecker.swift
Created March 2, 2018 16:22
Is it today? (Calendar, Date, DateFormatter, Swift)
import Foundation
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd'T'HH:mm:ssZZZZZ"
let date = dateFormatter.date(from: "2018-03-02T10:00:00-0800")
Calendar.current.isDateInToday(date!)
@backslash-f
backslash-f / ClampingPlayground.swift
Last active September 7, 2022 09:26
Swift Clamping Example
import Foundation
import UIKit
/// Clamps the given `value` into the range defined by `minValue` and `maxValue`. For example:
///
/// (0.0 ... 5.0).clamp(4.2) = 4.2
/// (0.0 ... 5.0).clamp(-1.3) = 0.0
/// (0.0 ... 5.0).clamp(6.4) = 5.0
///
/// Source: https://stackoverflow.com/a/46799935/584548
@backslash-f
backslash-f / CodableDate.swift
Last active September 10, 2020 04:05
Swift Codable + Date
// Fix for error: "Expected to decode Double but found a string/data instead"
public struct CodableEntity: Codable {
public let myBool: Bool
public let myDate: Date
public let myInt: Int
}
let jsonData = Data("""
@backslash-f
backslash-f / MakeFirstLineBold.swift
Created July 4, 2018 09:37
Make the first line of a UILabel (any multi line text really) bold
import UIKit
import PlaygroundSupport
extension UILabel {
/// Makes the first line (of a multiline label) bold.
/// In case the label is a "single line" one, the function does nothing.
/// In case there is no bold version of the current font, the function does nothing.
public func makeFirstLineBold() {
import UIKit
import PlaygroundSupport
let subviewFrame = CGRect(x: 50, y: 50, width: 300, height: 300)
let subview = UIView(frame: subviewFrame)
subview.backgroundColor = .yellow
subview.layer.cornerRadius = 20
subview.layer.shadowColor = UIColor.black.cgColor
@backslash-f
backslash-f / swift.sh
Last active March 15, 2019 11:57
Primitive example on how to manually parse a JSON in Swift
#!/usr/bin/swift
import Foundation
let jsonData = Data("""
{
"premiumBundle": {
"enabled": true,
"variations": {
"sku1": {
@backslash-f
backslash-f / VaporPOST.swift
Last active May 27, 2019 13:43
Vapor, POST returning Future<SomeContent>
// `POST` receiving an `Request` and returning an `ReceiptValidation`.
func validate(_ request: Request) throws -> Future<ReceiptValidation> {
let promise = request.eventLoop.newPromise(ReceiptValidation.self)
DispatchQueue.global().async {
guard let receipt = try? request.content.decode(Receipt.self).wait() else {
promise.fail(error: Abort(HTTPResponseStatus.badRequest))
return
}
@backslash-f
backslash-f / sublime-keymaps-user
Last active October 13, 2020 09:00
Gist for Sublime Key Bindings
[
{
"keys": ["super+enter"],
"command": "build"
},
{
"keys": ["super+shift+c"],
"command": "show_u200b"
},
{