2015年6月4日 星期四

[IOS/Swift] how to post data to RESTful WebService?

說明:
如何透過RESTful WebService來更新後台的資料?


語法:
var strURL = WS URL;
var request = NSMutableURLRequest(URL: NSURL(string: strURL));
request.HTTPMethod = "POST";
        
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in
            
    if (response != nil)
    {
        var strResponse = NSString(data: data, encoding: NSUTF8StringEncoding);
        //replace the '\' of strResponse
        print(strResponse.stringByReplacingOccurrencesOfString("\"", withString: ""));
    }
}

2015年6月3日 星期三

[C#/Excel PIA] Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 8000401a The server process could not be started because the configured identity is incorrect. Check the username and password.

說明:
使用Excel PIA寫schedule job產出Excel,程式放在64 bits server上,執行出現該錯誤。


解法:
出現此錯誤,主要是COM元件的存取帳戶被設為Interactive,
因此程式執行時,需要有帳戶登入該server。看過網路上的解法,
打開Administrative Tools下的component service -> Computers -> DCOM Config,
找到Microsoft Excel Application或{00024500-0000-0000-C000-000000000046},
右鍵Properties,找到Security tab或identity tab,將帳戶加進去,
但我找不到Excel Application,試過在command line下mmc comexp.msc /32等方法,還是沒顯示,最後是在登錄檔,HKEY_CLASSES_ROOT\AppID\{00020812-0000-0000-C000-000000000046},將某個屬性值的Interactive改掉即可。


參考資料:
https://ovaismehboob.wordpress.com/2013/07/06/production-deployment-issues-when-asp-net-web-application-access-com-component/
http://blogs.technet.com/b/the_microsoft_excel_support_team_blog/archive/2012/11/12/microsoft-excel-does-not-appear-in-dcom-configuration-snap-in.aspx
https://msdn.microsoft.com/en-us/library/ms678426(VS.85).aspx
http://www.cnblogs.com/fengjunkuan/

2015年4月11日 星期六

[IOS/Swift] how to parse json from the webservice

說明:
json可以拆解成Array,以[]表示,例:[{"id": "#123", "name": "Apple"},{"id": "#321", "name": "Google"}]。
另一種是Object,以{"key": "value"}表示,如http://date.jsontest.com/提供的:
{
   "time": "03:53:25 AM",
   "milliseconds_since_epoch": 1362196405309,
   "date": "03-02-2013"
}

這兩種格式可分別使用Swift內建NSArray跟NSDictionary來Parse,那Array內又有Array該如何Parse呢?


做法:
假設有一json如下:
{
"Parents": [ {"id": "#123",
                       "name": "Daddy1",
                       "Children": [ {"c_id": "#124",
                                                "c_name": "kid1"},
                                           {"c_id": "#125",
                                                "c_name": "kid2"}]
                  },
                     {"id": "#234",
                       "name": "Daddy2",
                       "Children": [ {"c_id": "#235",
                                                "c_name": "kid1"},
                                           {"c_id": "#236",
                                                "c_name": "kid2"}]
                     },
                     {"id": "#345",
                       "name": "Daddy3",
                       "Children": [ {"c_id": "#346",
                                                "c_name": "kid1"},
                                           {"c_id": "#347",
                                                "c_name": "kid2"}]
                     }
                ]
}

var arrResult = json.objectForKey("Parents") as NSArray;
var row: NSDictionary;
var rowSub: NSDictionary;
var arrSubResult : NSArray;
var strID: String;
var strName: String;
var strC_ID: String;
var strC_Name: String;
var arrTotalResult = [DataModel]();


for var i=0 ; i < arrResult.count ; i++ {
 
    row = arrResult[i] as NSDictionary;
    arrSubResult = row.objectForKey("Children") as NSArray;

    strID = row["ID"] as String;
    strName = row["NAME"] as String;
 
    for var j=0 ; j < arrSubResult.count ; j++ {

        rowSub = arrSubResult[j] as NSDictionary;
        strC_ID = row["C_ID"] as String;
        strC_Name = row["C_NAME"] as String;

        //使用data model
        let data = DataModel(ID: self.strID, Name: self.strName, C_ID: self.strC_ID, C_Name: self.C_Name);
        arrTotalResult.append(data);
    }
}






2015年3月11日 星期三

[C#] Performance: 泛型 List V.S. ArrayList V.S. DataTable

說明:數值型別使用ArrayList(型別轉換為Object)會有裝箱與拆箱的問題,參考型別也需進行型別轉換,因此使用List<T>應有較佳的執行效率,而DataTable在擷取DB資料方便使用,但效能似乎較差? 使用StopWatch來簡單驗證(皆跑1000000次)。


Case1:
使用數值型別int做比較,List > ArrayList >> DataTable。








Case2:
使用參考型別string做比較,List ≒ ArrayList >> DataTable。



2015年2月2日 星期一

[git/gerrit] Mac OS使用git將文件push上gerrit的branch

說明:git是分散式版本控制系統之一,gerrit是以git專案為基的code review系統,記錄在Mac OS下如何將code push上gerrit的branch。


做法:
1. 安裝git(http://git-scm.com/)。

2. 啟用gerrit之帳戶與建立SSH key。可參閱以下網址:
http://git-scm.com/book/zh/v2/Git-on-the-Server-Generating-Your-SSH-Public-Key

3. 測試本地電腦跟gerrit連線ok後,開啟電腦上的終端機,輸入defaults write com.apple.finder AppleShowAllFiles TRUE並按下Enter,再輸入killall Finder然後送出,讓電腦顯示系統的隱藏檔案,方便後續操作.git裡的config檔。

4. 在終端機輸入git clone ssh://ssh_user_name@gerrit_url:29418/project_name.git。

5. 專案複製到本地電腦帳戶下資料夾後 -> 打開此專案,修改某行程式碼儲存後,可將該檔案commit -> 輸入commit message -> commit送出。

6. 回到終端機視窗,輸入cd ~/project_name/,進到該專案下 -> 輸入git status,檢視目前專案異動狀態。

7. 輸入scp -p -P 29418 ssh_user_name@gerrit_url:hooks/commit-msg .git/hooks/,可將每次commit所需的Change_Id自動加進去commit message,否則可能會出現ERROR: missing Change-Id in commit message的訊息。

8. 打開Finder,進到本機帳號下的Project,打開隱藏的.git資料夾 -> 打開裡面的.config檔,加入以下指令,增加gerrit用來review code的branch,
[remote "review"]
pushurl = ssh://ssh_user_name@gerrit_url:29418/project_name
push = refs/heads/master:refs/for/master

9. 最後在終端上輸入git push review,即可將code push上gerrit review
code的branch -> 進gerrit即可看到剛push上去的記錄。



參考資料:
https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/xcode_guide-continuous_integration/PublishYourCodetoaSourceRepository/PublishYourCodetoaSourceRepository.html
http://stackoverflow.com/questions/8845658/gerrit-error-when-change-id-in-commit-messages-are-missing
http://vimeo.com/23609339

2015年1月23日 星期五

[IOS/Swift] 在text field內左側放入圖片(put the pic inside the text field)

說明:想在text field內左側塞入一小圖,但又不想放在background占滿整個text field,該怎麼做?


做法:
在storyboard放入一text field,接著在viewDidLoad事件中,建構一UIImageView object,並給予初始位置與大小,在assign給text field的leftView的property。例:

let imageTest = UIImageView(image: UIImage(named: "xxx.png"));

imageTest.bounds = CGRect(x: 0, y:0, width: 20, height: 20);
txtTest.leftViewMode = UITextFieldViewMode.Always;
txtTest.leftView = ImageTest;


參考資料:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/index.html
http://stackoverflow.com/questions/13508836/add-image-to-uitextfield-in-xcode

[Oracle] 使用單行函式(function)記錄

說明1:想把查詢出來的多列資料合併成一列(combine multiple rows into one row),該如何做?


做法:使用LISTAGG函式,語法:LISTAGG(column[, 'delimiter']) WITHIN GROUP (ORDER BY column),例:
SELECT LISTAGG(ITEM, ';') WITHIN GROUP (ORDER BY No) DISPLAY
FROM table;


參考資料:
//http://stackoverflow.com/questions/1076011/how-can-multiple-rows-be-concatenated-into-one-in-oracle-without-creating-a-stor



說明2:去除查詢資料的特定字元TRIM/LTRIM/RTRIM


語法:L(R)TRIM( string, [ trim_string ] );
TRIM( [ [ LEADING | TRAILING | BOTH ] trim_character FROM ] string )
例:
--remove . on the right side of DISPLAY
SELECT RTRIM(DISPLAY, '.')DISPLAY
FROM table


參考資料:
http://www.techonthenet.com/oracle/functions/trim.php
http://give.pixnet.net/blog/post/25491792-(%E8%BD%89%E8%BC%89)-oracle%E7%9A%84%E5%AD%97%E4%B8%B2%E8%99%95%E7%90%86