Monday, July 1, 2013

Delegate in iOS

My question is "what is delegation?" after writing several iOS programs.

"Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object - the delegate - and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you easily customize the behavior of several objects in one central object."

Basically, "delegation" is way that one object communicate with the other object.

這網頁中的Example 1是一個很好的範例,講述有兩個object,為brain和beer bottle,brain可以不斷下達指令給beer bottle要喝,但是brain因為要看電視所以太忙沒有辦法知道什麼時候beer被喝完,於是乎,beer bottle此時就要用delegation,當沒有beer時告知brain。

而這網頁講述了delegation中物件的關係,若此時物件A call 物件B:

  • A是B的delegate object
  • B會有A的reference
  • A會implement B的delegate method
  • B可以與A communicate,用delegate methods

Reference:



Sunday, June 30, 2013

Learning VIM (if using Windows)

vim在linux/unix底下因可以快速在shell和vim中切換,會有比較好的效益,然而,vim編輯器本身的能力即使在Windows底下仍是一樣的。
在Windows底下跑vim有幾種方法:
  1. 安裝gVim http://www.vim.org/download.php#pc
  2. 透過ssh等方式連到一台linux/unix的機器
  3. 使用Cygwin創立linux/unix模擬環境
純文字檔的編輯用方法1即可,但若是希望可以整合系統,同時也compile程式、執行、外部操作等,用方法2,3較合適。

//======================================

vim主要分成兩種模式:命令模式、編輯模式。我們一般notepad功能即是vim的編輯模式,鍵盤輸入什麼就出現什麼,而移動指標等操作需要透過方向鍵或滑鼠等。

vim可貴的地方在於命令模式,處於命令模式時,鍵盤輸入的東西並不會直接出現在螢幕,而是執行了所對應的指令,例如,j按鍵是下,k是上等。

vim越是熟悉之後,處於命令模式的比例越高,少數情況才進入輸入模式,如此狀況下編輯文件速度快。

//======================================

vim指令設計上其實是非常符合手指位置的(人體工學?)。一般而言,看到一份文件第一個用的指令是向下卷,即是指令 j ,可以使游標往下,而 j 恰好是右手食指位置,不需要移動手指就可以按到,向上的指令 k 則是位居第二名。

而有些則是從名稱的縮寫而來,例如 i 是insertion的縮寫 d 是delete等。

//======================================

vim非常適合處理反覆的情況,例如,往上的指令是 k ,若希望往上八行,打 8k 即可。
更深入的部分有巨集,使用指令 q ,可以記憶一連串的指令,然後再重複!(寫verilog十分需要)

//======================================

再者就是客制化,透過撰寫.vimrc這份設定文件,可以依照自己的需求設定vim,例如設定「編譯並執行」的快捷鍵、設定tab大小、設定程式syntax變色規則等。當移動到新的系統上,只需要將這份文件放到自己的home目錄,vim馬上又是自己最熟悉的樣子了。

//======================================

vim學習曲線比較陡,一開始可能會發現打文件反而變慢,必須經過一段時間才會達到效果。於是我畫了一張示意圖:

//======================================

Real Tutorial這裡:
  1. 基本指令(其實大概80%在用的都出現在此網頁):http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/
  2. Vim Adventure,透過玩遊戲的方式學習vim: http://vim-adventures.com/
  3. Vim Cheat Sheet,圖片方式的指令集: http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html
  4. Complete Tutorial: ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf
// end here

Drupal Installation

Follow the instruction in Drupal tutorial:
https://drupal.org/documentation/install/settings-file

However, for the permission setting part, it's not working if I follow the instruction.
During the installation, Drupal may have to do some modifications on the files in sites/default/, they may be modified. So, 'w' is required.

The easiest way to solve the permission issue here is to grant all the permissions and modify them back as soon as the installation is done.

  • chmod 777 settings.php && chmod 777 ../default/
  • process the installation
  • chmod 644 settings.php ^&& chmod 755 ../default/
Done.

Sunday, June 23, 2013

std::vector

C++中std的vector十分方便,可以解決多數動態大小的問題,是索引值的方式和array更是相同,而新的值用pushback加入。

#include <cstdio>
#include <vector>

using namespace std;

int main(){

 vector<int> myVector;

 // setter: push new
 myVector.push_back(2); // index = 0
 myVector.push_back(5); // index = 1
 myVector.push_back(1); // index = 2

 // getter: by index
 printf("%d\n", myVector[1]); // 5

 // get size
 int size = myVector.size();
 printf("size = %d\n", size);

 return 0;
}

std::map

std::map是C++中的函式,include <map>之後可以使用。

很多時候我們需要用「一種資料」去查另外一種「對應的資料」,這時候就是map上場的時候了,有些程式語言稱為dictionary,顧名思義就是查字典的意思。使用一個"key"去找對應的"value"。

注意key可以是任何的資料形態,value也是,以下是一個簡單的範例程式,包含:

  1. setter:設定新的項目
  2. getter:取得已存的項目,若不存在會回傳0,並被加入map中
  3. is not found:檢查該key有無存在在map中
  4. wall through:走過所有map中的項目

#include <cstdio>
#include <map>
#include <iostream>

using namespace std;

int main(){

 map<char, int> myMap;
 map<char, int>::const_iterator iter;

 // setter
 myMap['h']=2;
 myMap['e']=3;

 // getter
 printf("%d\n", myMap['h']); //2
 printf("%d\n", myMap['e']); //3
 printf("%d\n", myMap['r']); //0, not exist

 // check if exsit
 int isNotFound;
 isNotFound = ( myMap.find('z') == myMap.end() );
 printf("%c\n", (isNotFound)?'y':'n'); // y

 isNotFound = ( myMap.find('e') == myMap.end() );
 printf("%c\n", (isNotFound)?'y':'n'); // n

 // walk through all elements in map
 for(iter = myMap.begin() ; iter != myMap.end() ; iter++)
  printf("- %c:%d\n", iter->first, iter->second);

 return 0;
}

Thursday, May 16, 2013

Matchismo on iOS



This is the first assignment for the course CS193P at Stanford 2013 winter session. It shows how the "MVC" structure works, and also includes really good coding style in objective-oriented programming.

http://www.stanford.edu/class/cs193p/cgi-bin/drupal/

Some interesting stuffs I would like to share is as below:

  1. Connecting the "View" related events or properties by dragging the object from GUI programming interface to text code. Cool.
  2. NIL pointer design, I've written things about this in last post.
  3. General initialisation design. Objects in objective-C mostly have the some usage in initialisations, setters/getters, function callings, etc. It's easy for the developers to learn some basic examples and apply on different occasions.
  4. Auto-complete editing design in Xcode. Although this feature, auto-complete while typing the code, is not new stuff, it's still the best auto-complete mechanism I've ever seen. It's fast, and smart. It allows you to jump to next blank in a very short time. While writing the program, you spent very little time on typing. Around 80%~90% of the code can be done by the auto-complete function, only the new names for object/function/etc are needed to be typed completely. At last, Xcode also generates those an annoying special characters in the code, like "", [], {}, etc.
Finally, the developing environment for iOS is not only including great designs but also so much fun.



Saturday, May 11, 2013

NIL Pointer in Objective C

In many programming languages, we do the checking if the pointer is null (equally, 0) or not before we start to grab the content. However, in Objective C, there's no need to do this. The design makes you no need to do so.

When a pointer has no object points to, it's automatically storing the value 0, which we call a NIL pointer. And, we can complete our other designs without checking the pointer is NIL or not because that function calls just do nothing on NIL pointers, and return 0.

Take one example I learned in the class,
if ([card.contents isEqualToString:self.contents] ){ score = 1; }
No need to card.contents is NIL or not. If it's NIL, isEqualToString just simply skip it and return 0, which may cause the if-statement failed, which is also the right thing we want.