Skip to content

Instantly share code, notes, and snippets.

View el-hoshino's full-sized avatar
🏠
Working from home

Elvis Shi el-hoshino

🏠
Working from home
View GitHub Profile
@el-hoshino
el-hoshino / CodePiece.swift
Created March 27, 2018 09:08 — forked from takasek/CodePiece.swift
ポプテピピックが完成したら竹書房を破壊するSwiftコード #CodePiece
enum PopTeamEpic: String {
case po = "ポ"
case p = "プ"
case teame = "テピ"
case pic = "ピック"
}
struct 蒼井翔太: Sequence, IteratorProtocol {
mutating func next() -> PopTeamEpic? {
switch arc4random_uniform(4) {
case 0: return .po
@el-hoshino
el-hoshino / Lazy.swift
Created March 5, 2018 07:45
Declaring a lazy-initialized object with `let` in Swift
class Lazy <T> {
private var t: T?
private let initializer: () -> T
private let semaphore = DispatchSemaphore(value: 1)
init(_ initializer: @escaping () -> T) {
self.initializer = initializer
@el-hoshino
el-hoshino / CodePiece.swift
Created March 1, 2018 01:19
🤔 #CodePiece #tryswiftconf
let myVariable: Int = { while(true) {} }()
@el-hoshino
el-hoshino / CodePiece.swift
Created February 16, 2018 14:17
🤔 #swift #CodePiece
protocol TestProtocol {
associatedtype Element
}
struct TestStruct<Element>: TestProtocol {
let element: Element
}
// error: type 'TestStruct<Element>' does not conform to protocol 'TestProtocol'
@el-hoshino
el-hoshino / Scanner.swift
Created January 30, 2018 03:30
Using Scanner to scan String to Int in Swift (Objective-C way)
let string = "123"
let scanner = Scanner(string: string)
var int = NSNotFound
scanner.scanInt(&int)
print(int)
@el-hoshino
el-hoshino / file0.swift
Last active January 25, 2018 05:33
【バグ】NSObject を継承したオブジェクトを unowned プロパティーとして保持すると、それを保持しているインスタンスを print すると落ちる ref: https://qiita.com/lovee/items/56555e3c7cf880a44efd
import Foundation
class C: NSObject {
}
struct D {
unowned let x: C
}
@el-hoshino
el-hoshino / Operator.swift
Last active September 7, 2017 06:52
Swift 4 で「プラマイ」範囲を作る ref: http://qiita.com/lovee/items/4159b5c64b371ecb0b8f
infix operator ± : RangeFormationPrecedence
extension Numeric where Self: Comparable {
public static func ± (lhs: Self, rhs: Self) -> ClosedRange<Self> {
let added = lhs + rhs
let subtracted = lhs - rhs
let lowerBound = min(added, subtracted)
let upperBound = max(added, subtracted)
return lowerBound ... upperBound
@el-hoshino
el-hoshino / Playground.swift
Last active July 26, 2017 09:25
Swift でアニメーションの連続実行をしてみる話 ref: http://qiita.com/lovee/items/460cbb02a345e0ff7910
let base = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
base.backgroundColor = .white
PlaygroundPage.current.liveView = base
let view = UIView(frame: CGRect(x: 100, y: 100, width: 200, height: 200))
view.backgroundColor = .blue
base.addSubview(view)
// アニメーションブロック
UIView.animate(eachBlockDuration: 1, eachBlockDelay: 0, eachBlockOptions: .curveEaseInOut, animationBlocks:
@el-hoshino
el-hoshino / CodePiece.swift
Created June 12, 2017 09:16
なんかすごい黒魔術みたいなのを発見したw #swift #CodePiece
extension Int {
static func `init`(float: Float) -> Float {
return float
}
}
let float = Int(float: 3.14)
print(float)
@el-hoshino
el-hoshino / CodePiece.swift
Created March 2, 2017 07:56
そういえば今日unsafePointerの話あったけど、ポインター使うとこんなこともできるよ #CodePiece #tryswiftconf
var age = 28
let unknown = withUnsafePointer(to: &age) { $0.advanced(by: 12).pointee }
print(unknown) // 7597125269629269102