Skip to content

Instantly share code, notes, and snippets.

View ryotapoi's full-sized avatar

Ryota Kurosaki ryotapoi

View GitHub Profile
@ryotapoi
ryotapoi / NSMutableArray.swift
Last active February 24, 2017 08:47
SequenceからNSMutableArrayへの変換
// `public convenience init?(contentsOf url: URL)`
// というメソッドがあるが、Objective-Cからは見えず、Swiftからは引数型で区別できる。
// もっと良い名前があれば変更する。
extension NSMutableArray {
public convenience init<S : Sequence>(contentsOf newElements: S) {
self.init()
newElements.forEach(self.add)
}
}
@ryotapoi
ryotapoi / InstantiableFromStoryboard.swift
Created May 25, 2017 10:45
StoryboardからUIViewControllerを生成する。UIViewControllerひとつにつきStoryboardがひとつある想定
public protocol BundleSearchable {
static func searchBundle() -> Bundle?
}
extension BundleSearchable {
public static func searchBundle() -> Bundle? {
if let anyClass = self as? AnyClass {
return Bundle(for: anyClass)
} else {
return nil
@ryotapoi
ryotapoi / StoryboardLoader.swift
Created May 25, 2017 10:46
StoryboardからUIViewControllerを生成する。 UIViewControllerひとつに複数のStoryboardがある場合を想定。
public struct Storyboard {
public init(_ name: String, bundle: Bundle? = nil) {
self.name = name
self.bundle = bundle
}
public var name: String
public var bundle: Bundle?
public func instantiate<VCType>(_ vcType: VCType.Type = VCType.self, identifier: String? = nil) -> VCType? {
if let identifier = identifier {
let array: [Int] = [1, 2, 3, 4]
for (first, second) in zip(array.dropLast(), array.dropFirst()) {
print(first, second)
}
extension Array {
func eachConsecutive(_ number: Int, body: (ArraySlice<Element>) -> Void) {
var lower = 0
@ryotapoi
ryotapoi / markdown_to_textile.sh
Last active December 4, 2018 10:20
markdownをtextileに変換
#!/bin/bash
# 要pandoc。mac環境に依存している。
# `pbpaste`: クリップボードの内容をペースト
# `pbcopy`: クリップボードにコピー
pandoc --from=markdown --to=textile <(pbpaste) | pbcopy
# githubのmarkdownの場合
# pandoc --from=markdown_github --to=textile <(pbpaste) | pbcopy
let cookie = HTTPCookie(properties: [
    .name: "name1",
    .value: "value1",
    .path: "/",
    .originURL: URL(string: "https://hoge.com")! // StringでもOK
])!
button.imageView?.contentMode = .scaleAspectFit
button.contentHorizontalAlignment = .fill
button.contentVerticalAlignment = .fill

コードを書くと実行時にAspect Fitになる。 Interface Builder上では正しく表示されない。

IBDesignableで指定できるボタンを作ると便利かも?

@ryotapoi
ryotapoi / Publisher.swift
Created May 27, 2019 03:08
Delegateを複数のオブジェクトに配信する
public class Publisher<Subscriber> {
private class Disposer {
private let disposing: () -> Void
fileprivate init(disposing: @escaping () -> Void) {
self.disposing = disposing
}
deinit {
public struct CircularBuffer<Element> {
var beginIndex: Int
var endIndex: Int?
var buffer: [Element?]
var capacity: Int {
return buffer.count
}
extension Array where Element == URLQueryItem {
func stringValue(for name: String) -> String? {
let item = first { $0.name == name }
return item?.value
}
func urlValue(for name: String) -> URL? {
guard let string = stringValue(for: name) else { return nil }
return URL(string: string)