Skip to content

Instantly share code, notes, and snippets.

View shanev's full-sized avatar
💭
#BUIDL

shane.stars shanev

💭
#BUIDL
View GitHub Profile
import Foundation
import PlaygroundSupport
let apiKey = "[API KEY]"
let endPointUrl = "https://westus.api.cognitive.microsoft.com/emotion/v1.0"
let url = URL(string: "\(endPointUrl)/recognize")!
let testImageString = "https://pbs.twimg.com/profile_images/853106910861643776/tZh0rhmL.jpg"
struct Response: Decodable {
let scores: Score
module.exports = {
"env": {
"browser": false,
"node": true,
"es6": true,
"mocha": true,
},
"rules": {
"no-param-reassign": [2, { "props": false }],
"no-console":0,
import UIKit
// reference each view controller before UIApplicationMain runs
print(CustomViewController.self)
// The following is required because there's an impedence mismatch between
// `CommandLine` and `UIApplicationMain` <rdar://problem/25693546>.
let argv = UnsafeMutableRawPointer(CommandLine.unsafeArgv).bindMemory(
to: UnsafeMutablePointer<Int8>.self,
capacity: Int(CommandLine.argc)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
guard
let viewController = segue.destination as? CustomViewController
else { return }
// compiler knows that CustomViewModel is our view model type
viewController.viewModel = CustomViewModel(data: "hello")
}
struct CustomViewModel {
let data: String
init(data: String) {
self.data = data
}
}
final class CustomViewController: ViewController<CustomViewModel> {
override func viewDidLoad() {
import UIKit
public class ViewController<T>: UIViewController {
var viewModel: T!
override public func viewDidLoad() {
super.viewDidLoad()
assertViewModel()
}
}
import UIKit
print(DerivedViewController.self)
// The following is required because there's an impedence mismatch between
// `CommandLine` and `UIApplicationMain` <rdar://problem/25693546>.
let argv = UnsafeMutableRawPointer(CommandLine.unsafeArgv).bindMemory(
to: UnsafeMutablePointer<Int8>.self,
capacity: Int(CommandLine.argc)
)
@shanev
shanev / CustomViewController.swift
Last active February 23, 2018 20:09
Generic view controllers with storyboards and view model injection
struct CustomViewModel {
let data: String
init(data: String) {
self.data = data
}
}
final class CustomViewController: ViewController<CustomViewModel> {
override func viewDidLoad() {
@shanev
shanev / icon_create.sh
Created April 21, 2017 16:56
Generate an iOS app icon with a gradient background using an input image
#!/bin/sh
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
FILE="$DIR/../Design/appicon-foreground.svg"
OUTPUT_LOCATION="$DIR/../iTunes"
# convert svg asset to png
convert -density 2000 -resize 1024x1024 -background none $FILE appicon-foreground.png
@shanev
shanev / shorttimeago.swift
Last active August 29, 2015 14:19
"Short time ago" function for Swift (based on https://github.com/MatthewYork/DateTools)
func shortTimeAgoSinceDate(date: NSDate) -> String {
let calendar = NSCalendar.currentCalendar()
let unitFlags = NSCalendarUnit.CalendarUnitMinute | NSCalendarUnit.CalendarUnitHour | NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitWeekOfYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitSecond
let now = NSDate()
let earliest = now.earlierDate(date)
let latest = (earliest == now) ? date : now
let components:NSDateComponents = calendar.components(unitFlags, fromDate: earliest, toDate: latest, options: nil)
if (components.year >= 1) {
return "\(components.year)y"