Skip to content

Instantly share code, notes, and snippets.

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

Suguru Kishimoto sgr-ksmt

👉
👁👃👁
View GitHub Profile
import Foundation
import UIKit
typealias Replacer = String -> String
extension String {
var first: String {
return String(characters.prefix(1))
}
}
@sgr-ksmt
sgr-ksmt / String+split.swift
Created March 22, 2016 03:36
String#split
public extension String {
public func split(length: Int) -> [String] {
let array = self.characters.map { String($0) }
let limit = self.characters.count
return 0
.stride(to: limit, by: length)
.map({
array[$0..<$0.advancedBy(length, limit: limit)].joinWithSeparator("")
})
}
@sgr-ksmt
sgr-ksmt / NSNotificationObservable.swift
Last active July 22, 2016 01:08
Notification with enum.
import Foundation
import UIKit
enum NotificationKey: String {
case Hoge
}
//--------------- pattern1: use protocol-extension
protocol NSNotificationObservable: RawRepresentable {
var rawValue: String { get }
@sgr-ksmt
sgr-ksmt / script.sh
Created August 22, 2016 10:23
Output percentage of languages between Swift and Objective-C.
#!/bin/bash
calc_percentage() {
echo "$1/$2*100" | bc -l | awk '{s=($0<0)?-1:1;print int($0*s*100+0.5)/100/s;}'
}
arr=($(cloc --exclude-dir=Pods,Carthage --include-lang=Swift,Objective\ C ./ | sed -e '1,5d' | sed 's/-//g' | sed 's/Objective C/ObjectiveC/g' | awk '{print $5}' | grep -v -e 'code' -e '^S*$'))
swift_files=${arr[0]}
objc_files=${arr[1]}
sum=${arr[2]}
@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)
}
}
}
@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 / 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 / gist:295b069efe1452208fb737b60f5c58de
Created September 22, 2016 14:43
dispatch after in Swift 3.0
DispatchQueue.main.asyncAfter(deadline: .now() + __var__) {
}
extension CustomStringConvertible where Self: RawRepresentable {
var description: String {
return String(describing: self.rawValue)
}
}
enum Hoge: Int, CustomStringConvertible {
case a
}
_ = (Date(timeIntervalSinceNow: -100)..<Date()) ~= Date(timeIntervalSinceNow: -50)
// true