Skip to content

Instantly share code, notes, and snippets.

View dolphinSuPixnet's full-sized avatar

dolphin su dolphinSuPixnet

  • PIXNET
  • Taipei, Taiwan
View GitHub Profile
@dolphinSuPixnet
dolphinSuPixnet / TestCPU.swift
Created September 2, 2015 09:39
Test Your CPU using Swift
#!/usr/bin/env swift
import Foundation
func oneLoop() -> Double{
var sumValue = 0
let startTime = NSDate().timeIntervalSince1970
for i in 1...100000000 {
if (i % 2 == 1) {
sumValue++
} else {
@dolphinSuPixnet
dolphinSuPixnet / 0_reuse_code.js
Last active September 15, 2015 01:18
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@dolphinSuPixnet
dolphinSuPixnet / HTML Attributed string
Last active September 16, 2015 01:49
We can use -initWithData:options:documentAttributes:error: create an HTML based string. 利用 -initWithData:options:documentAttributes:error: 建立 HTML 語法為基礎的 attributed string.
NSString *origin = @"<b><i>hello world</i></b>";
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[origin dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:YES]
options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
documentAttributes:nil
error:nil];
_label.attributedText = attributedString;
@dolphinSuPixnet
dolphinSuPixnet / HTML based attributed string
Created September 16, 2015 01:52
We can use this create HTML based attributed string. 我們可以這樣建立一個 HTML 樣式的 attributed string
var attrStr = NSAttributedString(
data: "<b><i>text</i></b>".dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true),
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil,
error: nil)
label.attributedText = attrStr
@dolphinSuPixnet
dolphinSuPixnet / Codility lesson 1: the tape question.
Last active October 16, 2015 09:59
This performance is score 0.
-(int)solution:(NSArray *)A{
int minimum = 0;
for(int i=1; i<A.count; i++){
NSRange range1 = NSMakeRange(0, i);
NSArray *array1 = [A subarrayWithRange:range1];
int sum1 = 0;
for (int j = 0; j<array1.count; j++) {
NSNumber *number = array1[j];
sum1 += number.integerValue;
}
@dolphinSuPixnet
dolphinSuPixnet / NSLinguisticTagger example
Created October 22, 2015 06:41
NSLinguisticTagger 的一個簡單範例
/*判斷這個 error message 是不是中文:http://stackoverflow.com/questions/6325073/detect-language-of-nsstring*/
//告訴 tagger,我們想要它判斷哪些東西
NSArray *tagsSchemes = @[NSLinguisticTagSchemeLanguage];
NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:tagsSchemes options:0];
//告訴 tagger,我們要它判斷哪句話
[tagger setString:errorMessage];
NSRange range = NSMakeRange(0, errorMessage.length - 1);
//告訴 tagger,我們要它判斷的那句話會是哪種語言,不然它可能會判斷成日文或簡中
NSOrthography *orthography = [NSOrthography orthographyWithDominantScript:@"Hant" languageMap:@{@"Hant":@[@"zh-Hant"]}];
[tagger setOrthography:orthography range:range];
@dolphinSuPixnet
dolphinSuPixnet / 用來產生 POST request 的 NSMutableURLRequest Category
Created November 4, 2015 07:33
這樣可以產生一個 http method 為 POST,http body 為 multipart/form-data 的 url request
+(instancetype)PIXURLRequestForOauth2POST:(NSString *)urlString parameters:(NSDictionary *)parameters{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
request.HTTPMethod = @"POST";
//設定 http header
NSString *contentTypeValue = [NSString stringWithFormat:@"multipart/form-data; charset=utf-8; boundary=%@", kBoundaryConstant];
[request addValue:contentTypeValue forHTTPHeaderField:@"Content-Type"];
//設定 http body
[request setHTTPBody:[stringForFormData(parameters) dataUsingEncoding:NSUTF8StringEncoding]];
return request;
}