Skip to content

Instantly share code, notes, and snippets.

@yycking
yycking / ble.md
Last active December 26, 2017 10:28
ble筆記
  • 角色
    • 發佈
      • 週邊(peripheral)
        • 廣播(broadcast)
      • 中心(central)
        • 掃瞄(scan)
    • 連線
      • server
      • client
  • 搭配
@yycking
yycking / bindActionCreators.js
Last active December 15, 2017 02:45
bindActionCreators的實際作用
const { createStore, bindActionCreators } = Redux
const reducer = (state = {紅:0, 綠:0, 藍:0}, action) => {
switch (action.type){
case '紅': return {...state, 紅:action.值}
case '綠': return {...state, 綠:action.值}
case '藍': return {...state, 藍:action.值}
default: return state
}
}
@yycking
yycking / Hant-Hans.swift
Last active October 12, 2017 02:45
繁轉簡,簡轉繁,中文轉拼音,拼音轉注音
extension StringTransform {
public static let 简 = StringTransform("Hant-Hans")
public static let 繁 = StringTransform("Hans-Hant")
public static let 拼 = StringTransform("Han-Latin")
public static let 注 = StringTransform("Han-Latin;Latin-Bopomofo")
}
let 繁转简 = "繁轉簡".applyingTransform(.简, reverse: false)
let 簡轉繁 = "简转繁".applyingTransform(.繁, reverse: false)
let pīnyīn = "拼音".applyingTransform(.拼, reverse: false)
@yycking
yycking / UIBarButtonSystemItem+image.swift
Created September 22, 2017 08:15
Get UIBarButtonItem icon
extension UIBarButtonSystemItem {
func image() -> UIImage? {
let tempItem = UIBarButtonItem(barButtonSystemItem: self,
target: nil,
action: nil)
// add to toolbar and render it
let bar = UIToolbar()
bar.setItems([tempItem],
animated: false)
@yycking
yycking / UILabel+Link.m
Last active December 9, 2019 01:55
Make a clickable link in an UILabel
@yycking
yycking / pipe-operator.swift
Created February 22, 2017 10:25
pay respect to pipe-forward (|>) operator and Then(Super sweet syntactic sugar for Swift initializers.)
infix operator |>: AdditionPrecedence
public func |> <T,U>(lhs: T, rhs: (T) -> U) -> U {
return rhs(lhs)
}
public func |> <T>(lhs: T, rhs: inout T) {
rhs = lhs
}
infix operator <|: AdditionPrecedence
@yycking
yycking / facebook-bot.php
Created February 17, 2017 05:24
Simple Facebook Chat Bot for PHP
<?php
ob_start();
function send($data) {
$token = file_get_contents('.token');
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$token;
$content = json_encode($data);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
@yycking
yycking / Data+=.swift
Created February 15, 2017 06:02
+ and += operator for Data in swift
public protocol DataConvertible {
static func + (lhs: Data, rhs: Self) -> Data
static func += (lhs: inout Data, rhs: Self)
}
extension DataConvertible {
public static func + (lhs: Data, rhs: Self) -> Data {
var value = rhs
let data = Data(buffer: UnsafeBufferPointer(start: &value, count: 1))
return lhs + data
@yycking
yycking / self-signed-SSL.swift
Created January 23, 2017 10:26
accept a self-signed SSL certificate
import UIKit
class NSURLSessionPinningDelegate: NSObject, URLSessionDelegate {
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void) {
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
if let trust = challenge.protectionSpace.serverTrust,
let pem = Bundle.main.path(forResource: "https", ofType: "cer"),
let data = NSData(contentsOfFile: pem),
@yycking
yycking / UILabel.m
Created January 4, 2017 03:28
將UILabel中的圖字置中
UILabel *label = [[UILabel alloc] initWithFrame:tableView.bounds];
label.numberOfLines = 0;
NSMutableAttributedString *attachmentString = [NSMutableAttributedString new];
NSTextAttachment *imageAttachment = [NSTextAttachment new];
imageAttachment.image = [UIImage imageNamed:images[index]];
NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:imageAttachment];
[attachmentString appendAttributedString:imageString];