Skip to content

Instantly share code, notes, and snippets.

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

Kang Byeonghak ohtwo

🌙
° ☾ ☆ ¸. ¸ ★ :.  . • ○ ° ★
View GitHub Profile
import UIKit
public extension NSCollectionLayoutAnchor {
struct Offset {
fileprivate var absolute: CGPoint?
fileprivate var fractional: CGPoint?
private init(absolute: CGPoint?, fractional: CGPoint?) {
self.absolute = absolute
self.fractional = fractional
@ohtwo
ohtwo / SheetModalPresentationController.swift
Created October 26, 2022 04:52 — forked from vinczebalazs/SheetModalPresentationController.swift
A presentation controller to use for presenting a view controller modally, which can be dismissed by a pull down gesture. The presented view controller's height is also adjustable.
import UIKit
extension UIView {
var allSubviews: [UIView] {
subviews + subviews.flatMap { $0.allSubviews }
}
func firstSubview<T: UIView>(of type: T.Type) -> T? {
allSubviews.first { $0 is T } as? T
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
}
@ohtwo
ohtwo / BytesPlayground.swift
Created June 25, 2018 01:51 — forked from brennanMKE/BytesPlayground.swift
Copy bytes from Data with Swift
import Foundation
let size = MemoryLayout<Int16>.stride
let data = Data(bytes: [1, 0, 2, 0, 3, 0]) // little endian for 16-bit values
let int16s = data.withUnsafeBytes { (bytes: UnsafePointer<Int16>) in
Array(UnsafeBufferPointer(start: bytes, count: data.count / size))
}
let length = data.count * MemoryLayout<Int16>.stride
@ohtwo
ohtwo / NSTimer.md
Created May 30, 2018 10:33 — forked from radex/NSTimer.md
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!")
}
@ohtwo
ohtwo / README.md
Created January 18, 2018 12:05 — forked from unnamedd/README.md
Xcode pre-action to build custom Info.plist

Automatic build versions from git in Xcode (and other goodies)

Installation procedure for pre-build actions to automatically populate Xcode Info.plist with dynamic data.

1. Xcode Scheme pre-action

Edit Xcode Scheme and add a pre-action script. Copy the contents of preaction.sh into the pre-action script box.

import Foundation
public struct Meter {
fileprivate var value: Double
public init(_ value: Double) {
self.value = value
}
public init(_ value: Int) {
@ohtwo
ohtwo / ioslocaleidentifiers.csv
Created January 22, 2016 12:25 — forked from jacobbubu/ioslocaleidentifiers.csv
iOS Locale Identifiers
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
mr Marathi
bs Bosnian
ee_TG Ewe (Togo)
ms Malay
kam_KE Kamba (Kenya)
mt Maltese
ha Hausa
es_HN Spanish (Honduras)
ml_IN Malayalam (India)
ro_MD Romanian (Moldova)
@ohtwo
ohtwo / NSDate+RFC3339.swift
Created November 19, 2015 06:49 — forked from algal/NSDate+RFC3339.swift
NSDate+RFC3339
extension NSDate {
var stringFormattedAsRFC3339: String {
return rfc3339formatter.stringFromDate(self)
}
class func dateFromRFC3339FormattedString(rfc3339FormattedString:String) -> NSDate?
{
/*
NOTE: will replace this with a failible initializer when Apple fixes the bug
that requires the initializer to initialize all stored properties before returning nil,
@ohtwo
ohtwo / dispatch_once.swift
Last active September 8, 2015 03:38 — forked from kristopherjohnson/dispatch_once.swift
Example of using dispatch_once() in Swift
import Foundation
var token: dispatch_once_t = 0
func test() {
dispatch_once(&token) {
println("This is printed only on the first call to test()")
}
println("This is printed for each call to test()")
}