Skip to content

Instantly share code, notes, and snippets.

@yycking
yycking / NS_ENUM_STRING.m
Created December 1, 2016 08:35
NS_ENUM 支援字串
#define LIST_OF_ServerAPI \
/*會員*/ \
api(登入, member/login) \
api(FB登入, member/fbLogin)
typedef NS_ENUM(NSInteger, ServerAPI) {
#define api(key, name) key,
LIST_OF_ServerAPI
#undef api
};
@yycking
yycking / NSLocalizedString+Base.m
Last active December 1, 2016 09:06
NSLocalizedString auto load Base.lproj if key not found
#ifndef L
#define L(key) [NSBundle.mainBundle localizedStringForKey:(key)]
#endif
@interface NSBundle (NSLocalizedString_Base)
- (NSString *)localizedStringForKey:(NSString *)key;
@end
@implementation NSBundle (NSLocalizedString_Base)
@yycking
yycking / plcrashReporter_setCrashCallbacks.swift
Created December 12, 2016 10:16
crashReporter.setCrashCallbacks on swift
func crashReporterScreenCapture(info:UnsafeMutablePointer<siginfo_t>, uap:UnsafeMutablePointer<ucontext_t>, context:UnsafeMutablePointer<Void>) {
}
var crashReporterCapture = PLCrashReporterCallbacks()
crashReporterCapture.version = 0
crashReporterCapture.context = nil
crashReporterCapture.handleSignal = crashReporterScreenCapture
crashReporter.setCrashCallbacks(&crashReporterCapture)
@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];
@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 / 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 / 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 / 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 / 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 / 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
}
}