Skip to content

Instantly share code, notes, and snippets.

View jbarros35's full-sized avatar

10x developer jbarros35

  • Lisbon
View GitHub Profile
@jbarros35
jbarros35 / gist:b57cfe037227eb948c0ad6c80c1aefee
Created October 31, 2019 17:27
setup DrawerController with a UITabBar
DispatchQueue.main.async {
// Init Drawer
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let storyBoard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
let navVC = storyBoard.instantiateViewController(withIdentifier: "MainController") as! MyTabBarController
navVC.loggedAlready = true
// is the main controller for the drawer.
let drawerVC = storyBoard.instantiateViewController(withIdentifier: "DrawerVC")
// is the drawer, and the drawer needs a main controller
appDelegate.drawerController.mainViewController = navVC
static func showStandardMessage(reference: UIViewController, title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
alert.view.setNeedsLayout()
reference.present(alert, animated: true, completion: nil)
}
static func showStartCancel(reference: UIViewController, title: String, message: String, callback: @escaping (UIAlertAction)->()) {
// create the alert
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
@jbarros35
jbarros35 / gist:8f280d6300f6e97933e0400d16e9a530
Created April 25, 2018 14:42
Convert JavaScript dates and Swift
// REMARK: format date for better display
func convertDate(date: Date) -> String? {
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyyMMdd HH:mm:ss"
let dateString = dateFormatterGet.string(from: date)
return dateString
}
// REMARK: server sends dates on JS string format
func convertJSDate(date: String) -> String? {
@jbarros35
jbarros35 / gist:c72a50b7dca030e72d93f13989b556d2
Created April 25, 2018 14:42
Convert JavaScript dates and Swift
// REMARK: format date for better display
func convertDate(date: Date) -> String? {
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyyMMdd HH:mm:ss"
let dateString = dateFormatterGet.string(from: date)
return dateString
}
// REMARK: server sends dates on JS string format
func convertJSDate(date: String) -> String? {
@jbarros35
jbarros35 / gist:0562bf47f162c4351d6f05b602203c7d
Created April 25, 2018 14:42
Convert JavaScript dates and Swift
// REMARK: format date for better display
func convertDate(date: Date) -> String? {
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyyMMdd HH:mm:ss"
let dateString = dateFormatterGet.string(from: date)
return dateString
}
// REMARK: server sends dates on JS string format
func convertJSDate(date: String) -> String? {
struct Event: Decodable {
let id: String
let event: String
let data: [[Int]]?
let date: String?
}
func decode() {
let json = """
{
"id": "b1afdc30-47e7-11e8-8931-33f1426b7c32",
var customersInLine = ["Alex", "Ewa", "Barry", "Daniella"]
print(customersInLine.remove(at:0)) // Alex
func serve(customer : @autoclosure () -> String) {
print("serving customer \(customer())")
}
serve(customer: customersInLine.remove(at:0)) // Ewa
var param = "try change me brother"
func defaultFunc(_ param:String) {
// param = "I change you" won't compiles
}
defaultFunc(param)
print(param) // try change me brother
func inOutFunc(_ param :inout String) {
param = "I changed you"
}
inOutFunc(&param) // needs explicit &
@jbarros35
jbarros35 / gist:e4d4d831305e2107997c25e6c4182dd1
Last active April 16, 2018 11:03
Simple count words with dictionary in Swift
var dictionary = [String: Int]()
var counts = "all for all fun fun funny".components(separatedBy:" ").map({
var counter = dictionary[$0] ?? 0
dictionary[$0] = counter + 1
})
print(dictionary)
@jbarros35
jbarros35 / gist:f0dd9bb38cd4d1cc7ccc1ffdbcbefe6c
Created April 13, 2018 09:37
Swift a failable initializer, will fails if any of those params would nil
struct User {
let id: Int
let name: String
let username: String
init?(dictionary: Dictionary<String: Any>) {
guard
let id = dictionary[“id”] as? Int,
let name = dictionary[“name”] as? String,
let username = dictionary[“username”] as? String
else {