Skip to content

Instantly share code, notes, and snippets.

View albertoramonj's full-sized avatar

Alberto R. albertoramonj

View GitHub Profile
@rd13
rd13 / .swift
Last active September 11, 2022 17:00
Copy database file from bundle to documents in Swift 3
func copyDatabaseIfNeeded() {
// Move database file from bundle to documents folder
let fileManager = FileManager.default
let documentsUrl = fileManager.urls(for: .documentDirectory,
in: .userDomainMask)
guard documentsUrl.count != 0 else {
return // Could not find documents URL
@cmoulton
cmoulton / Custom HTTP Headers with Swift and Alamofire.swift
Last active July 15, 2022 12:37
Custom HTTP Headers with Swift 3 or 4 and Alamofire 4.0-4.7: See https://grokswift.com/custom-headers-alamofire4-swift3/ for explanations
// MARK: - Adding a header to a single request
func doRequestWithHeaders1() {
let headers: HTTPHeaders = [
"X-Mashape-Key": MY_API_KEY,
"Accept": "application/json"
]
Alamofire.request("https://mashape-community-urban-dictionary.p.mashape.com/define?term=smh", headers: headers)
.responseJSON { response in
debugPrint(response)
@dduan
dduan / migrate.py
Created October 25, 2016 00:10
Migrate Xcode 8.0 style comment to Xcode 8.1 style
# usage:
# IFS=$'\n' find ROOT_SOURCE_FOLDER -name "*.swift" -exec python PATH/TO/migrate.py {} \;
import sys
import re
func = re.compile('func\s+\w+\(')
param = re.compile('(?P<indent>\s*)/// -[ ]*[pP]arameter[ ]+(?P<name>\w+)[ ]*:(?P<description>[^\n]+)\n')
ret = re.compile('(?P<indent>\s*)/// -[ ]*[rR]eturns[ ]*:(?P<description>[^\n]+)\n')
continuation = re.compile('\s*///(?P<description>[^\n]+)')
@mystygage
mystygage / docker-compose.yml
Created March 6, 2017 21:02
Microsoft SQL Server in Docker with data volume
version: '3'
services:
mssql-server-linux:
image: microsoft/mssql-server-linux:latest
volumes:
- mssql-server-linux-data:/var/opt/mssql/data
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=${SQLSERVER_SA_PASSWORD:-yourStrong(!)Password}
@tatimagdalena
tatimagdalena / DateComparison.swift
Last active June 5, 2023 06:41
Compare two dates depending on granularity.
let granularity = Calendar.Component.day
let comparisonResult = Calendar.current.compare(firstDate, to: secondDate, toGranularity: granularity)
// granularity:
// .second
// .minute
// .hour
// .day
// .month
// .year
@Ben-G
Ben-G / L10NTests.swift
Created June 16, 2017 20:35
Very simple automated test to ensure localizations exist and are well-formed on iOS.
import Foundation
import XCTest
/// Basic sanity check that ensures that we are able to retrieve localized strings for all languages
/// we support.
final class L10NTests: XCTestCase {
func testLocalizations() {
let locales = ["en", "es", "zh-Hans", "zh-Hant", "fi"]
for locale in locales {
@hemangshah
hemangshah / MyLabel.swift
Created August 24, 2017 09:44
Observe Text changes in UILabel in Swift
class MyLabel: UILabel {
var textWillChange:((_ oldText: String?)->())? = nil
var textDidChange:((_ newText: String?)->())? = nil
override var text: String? {
willSet {
if textWillChange != nil {
textWillChange!(self.text)
}
}
didSet {
@dfreniche
dfreniche / select-xcode.sh
Last active October 11, 2017 19:41
Rather crude script to change between Xcode 8 and Xcode 9 selecting the correct command line tools
#!/bin/bash
# if 1st param empty...
if [ -z "$1" ]
then
echo "Usage: select-xcode {8|9}"
exit
fi
XCODE8_APP=/Applications/Xcode-8.app
@davidlawson
davidlawson / BadgeBarButtonItem.swift
Last active November 20, 2024 22:07
UIBarButtonItem with badge, Swift 4, iOS 9/10/11
import UIKit
public class BadgeBarButtonItem: UIBarButtonItem
{
@IBInspectable
public var badgeNumber: Int = 0 {
didSet {
self.updateBadge()
}
}
@jlalvarez18
jlalvarez18 / deepDescription.swift
Last active June 4, 2022 12:12 — forked from mhuusko5/Swift – func deepDescription
Swift 4 – func deepDescription(any: Any) -> String (pretty print any object, recursively)
func deepDescription(_ any: Any) -> String {
guard let any = deepUnwrap(any) else {
return "nil"
}
if any is Void {
return "Void"
}
if let int = any as? Int {