Skip to content

Instantly share code, notes, and snippets.

View sgr-ksmt's full-sized avatar
👉
👁👃👁

Suguru Kishimoto sgr-ksmt

👉
👁👃👁
View GitHub Profile
_ = (Date(timeIntervalSinceNow: -100)..<Date()) ~= Date(timeIntervalSinceNow: -50)
// true
@sgr-ksmt
sgr-ksmt / StoryboardInstantiatable.swift
Last active October 12, 2023 09:11
Create UIViewController from storyboard and pass parameter safely.
public protocol StoryboardInstantiatable {
static var storyboardName: String { get }
static var viewControllerIdentifier: String? { get }
static var bundle: Bundle? { get }
}
public extension StoryboardInstantiatable where Self: UIViewController {
public static var storyboardName: String {
return String(describing: self)
}
extension CustomStringConvertible where Self: RawRepresentable {
var description: String {
return String(describing: self.rawValue)
}
}
enum Hoge: Int, CustomStringConvertible {
case a
}
@sgr-ksmt
sgr-ksmt / acknowledgement_generator.swift
Created November 16, 2016 10:30
licenses plist generator: support CocoaPods/Carthage
#!/usr/bin/env xcrun --sdk macosx swift
import Foundation
let version = "1.1"
var debug = false
// MARK: Exit status code
enum ExitCode: Int32 {
case success = 0
@sgr-ksmt
sgr-ksmt / file.swift
Last active May 8, 2023 03:51
UIScrollView: scroll top/bottom/left/right perfectly! http://blog.sgr-ksmt.org/2016/11/11/uiscrollview_scroll_extension/
extension UIScrollView {
public enum ScrollDirection {
case top
case bottom
case left
case right
}
public func scroll(to direction: ScrollDirection, animated: Bool) {
let offset: CGPoint
@sgr-ksmt
sgr-ksmt / file.swift
Created September 26, 2016 05:55
Safe DispatchQueue.main.sync (Swift3.0)
extension DispatchQueue {
class func mainSyncSafe(execute work: () -> Void) {
if Thread.isMainThread {
work()
} else {
DispatchQueue.main.sync(execute: work)
}
}
class func mainSyncSafe<T>(execute work: () throws -> T) rethrows -> T {
@sgr-ksmt
sgr-ksmt / gist:295b069efe1452208fb737b60f5c58de
Created September 22, 2016 14:43
dispatch after in Swift 3.0
DispatchQueue.main.asyncAfter(deadline: .now() + __var__) {
}
@sgr-ksmt
sgr-ksmt / echo_2_2.swift
Created August 30, 2016 10:22
swift scripting for 2.2 and 3.0!
#!/usr/bin/env xcrun swift
let arguments = Array(Process.arguments.dropFirst())
print(arguments.joinWithSeparator(","))
@sgr-ksmt
sgr-ksmt / .swift
Created August 29, 2016 11:14
draft
extension ReusableTableViewCell {
static var cellIdentifier: String {
return String(self)
}
}
@sgr-ksmt
sgr-ksmt / sample.swift
Last active August 29, 2016 04:58
UIView animate process: animate or not
extension UIView {
static func animateIfNeeded(animated: Bool, duration: NSTimeInterval, animations: () -> Void, completion: ((Bool) -> Void)?) {
if animated {
self.animateWithDuration(duration, animations: animations, completion: completion)
} else {
animations()
completion?(true)
}
}
}