Skip to content

Instantly share code, notes, and snippets.

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

Suguru Kishimoto sgr-ksmt

👉
👁👃👁
View GitHub Profile
@sgr-ksmt
sgr-ksmt / EnumEnumerable.swift
Created February 26, 2016 04:32
Enumerable enum protocol
public protocol EnumEnumerable {
typealias Case = Self
}
public extension EnumEnumerable where Case: Hashable {
private static var generator: AnyGenerator<Case> {
var n = 0
return anyGenerator {
defer { n += 1 }
let next = withUnsafePointer(&n) { UnsafePointer<Case>($0).memory }
@sgr-ksmt
sgr-ksmt / rx_reachedBottom?.swift
Last active March 3, 2016 16:33
rx_reachedBottom??
import RxSwift
import RxCocoa
public extension UIScrollView {
public var rx_reachedBottom: Observable<Bool> {
return rx_contentOffset.map { $0.y == (self.contentSize.height - self.frame.height) }
}
}
@sgr-ksmt
sgr-ksmt / CodePiece.swift
Created March 4, 2016 13:34
CGRectZeroと書くべきか、CGRect.zeroと書くべきか、省略して.zeroと書くべきか... #CodePiece
let view = UIView(frame: CGRectZero)
// ↓
let view = UIView(frame: CGRect.zero)
// ↓
let view = UIView(frame: .zero)
@sgr-ksmt
sgr-ksmt / .swiftlint.yml
Created March 9, 2016 01:14
My .swiftlint.yml
# rules
#- colon
#- control_statement
#- file_length
#- force_cast
#- function_body_length
#- leading_whitespace
#- line_length
#- nesting
#- operator_whitespace
public protocol FilePathConvertible {
var filePath: String { get }
var fileURL: NSURL { get }
}
extension String: FilePathConvertible {
public var filePath: String {
return self
}
public var fileURL: NSURL {
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 / EnumEnumerable.swift
Created June 21, 2016 06:18
EnumEnumerable for Swift3.0
public protocol EnumEnumerable {
associatedtype Case = Self
}
public extension EnumEnumerable where Case: Hashable {
private static var iterator: AnyIterator<Case> {
var n = 0
return AnyIterator {
defer { n += 1 }
let next = withUnsafePointer(&n) { UnsafePointer<Case>($0).pointee }
@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]}