Skip to content

Instantly share code, notes, and snippets.

@hachinobu
hachinobu / gist:b09262a49043914339e6
Last active August 29, 2015 14:20
Swiftのreduce便利

var evens = [Int]()
for i in 1...10 {
if i % 2 == 0 {
evens.append(i)
}
}
var evenSum = 0
for i in evens {
@hachinobu
hachinobu / gist:1033e403e01ebd59b24f
Last active August 29, 2015 14:21
Swift Optional
let a: String?
let b: String!
let c: String

a = nil //OK
b = nil //OK
c = nil //NG
@hachinobu
hachinobu / gist:f195a286b5f891a3fb90
Created May 12, 2015 07:18
Optional Bindingによる開示

navigationController?.toolbarHidden = false
// let hiddenはnil判定かつOptionalの開示された変数なのでwhereがないと条件が真になってしまう
if  let hidden = navigationController?.toolbarHidden where isbar {
	println(hidden)
}
else {
	println(hidden)
}
@hachinobu
hachinobu / gist:d8dac5fa3f9601c6a0f5
Created July 19, 2015 06:48
ArrayでremoveObject
import Foundation
public func removeObject<T : Equatable>(object: T, inout fromArray array: [T])
{
var index = find(array, object)
array.removeAtIndex(index!)
}
@hachinobu
hachinobu / gist:2bc0113fdae73175a085
Created July 20, 2015 15:05
Core Graphics Tutorial
http://www.raywenderlich.com/90695/modern-core-graphics-with-swift-part-3
@hachinobu
hachinobu / flatmap.swift
Last active August 29, 2015 14:27
Swift flatmap
func flatMap<A, B>(x: A?, f: A -> B?) -> B? {
if let x = x {
return f(x)
} else {
return nil
}
}
@hachinobu
hachinobu / map.swift
Created August 9, 2015 12:49
Swift map
func map<T, U>(xs: [T], f: T -> U) -> [U] {
var result: [U] = []
for x in xs {
result.append(f(x))
}
return result
}
@hachinobu
hachinobu / TableSectionRow.swift
Created August 9, 2015 13:31
TableViewのsectionとrowの情報をEnumを利用して定義してみる
struct TableSectionRowInfo {
enum RowInfo {
case SecOne1
case SecOne2
case SecTwo1
case SecTwo2
case SecTwo3
func rowCount() -> Int {
@hachinobu
hachinobu / optional.sswift
Created September 27, 2015 13:21
OptionalSwift2.0
let someOptional:Int? = 42
if case .Some(let x) = someOptional{
print("someOptional value is \(x)")
}
if case let x? = someOptional{
print("someOptional value is \(x)")
}
let arrayOptionalInts:[Int?] = [nil,1,2,3,nil,5]
@hachinobu
hachinobu / GroupBy.swift
Last active November 15, 2015 10:27
ArrayでGroupBy
import UIKit
public protocol Groupable {
func sameGroupAs(other: Self) -> Bool
}
extension CollectionType {
public typealias ItemType = Self.Generator.Element
public typealias Grouper = (ItemType, ItemType) -> Bool