Skip to content

Instantly share code, notes, and snippets.

import UIKit
private func angle(_ degree: Double) -> CGFloat {
return CGFloat(Double.pi * degree / 180.0)
}
let bounds = CGRect(x: 0, y: 0, width: 150, height: 100)
// corner radius for bottom right, bottom left, top left, and top right
let r: (CGFloat, CGFloat, CGFloat, CGFloat) = (5, 10, 20, 40)
@bricklife
bricklife / BindingSource+Extensions.swift
Created August 25, 2017 10:39
bind(to:) function for ReactiveSwift
import ReactiveSwift
import enum Result.NoError
extension BindingSource {
@discardableResult
func bind<P: BindingTargetProvider>(to provider: P) -> Disposable? where P.Value == Value, Error == NoError {
return provider <~ self
}
@bricklife
bricklife / CodePiece.swift
Created August 10, 2017 22:46
16進数文字列からDataを生成するやつその3。ずらしてzipして間引いてみた。Rxのbufferが欲しい #CodePiece
let hexString = "0123456789abcdefABCDEF"
let bytes = zip(hexString.characters, hexString.characters.dropFirst())
.enumerated()
.filter { $0.offset % 2 == 0 }
.map { String([$0.element.0, $0.element.1]) }
.flatMap { UInt8($0, radix: 16) }
Data(bytes: bytes) as NSData // <01234567 89abcdef abcdef>
@bricklife
bricklife / CodePiece.swift
Created August 10, 2017 22:16
16進数文字列からDataを生成するString.Index版。たぶんさっきのと同じ結果 #CodePiece
let hexString = "0123456789abcdefABCDEF"
var bytes = [UInt8]()
var index = hexString.startIndex
while let i = hexString.index(index, offsetBy: 2, limitedBy: hexString.endIndex) {
let byteLiteral = hexString.substring(with: index..<i)
guard let byte = UInt8(byteLiteral, radix: 16) else { continue }
bytes.append(byte)
index = i
@bricklife
bricklife / CodePiece.swift
Created August 10, 2017 22:05
16進数文字列からDataを生成するやつ。もっと綺麗に書けそう #CodePiece
let hexString = "0123456789abcdefABCDEF"
let even = hexString.characters.enumerated().filter { $0.offset % 2 == 0 }.map { $0.element }
let odd = hexString.characters.enumerated().filter { $0.offset % 2 == 1 }.map { $0.element }
let bytes = zip(even, odd).flatMap { UInt8(String([$0.0, $0.1]), radix: 16) }
Data(bytes: bytes) as NSData // <01234567 89abcdef abcdef>
//
// BLEPeripheral.swift
// BLEPeripheral
//
// Created by ooba on 26/07/2017.
// Copyright © 2017 mercari. All rights reserved.
//
import Foundation
import CoreBluetooth
//
// BLEDump.swift
// BLEDump
//
// Created by Shinichiro Oba on 2017/07/25.
// Copyright © 2017 Shinichiro Oba. All rights reserved.
//
import Foundation
import CoreBluetooth
//: Playground - noun: a place where people can play
import UIKit
func createImage(text: String, fontSize: CGFloat, imageSize: CGSize) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(imageSize, false, 0)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
let rect = CGRect(x: 0, y: 0, width: imageSize.width, height: imageSize.height)
@bricklife
bricklife / APIKit+Rx+RAC.swift
Created March 28, 2017 18:00
APIKit 3 + RxSwift 3 + ReactiveCocoa 5
import Foundation
import APIKit
import RxSwift
import ReactiveSwift
import Result
extension Session : RxSwift.ReactiveCompatible {}
extension RxSwift.Reactive where Base: Session {
@bricklife
bricklife / lldb-plugin-json.py
Created January 13, 2017 08:12
LLDB Plugin - json
#!/usr/bin/env python
import lldb
def process(debugger, command, result, internal_dict):
lldb.debugger.HandleCommand("""
expr -l swift --
func $process(object: AnyObject) {
func json(object: AnyObject) -> String {
if let data = try? NSJSONSerialization.dataWithJSONObject(object, options: .PrettyPrinted) {