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 / file0.swift
Last active February 24, 2017 03:21
lazy var の遅延参照?という面白い動き ref: http://qiita.com/lovee/items/5f68762cb46c70af0c9a
struct Data {
var int = 1
mutating func increase(completion: (Int) -> Void) {
self.int += 1
completion(self.int)
}
}
@el-hoshino
el-hoshino / file0.swift
Last active February 18, 2017 00:41
配列の特定要素を抽出するのに for 文の代わりに if 文を使いましょう ref: http://qiita.com/lovee/items/1ae88d41b573b5d1012c
let list = ["Apple", "Boy", "Serval Cat"]
func search(for target: String) {
for item in list {
if item.lowercased().hasSuffix(target.lowercased()) {
print("We have a \(item)")
return
}
}
@el-hoshino
el-hoshino / TanakaLover
Created February 10, 2017 08:29
今更聞けない?Struct と Class の使い分け方(補足) ref: http://qiita.com/lovee/items/0bf5e663dd74983cc0d3
import Foundation
protocol BankAccountDelegate: class {
func account(_ account: BankAccount, changePasswordTo newPassword: String) throws -> Int
func account(_ account: BankAccount, transfer amount: Int, to receiver: Int) throws
}
//class BankAccount {
struct BankAccount {
@el-hoshino
el-hoshino / file0.swift
Last active December 21, 2016 06:21
Swift 3.0 の String の正規表現 ref: http://qiita.com/lovee/items/d5f1ecfbff90ffd5b0e5
range(of searchString: String, options: String.CompareOptions, range: Range<String.Index>?, locale: Locale?)
@el-hoshino
el-hoshino / after
Last active November 6, 2018 04:46
Swift で Class-Only Protocol を定義する ref: https://qiita.com/lovee/items/a686e11f00b31323e683
protocol SomeProtocol: class {
var aVariable: Int { get set }
func increase()
}
extension SomeProtocol {
func increase() {
self.aVariable += 1
}
}
@el-hoshino
el-hoshino / GCD.swift
Created April 29, 2016 12:56
dispatch いちいち書くの面倒だったら…自分で改良すればいいじゃない! ref: http://qiita.com/lovee/items/facfe77f6eab16651f08
import Foundation
struct GCD {
enum Thread {
case Main
case Static(queue: dispatch_queue_t)
case Dynamic(name: String, attribute: QueueAttribute)
@el-hoshino
el-hoshino / ChildViewController
Last active April 21, 2016 13:18
遅延生成で生成時に色々設定を入れる方法 ref: http://qiita.com/lovee/items/0bf551b5ef65ad00cccc
import UIKit
class ChildViewController: UIViewController {
init() {
super.init(nibName: nil, bundle: nil)
print("Child controller inited")
}
required init?(coder aDecoder: NSCoder) {
@el-hoshino
el-hoshino / file1.txt
Last active April 4, 2016 06:23
ソースコードで想いを届ける(Swift コマンドライン編) ref: http://qiita.com/lovee/items/a225e27610a663035cd8
% swift iLoveYou.swift 春彦母
私は春彦母のことが好きです
@el-hoshino
el-hoshino / file0.swift
Last active March 30, 2016 13:58
Range<Int> に distance 作って Int に指定範囲で乱数を作成させる extension ref: http://qiita.com/lovee/items/942de12e9edb5ad01f48
//普通に distanceTo を使うからこの方法やめる
//extension Range where Element: IntegerType {
extension Range {
//普通に distanceTo を使うからこの方法やめる
//var distance: Element {
// return self.endIndex - self.startIndex
//}
var distance: Element.Distance {
@el-hoshino
el-hoshino / after
Created March 24, 2016 08:35
【TIPS】protocol の対応は extension で! ref: http://qiita.com/lovee/items/2b96b9db0fbe7fccfbc1
class Person {
var car: Car?
func goForADrive() {
self.car?.drive()
}
func buyGasoline() {