Skip to content

Instantly share code, notes, and snippets.

Avatar

10x developer jbarros35

  • Lisbon
View GitHub Profile
@jbarros35
jbarros35 / gist:b57cfe037227eb948c0ad6c80c1aefee
Created Oct 31, 2019
setup DrawerController with a UITabBar
View gist:b57cfe037227eb948c0ad6c80c1aefee
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
View gist:8b7af5f6eb2469d41e15a57ddbb86951
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 Apr 25, 2018
Convert JavaScript dates and Swift
View gist:8f280d6300f6e97933e0400d16e9a530
// 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 Apr 25, 2018
Convert JavaScript dates and Swift
View gist:c72a50b7dca030e72d93f13989b556d2
// 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 Apr 25, 2018
Convert JavaScript dates and Swift
View gist:0562bf47f162c4351d6f05b602203c7d
// 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? {
View gist:70649c0d8028a697764a2d06eea61407
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",
View gist:6ee30b24de0ceebc79330ef8945e39b1
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
View gist:4ebca639123a76f62280fa911446a694
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 Apr 16, 2018
Simple count words with dictionary in Swift
View gist:e4d4d831305e2107997c25e6c4182dd1
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 Apr 13, 2018
Swift a failable initializer, will fails if any of those params would nil
View gist:f0dd9bb38cd4d1cc7ccc1ffdbcbefe6c
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 {