Skip to content

Instantly share code, notes, and snippets.

View svetlanama's full-sized avatar

Svitlana Moiseyenko svetlanama

View GitHub Profile
@jacks205
jacks205 / timeago.swift
Last active October 15, 2018 21:54 — forked from minorbug/timeago.swift
"Time ago" function for Swift (based on MatthewYork's DateTools for Objective-C) *Swift 2
func timeAgoSinceDate(date:NSDate, numericDates:Bool) -> String {
let calendar = NSCalendar.currentCalendar()
let now = NSDate()
let earliest = now.earlierDate(date)
let latest = (earliest == now) ? date : now
let components:NSDateComponents = calendar.components([NSCalendarUnit.Minute , NSCalendarUnit.Hour , NSCalendarUnit.Day , NSCalendarUnit.WeekOfYear , NSCalendarUnit.Month , NSCalendarUnit.Year , NSCalendarUnit.Second], fromDate: earliest, toDate: latest, options: NSCalendarOptions())
if (components.year >= 2) {
return "\(components.year) years ago"
} else if (components.year >= 1){
@marioeguiluz
marioeguiluz / HeapSort.swift
Last active November 14, 2017 13:17
Heap Sort Swift
//Safe bounds check for Array
//http://stackoverflow.com/questions/25329186/safe-bounds-checked-array-lookup-in-swift-through-optional-bindings
extension Collection {
/// Returns the element at the specified index iff it is within bounds, otherwise nil.
subscript (safe index: Index) -> Iterator.Element? {
return index >= startIndex && index < endIndex ? self[index] : nil
}
}
//Helper function to exchange position in one array
@alexnodejs
alexnodejs / async-calls
Created May 14, 2015 00:00
Wait for multiple async calls and do something after that (Swift)
let url = NSURL(string: "https://www.google.net")
let request = NSURLRequest(URL: url!)
let group = dispatch_group_create()
for index in 1...100 {
dispatch_group_enter(group)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
println("\(index) complete")
dispatch_group_leave(group)
}
@scottdelly
scottdelly / FacebookLogin.swift
Last active April 23, 2021 21:43
Facebook Login with iOS SDK 4.0 in Swift
//If you have a Bridging-Header:
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
//In your AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [String: AnyObject]?) -> Bool {
//App launch code
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
//Optionally add to ensure your credentials are valid:
func sumRecursively(numbers: [Int], _ acc:Int = 0) -> Int {
if let head = numbers.first {
let tail = Array(dropFirst(numbers))
return sumRecursively(tail, head + acc)
} else {
return acc
}
}
let myNumbers = [1,2,3,4,5]
@katoy
katoy / draw.swift
Last active October 28, 2016 09:48
draw line in playground (Xcode 6.1)
// See http://techlife.cookpad.com/entry/2014/11/12/170041
//
// 本来はplayground用に用意されているXCPlaygroundフレームワークのXCPShowViewを使って
// Timelineに表示することが可能ですが、現行のXcode6.1でiOS用にUIKitを使って表示した場合
// コンソールにエラーが出てしまうため使用していません。
//
import UIKit
// ビューのサイズ
@ShamylZakariya
ShamylZakariya / debounce.swift
Created September 4, 2014 21:01
Simple Swift Debouncer
func debounce( delay:NSTimeInterval, #queue:dispatch_queue_t, action: (()->()) ) -> ()->() {
var lastFireTime:dispatch_time_t = 0
let dispatchDelay = Int64(delay * Double(NSEC_PER_SEC))
return {
lastFireTime = dispatch_time(DISPATCH_TIME_NOW,0)
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
@sebmarkbage
sebmarkbage / transferring-props.md
Last active September 27, 2024 00:10
Deprecating transferPropsTo

Deprecating transferPropsTo

It's a common pattern in React to wrap a component in an abstraction. The outer component exposes a simple property to do something that might have more complex implementation details.

We used to have a helper function called transferPropsTo. We no longer support this method. Instead you're expected to use a generic object helper to merge props.

render() {
 return Component(Object.assign({}, this.props, { more: 'values' }));
@staltz
staltz / introrx.md
Last active July 13, 2025 10:33
The introduction to Reactive Programming you've been missing
@nlothian
nlothian / Penn Treebank II Tags.md
Last active April 15, 2025 13:39
Penn Treebank II Tags