Skip to content

Instantly share code, notes, and snippets.

View halftrue's full-sized avatar
🐟
Busy

叉叉咖 halftrue

🐟
Busy
View GitHub Profile
@halftrue
halftrue / builder.swift
Created May 2, 2017 10:08
A way to initialize object with backward compatibility.
class UserBuilder {
var name: String
var gender: String
init() {
self.name = "default name"
self.gender = "default gender"
}
}
sudo pmset -a GPUSwitch 0
0 ,强制使用核显,
1 , 强制使用独显,
2 ,自动切换图形卡
@halftrue
halftrue / weekDay.c
Created August 12, 2016 15:01
万年历计算星期-基姆拉尔森公式
#include <stdio.h>
/*
* 基姆拉尔森计算公式
* W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7
*/
int week(int y, int m, int d)
{
if (m < 3) {
m += 12;
@halftrue
halftrue / escapedParameters.swift
Created July 26, 2016 12:40
A function can easily turn parameters into a escaped string.
private func escapedParameters(parameters: [String:AnyObject]) -> String {
if parameters.isEmpty {
return ""
} else {
var keyValuePairs = [String]()
for (key, value) in parameters {
// make sure that it is a string value
@halftrue
halftrue / Dictionary+KeysForValue.swift
Created April 29, 2016 09:51 — forked from ijoshsmith/Dictionary+KeysForValue.swift
Find keys mapped to a value in Swift dictionary
extension Dictionary where Value: Equatable {
/// Returns all keys mapped to the specified value.
/// ```
/// let dict = ["A": 1, "B": 2, "C": 3]
/// let keys = dict.keysForValue(2)
/// assert(keys == ["B"])
/// assert(dict["B"] == 2)
/// ```
func keysForValue(value: Value) -> [Key] {
return flatMap { (key: Key, val: Value) -> Key? in
// approach one
extension String {
var length: Int {
return characters.count
}
}
// approach two
extension String {
var length: Int {
return (self as NSString).length