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
import Foundation
let candidates = ["ガルパン", "は", "いい", "ぞ"]
var createdPhrase = [String](count: candidates.count, repeatedValue: "")
for i in 0 ..< Int.max {
if createdPhrase != candidates {
let randomNumber = Int(arc4random_uniform(UInt32(candidates.count)))
let nextWord = candidates[randomNumber]
print(nextWord)
@el-hoshino
el-hoshino / file0.swift
Created February 10, 2016 06:24
配列の一部範囲を取り出して新しい配列を生成する方法のパフォーマンス比較 ref: http://qiita.com/lovee/items/155a662146445ca4a79c
let array = [Int](0 ..< 10000)
let number = array[1000] // number = 1000
@el-hoshino
el-hoshino / file0.swift
Last active December 24, 2015 05:44
十六進数の文字列から実際の数値を取得する ref: http://qiita.com/lovee/items/eac017d1dcaf52541d3d
let hexString = "FF"
var hex:UInt32 = 0x0
let scanner:NSScanner = NSScanner(string: hexString)
scanner.scanHexInt(&hex)
print(hex) //255
@el-hoshino
el-hoshino / TestModel
Last active August 29, 2017 02:44
ガチで Swift でプログラム組んで1年経っての心得 ref: http://qiita.com/lovee/items/555a5b76097347aa2367
import UIKit
protocol TestModelDelegate {
func display(something: NSObject)
}
class TestModel: NSObject {
var displayedString: String
var delegate: TestModelDelegate?
@el-hoshino
el-hoshino / file0.swift
Last active November 27, 2015 07:06
「要素が範囲に含まれる」という演算子を作ってみた ref: http://qiita.com/lovee/items/32ceca1effbeceb959d6
let a = 1
0 ..< 10 ~= a // true
@el-hoshino
el-hoshino / file0.swift
Created November 25, 2015 03:07
UIImage の画素データを Array で取得する方法 ref: http://qiita.com/lovee/items/74c0310a50d1b752ceb8
func getByteArrayFromImage(imageRef: CGImageRef) -> [UInt8] {
let data = CGDataProviderCopyData(CGImageGetDataProvider(imageRef))
let length = CFDataGetLength(data)
var rawData = [UInt8](count: length, repeatedValue: 0)
CFDataGetBytes(data, CFRange(location: 0, length: length), &rawData)
return rawData
}
@el-hoshino
el-hoshino / file0.swift
Last active November 12, 2015 03:34
switch 文で Optional Binding を使いたかった話 ref: http://qiita.com/lovee/items/7bd9aee01d7ccbf72d2b
let dict = [1: 2, 3: 4]
let key = 1
if let value = dict[key] {
print(value)
}
@el-hoshino
el-hoshino / file0.swift
Created October 23, 2015 05:57
UIButton の動作を Callback で設定する方法 ref: http://qiita.com/lovee/items/65b1f10313454fca4a05
import UIKit
class CallbackButton: UIButton {
private var action: (() -> Void)?
init(frame: CGRect, action: (() -> Void)? = nil) {
self.action = action
@el-hoshino
el-hoshino / file0.swift
Last active September 4, 2015 10:34
Swift での Int 型 switch case の落とし穴(?) ref: http://qiita.com/lovee/items/31bc2b4e66a93944c9a6
let a = 0
switch a {
case Int.min ..< 0:
print("負数")
case 0:
print("零")
case 1 ... Int.max:
print("正数")
@el-hoshino
el-hoshino / file0.swift
Last active August 27, 2015 05:55
NSUserDefaults で struct を保存(?)する方法 ref: http://qiita.com/lovee/items/2b39e1c54d6d00adead9
class ViewController: UIViewController {
var settings: UserSettings
init() {
self.settings = UserSettings(a: 0, b: false, c: "NO")
super.init(nibName: nil, bundle: nil)
}