Skip to content

Instantly share code, notes, and snippets.

class ViewController: UIViewController {
enum ImageViewState: Int {
case open
case close
var opposite: ImageViewState {
switch self {
case .open:
return .close
struct Address : Codable {
var street: String
var zipCode: String
var city: String
var state: String
}
struct AnyCodingKey : CodingKey {
var stringValue: String
@hachinobu
hachinobu / ProtcolType.swift
Last active December 22, 2015 05:24
Protocol Type generic
protocol MyProtocol {
var myName: String { get }
init()
}
struct MyA: MyProtocol {
var myName: String {
return "My Name is A"
@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
@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 / 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 / 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 / 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 / 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 / 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!)
}