Skip to content

Instantly share code, notes, and snippets.

View ohtwo's full-sized avatar
🌙
° ☾ ☆ ¸. ¸ ★ :.  . • ○ ° ★

Kang Byeonghak ohtwo

🌙
° ☾ ☆ ¸. ¸ ★ :.  . • ○ ° ★
View GitHub Profile
@akisute
akisute / APIClient.swift
Last active October 6, 2015 17:53
Example of APIClient and TestCase in Swift, using AFNetworking/Bolts
import UIKit
// How to make singleton classes in Swift: http://stackoverflow.com/questions/24024549/dispatch-once-singleton-model-in-swift
// Basically using global constants is the most easy and safe way to go
class APIClient: NSObject {
let functionSessionManager:AFHTTPSessionManager
class var sharedInstance:APIClient {
@evgenyneu
evgenyneu / empty_swift_file_template.sh
Last active September 19, 2016 11:08
Empty Swift file template script
#!/bin/bash
#
# This script makes an empty Swift file template.
#
# Usage:
#
# sudo ./change_xcode_template
#
# or specify the Xcode app name
import Foundation
struct Meter {
var value: Double
init(_ value: Double) {
self.value = value
}
var mm: Double { return value * 1000.0 }
@mattt
mattt / regex.swift
Created August 11, 2014 18:08
Creating a regular expression object from a String literal
class Regex {
let pattern: String
let options: NSRegularExpressionOptions!
private var matcher: NSRegularExpression {
return NSRegularExpression(pattern: self.pattern, options: nil, error: nil)
}
required init(pattern: String, options: NSRegularExpressionOptions = nil) {
self.pattern = pattern
@radex
radex / NSTimer.md
Last active May 30, 2018 10:33
Swift Extensions: NSTimer

NSTimer is a great example of an over-verbose, outdated Objective-C API. To run a simple line of code after a delay, you need to write a lot of boilerplate crap.

How about this:

NSTimer.schedule(5.seconds) {
  println("Hello world!")
}
func ==<U: Equatable, T: protocol<RawRepresentable, Equatable> where T.RawValue == U>(lhs: U, rhs: T) -> Bool {
return lhs == rhs.rawValue
}
func !=<U: Equatable, T: protocol<RawRepresentable, Equatable> where T.RawValue == U>(lhs: U, rhs: T) -> Bool {
return lhs != rhs.rawValue
}
override func viewDidLoad() {
super.viewDidLoad()
self.setUpLocationmanager()
let beaconSequence = locationManager.rx_didRangeBeaconsInRegion.map{$0.beacons}
let sortedBeaconSequence = beaconSequence.map{beacons in self.sortBeaconsByRSSI(beacons)}
let nearestBeaconSequence = sortedBeaconSequence.map({$0[0]})
.filter{$0.proximity == CLProximity.Immediate}
.distinctUntilChanged{$0.minor == $1.minor}
@natecook1000
natecook1000 / shuffle.swift
Last active December 11, 2018 06:53
Shuffle/shuffled functions & Array extensions
// (c) 2014 Nate Cook, licensed under the MIT license
//
// Fisher-Yates shuffle as top-level functions and array extension methods
/// Shuffle the elements of `list`.
func shuffle<C: MutableCollectionType where C.Index == Int>(inout list: C) {
let c = count(list)
for i in 0..<(c - 1) {
let j = Int(arc4random_uniform(UInt32(c - i))) + i
swap(&list[i], &list[j])
#!/bin/sh
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
# make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"
# Step 1. Build Device and Simulator versions
xcodebuild -target "AlamoWater" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build
xcodebuild -target "AlamoWater" -configuration ${CONFIGURATION} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build
@danielgalasko
danielgalasko / UITableViewAndUICollectionView+IndexPathForSubview.swift
Last active January 15, 2020 13:56
Easily retrieve the indexPath of cell's subview in UITableView. Great for button taps and TextFields
extension UITableView {
/**
Returns an index path identifying the row and section
of the cell containing the provided view
:param: cellSubview A subview of any given UITableViewCell in the table. Typically this is either a `UIButton` or `UITextField`
*/
func indexPathForCellWithSubview(cellSubview: UIView) -> NSIndexPath? {
let cellFrame = convertRect(cellSubview.bounds, fromView: cellSubview)
let cellCenter = CGPoint(x: CGRectGetMidX(cellFrame), y: CGRectGetMidY(cellFrame))