Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
KentarouKanno / reduce.md
Last active August 26, 2016 13:54
reduce

Array reduce

★ 配列の中の2要素から計算して、その結果を返す。
さらにその結果から次の要素を計算して結果を返し、それを配列分繰り返した値を返す。

let numList = [1 ,2 ,3 ,4 ,5]

let value = numList.reduce(0){(a:Int, b:Int) -> Int in
    return a + b
}
@KentarouKanno
KentarouKanno / Array sort.md
Last active April 6, 2017 14:19
Array sort

Array sort

★ クロージャで第一引数と第二引数を比較してソートする。

let numList = [1 ,2 ,3 ,4 ,5]

let value = numList.sorted{(a:Int, b:Int) -> Bool in
    return a > b
}
@KentarouKanno
KentarouKanno / dispatch_semaphore_t.m
Last active November 5, 2015 14:18
dispatch_semaphore_t
#import "Connection.h"
#import "Reachability.h"
@implementation Connection {
BOOL b;
}
-(BOOL) connectionStart {
Reachability *reachability = [Reachability reachabilityWithHostName:@"http://www.apple.com"];
@KentarouKanno
KentarouKanno / 乱数 random number.swift
Last active November 7, 2015 23:45
Min値とMax値を指定して乱数を生成
// Min値とMax値を指定して乱数を生成
func generateRandomNumber(var min min: Int, var max: Int) -> Int {
if min > max {
(min, max) = (max, min)
}
return Int(arc4random_uniform(UInt32(max + 1 - min)) + UInt32(min))
}
@KentarouKanno
KentarouKanno / Playground MarkUp
Last active November 8, 2015 03:08
Playground MarkUp
/*:
# **First** Comment
* * *
*Second Comment*
*****
* Item 1
* Item 2
* Item 3
1. Item 1
extension UINavigationController {
override public func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let bottomLine = CALayer()
bottomLine.frame = CGRectMake(0, self.navigationBar.frame.size.height - 1, self.view.frame.width, 1)
bottomLine.backgroundColor = UIColor.blueColor().CGColor
self.navigationBar.layer.addSublayer(bottomLine)
}
@KentarouKanno
KentarouKanno / Segue Template.md
Last active June 10, 2016 23:58
Segue Template

Segue Template

★ 画面遷移

@IBAction func pushButton(sender: UIButton) {
    // 画面遷移呼び出し
    performSegueWithIdentifier("segueIdentifier", sender: self)
}

// 画面遷移前処理
@KentarouKanno
KentarouKanno / RGBA from UIColor.swift
Last active November 9, 2015 21:01
UIColorからRGBAを取得する
var color = UIColor.orangeColor()
var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
let b = color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
red //=> 1
green //=> 0.5
blue //=> 0
@KentarouKanno
KentarouKanno / NSBundle.md
Last active August 15, 2016 08:05
NSBundle

NSBundle

★ NSBundleを取得する

let bundle = NSBundle.mainBundle()

★ info.plistからkeyを指定して値を取得する

let value: String = NSBundle.mainBundle().objectForInfoDictionaryKey("key") as! String