2013年7月27日 星期六
2012年10月1日 星期一
Emacs for objective-c development
To develop GNUStep program under Debian, we need 2 handy tools:
1. Auto complete for obj-c.
2. Source browsing.
For the first one, emacs has auto complete package support for after Emacs24. If you are using Debian wheezy, you have to build from source. Before compile, the pre-requisite packages are:
$ sudo aptitude install build-essential libxpm-dev libgif-dev libtiff-dev libjpeg-dev libgtk2.0-dev libdbus-1-dev texinfo
Build with these pre-requisite packages can let your emacs customise the X fonts easily. And the build commands are :
$ ./configure --with-x-toolkit --with-xft
$ make
$ sudo make install
ps.: It seems that the default configuration does the same work.
The default install directory is /usr/local/bin.
Since Emacs24, in emacs package provides a convient way to install e-lisp plug-ins, add the following lines in your .emacs :
(require 'package)
(add-to-list 'package-archives
'("marmalade" .
"http://marmalade-repo.org/packages/"))
(package-initialize)
(add-to-list 'load-path "~/.emacs.d") ; This may not be appeared if you have already added.
Now open emacs, install the auto-complete package by :
M-x package-install [RET] auto-complete [RET]
After that, append the following lines in your .emacs
(require 'auto-complete-config)
(ac-config-default)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/elpa/auto-complete-1.4/dict")
(add-to-list 'ac-modes 'objc-mode) ;;here load the objc-mode
Note : Here the version is 1.4, modify the version if necessary.
OK, now we should have the emacs support auto-complete for obj-c.
For the second one: Source browsing, can be done with Gnu Global (gtags).
Since the Debian/Ubuntu currently provide Global-5.7.1, which does not support '--encode-path' option, the option will be used in the plug-in. We have to build Global-6.2.4 from source then "configure" "make install", and complete the setup by copying gtags.el to ~/.emacs.d and append :
(add-to-list 'load-path "~/.emacs.d") ; This may not be appeared if you have already added.
(autoload 'gtags-mode "gtags" "" t)
After that, the gtags command will be setup correctly. #
2012年9月29日 星期六
git-diff between branches
Before we diff, we have 4 branches :
tomteki-Mac-Pro:HearingLite tom$ git branch
TestUSB
* master
multiLanguage
testKbd
tomteki-Mac-Pro:HearingLite tom$
We want to diff the EditProfileViewController.m, to get the idea of where it is :
tomteki-Mac-Pro:HearingLite tom$ git diff --name-only master testKbd | grep EditProfileViewController
Classes/EditProfileViewController.h
Classes/EditProfileViewController.m
HearingLite/EditProfileViewController.h
HearingLite/EditProfileViewController.m
tomteki-Mac-Pro:HearingLite tom$
The above command will diff between two branches (testKbd and master).
Assume it is restructured from HearingLite/ path to Classes/ path (if doesn't , git ls-files can help ), the next thing to do is really diff them :
git diff master:Classes/EditProfileViewController.m testKbd:HearingLite/EditProfileViewController.m
In this note, we use the 'git diff --name-only' to get the path of our target file. And use 'git diff [branch] : [file path]' to diff the file in various branches.
2012年8月16日 星期四
Check members belonging to a group
dscacheutil -q group -a name admin
and we get the responses :
name: admin
password: *
gid: 80
users: root tom
More details about this command are in man pages.
2012年7月23日 星期一
App 如何指定可支援的機子?
這裡提供了一個方法,搭配 Apple 所訂的 Device Compatibility Matrix,就能用規格來卡位。
例如:『不適合在 3gs 執行』這個條件,就根據 Matrix 來找出 3gs 不支援的功能,像是 "front-facing-camera" or "gyroscope" 都可以,把它設定到你的 App Plist 的 "required device capabilities",將來就可以在你的 iTunes App 黃頁,巨大 Icon 的下方,看到 3gs 沒有被列入了!
參考文件: App related resources.
2012年6月1日 星期五
統計程式碼
find . \( -name *.h -or -name *.c \) -print
拿 linux source 來試試,沒問題。但是在我的 project 下,出現 error:
find: paths must precede expression: CADebugMacros.h修正後可以了:
find . \( -name '*.h' -or -name '*.c' \) -print最後加上 wc :
find . \( -name '*.h' -or -name '*.c' \) -exec cat "{}" ";" | wc -l 2012年5月31日 星期四
XCode 4.3 Extended Modules
原來這是以前放在 /Developer/Extras/CoreAudio/PublicUtility/** 下面的 header file. 隨著 XCode 4.3 推出,/Developer/** 目錄已經不復存在,一些 Cocoa FrameWork相關的模組內定為不安裝,必須請開發者自己搞定(Apple 內心獨白:呵呵,要付錢才給你用!)
解決這類問題,首先由 XCode 4.3 選單連到 Apple developer 網站:
當然,這時出現登入 iOS developer 的畫面,
登入後,看到 Extended modules:
下載後,可以自行指定要安裝在那兒,以我自己的環境為例, 整包 CoreAudio 目錄安裝於 /Users/tom/Documents/XCodeExtraLibrary/CoreAudio/* :
如上圖,在 XCode 的 Targets -> Build Settings 標籤內,輸入安裝的路徑,就可以解決問題!
Private Method in Objective-C
Now in 2013 things changed:
Apple 提供的 IDE : XCode 4.5 支援 objective-C 2.0 (大概是這個版本) catetgory,可以提供私有 iVars 與 methods 的宣告了!
2012年4月17日 星期二
iOS project 添加 Three20 支援 (XCode >= 4.0)
Three20 Screencast 指令節錄 網址: http://www.youtube.com/watch?v=-0-E-Z0fihg
Clone project:
git clone git://github.com/facebook/three20.git
Create your own project e.g. TestThree20, paralleled to the git downloads, then you have directories like this:
Documents
|
three20
|
TestThree20
Then, use one python scrip to automatically import three20 to your project:
python three20/src/scripts/ttmodule.py -p \
TestThree20/TestThree20.xcodeproj Three20 --xcode-version=4
Now, open your project in XCode, all things were done.
2012年4月1日 星期日
How to create project with Cocos2d and ARC support
Tiny tim
Often we want to write our APP under iOS5 with ARC support. But the developers of Cocos2d are still working for transfering the framework to the ARC.
How should we do ? Disable ARC for all Cocos2d files one by one ? Tiny tim has proposed a cleaner way --- managed by sub-project. Here it is :
1. Get cocos2d sources:
Go to https://github.com/cocos2d/cocos2d-iphone
git://github.com/cocos2d/cocos2d-iphone.git
2. Checkout the gles20 branch
git checkout gles20
3. Creating your own project with ARC enabled, by default, it is enabled by the XCode4 templates.
4. Drag and drop cocos2d-ios.xcodeproj to your project, and link cocos2d as a static library to your project.
5. Configure the user header search for YOUR PROJECT (not cocos2d-ios).
6. Configure the recursive searches.
7. Build the project. Note : If you exam "Build settings" of your project , the setting of "Objective-C Automatic Reference Counting" is "Yes". In cocos2d-ios sub-project, the same setting is "No".
2012年2月7日 星期二
Shadow Matrix 筆記
有些推導過程,中間的步驟很簡明,對於新手的我來說,還是得拿紙筆來演練,
才能順利接上,在此作個筆記。為了一致性,這裡的圖與符號標示,也是沿用該網頁。
目的:給定光源,在已知平面上 (以 normal vector \( \vec P \) 表示)找出平面以外某一點 \( \vec V \),
在平面上的投影點,然後導出其 Matrix Operation。
如圖,沿著射線,存在ㄧ k 值,使得 \( \vec V \) + k\( \vec L \) 為平面上的交點:
\[ (\vec V + k \vec L) \cdot \vec P = 0 \] \[ k = -\frac{\vec V \cdot \vec P}{\vec L \cdot \vec P} \] 所以投影點為: \[ \begin{align} \vec V + k \vec L &= \vec V - \frac{\vec V \cdot \vec P}{\vec L \cdot \vec P} \vec L \cr &= \frac{\vec V (\vec L \cdot \vec P) - (\vec V \cdot \vec P) \vec L}{\vec L \cdot \vec P} &(\ddagger) \end{align} \] 令 \[ \begin{align} \vec L &= \langle L_x,L_y,L_z,0 \rangle \cr \vec V &= \langle V_x,V_y,V_z,1 \rangle \cr \vec P &= \langle a,b,c,d \rangle \end{align} \] 對於 homogeneous coordinate \( \langle x,y,z,w \rangle \) 而言,w 是分母之意, 亦即對應的 3D coordinate 為 \( \langle x/w, y/w, z/w \rangle \), 也就是 \( \vec L \cdot \vec P \) 在 homogeneous coordinate 表示下恰為 \( w \) 之值。 展開 \((\ddagger)\) 式子的分子為: \[ \langle V_x,V_y,V_z,1 \rangle (aL_x + bL_y + cL_z) - (aV_x + bV_y + cV_z + d) \langle L_x,L_y,L_z,0 \rangle \] 其中 \(x\) 分量為: \[ V_x(bL_y + cL_z) - V_y(bL_x) - V_z(cL_x) - dL_x \] 同法可得 \(y,z\) 分量,整理得到 shadow matrix: \[ \begin{bmatrix} bL_y + cL_z & -bL_x & -cL_x & -dL_x \cr -aL_y & aL_x + cL_z & -cL_y & -dL_y \cr -aL_z & -bL_z & aL_x + bL_y & -dL_z \cr 0 & 0 & 0 & aL_x + bL_y + cL_z \end{bmatrix} \] 內心獨白: \(\LaTeX\) 打了會上癮!
MathJax Test
Another displayed equation is here:
\[
\forall x \exists y (x\le y \land y\le x \leftrightarrow x=y) .
\]
\[
\vec \Phi ( \vec r) = -\frac{G M_1}{| \vec{r}-\vec r_1 |} \]
To setup the MathJax capability, I added the following line to the HTML code, after the <head> command (as a single line, no line break):
<script src='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' type='text/javascript'/>
You can find more information from the MathJax website about this at http://www.mathjax.org/docs/1.1/start.html.
2011年12月20日 星期二
Gestures and View Transform
we first create it:
// Create a rotation gesture
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleRotateEvent:)];
[rotate setDelegate:self];
[redBox addGestureRecognizer:rotate];
Then write the handleRotateEvent: method:
// Called when the rotate gesture is recognized
- (void)handleRotateEvent:(UIRotationGestureRecognizer*)rotateGesture
{
// If our gesture ended, reset the box
if([rotateGesture state] == UIGestureRecognizerStateEnded) {
[self resetBox];
}
else {
// Store the value of our rotation gesture, then
// call our central update box tranform method
// Update box transform will also apply any transforms by the pinch gesture
rotation = rotateGesture.rotation;
[self updateBoxTransform];
}
}
In the updateBoxTransform, we directly use the rotation value to view’s transform:
- (void)updateBoxTransform
{
// Create a new transform based on the scale (scale determined by pinch gesture)
CGAffineTransform transform = CGAffineTransformMakeScale(scale, scale);
// Rotate the transform based on the rotation gesture value
transform = CGAffineTransformRotate(transform, rotation);
//-- contineously using the gesture's rotation
// Apply the transform to our red box
redBox.transform = transform;
}
Here we initialize a transform with Identity and scale, each time.The following paragraph uses another approach for the rotation:
- (void)handleRotateEvent:(UIRotationGestureRecognizer *)gestureRecognizer
{
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged) {
[gestureRecognizer view].transform =
CGAffineTransformRotate( [ [gestureRecognizer view] transform],
[gestureRecognize rotation] );
[gestureRecognizer setRotation:0]; //-- reset gesture's rotation
}
}
Since the source of transform is from the view object, we have to [gestureRecognizer setRotation:0]; such that we provide a fresh rotation quantum and add to the view’s transform
2011年11月25日 星期五
Discussions about using "Singleton" and "Archiving/Unarchiving"
My thoughts : Maybe it is unnecessary to implement them in the same class, as "wbyoung" said, two lines of code do the same work.
2011年8月8日 星期一
deleting trailing whitespace
When using git to maintain versions of XCode4 projects, often we need to "delete trailing whitespace" before we add or commit file to the git tree. While XCode4 leaves lots of line with trailing white spaces. How to remove them?
By Bash commands :
find -E . -type f -regex ".*\.(h|m|mm)" -print0 | xargs -0 sed -i -E "s/[[:space:]]*$//"
Note:
-E for find: Interpret pattern after -regex with Extended Regular Expression(ERE) syntax, should be placed before {directory}
".*\.(h|m|mm)" : match file name with .h, .m, .mm at the end, which is ERE syntax.
-E for sed: Interpret pattern using ERE syntax, the [:sapce:] represents the Whitespace character set: [ \t\r\n\v\f]
Thus, we can delete trailing white space with one command line in the project top directory ! Simple and clear !
2011年7月19日 星期二
The COSCUP 2011 iPhone program
緣由: 今年 COSCUP,由於智慧型手機的普遍化,增添了 App 競賽。所以就來共襄盛舉一番。
6 月 20 日:看到 COSCUP 的公告,開始熱血沸騰...可是,我不會 Java 、剛學會 Objective-C,只會寫 Hello World;如果要選比較可能做得到的平台,好像 iPhone 比較接近一些些。所以開始研讀 Cocoa 。
7 月 9 日,可以用 COSCUP API 抓到資料,用 json framework 來 parsing,感覺很幸福!
7 月 10, 11 ... 15 日,頭髮一直掉.... 媽!我錯了!但是為了自我實現的一個目標,繼續奮戰。感謝家人,同事的默默支持,也感謝 COSCUP 工作人員的協助。
這是現在完成的三個畫面。
以時間來看

以議程的類型來看

這兩種觀看的模式,可用右上角的按鈕群來互相切換。
另外,贊助廠商的導覽,可以由畫面下方的 Sponsors tab 來選擇。

因為這是一些資料的 parsing ,目的是使大家很快的找到需要的資訊,所以以極簡風格來完成使命。
第一次執行時,會從 COSCUP 網站下載資訊,之後就存在手機裡了,之後就從手機讀檔,需要 update 的話,左上角按鈕可以再從網站下載。
另外,有提供智慧型的語系選擇,例如,如果從 COSCUP 拿到了 Intel 廠商給的資訊,只有中文,那麼不論手機的語系設定如何,都會看到中文;那如果贊助廠商給的資訊有中、英文,那就會優先以手機的語系設定來顯示出贊助廠商的資訊。
程式碼放在 GitHub
git clone: git@github.com:tomjpsun/Coscup.git
就這樣啦!期待大家在8/20 見面啦!
2011年4月27日 星期三
2011年4月12日 星期二
greedy straegy abstract
再配合交大 [ 譚老師的講義 ],把 greedy algorithm 部份研讀了一下,
大概領悟力不夠,還是不能融會貫通,只好把重點節錄起來,慢慢消化:
以下 Greedy Algorithm 以 Greedy 簡稱之
Dynamic Algorithm 以 Dynamic 簡稱之
Optimal Solution 以 Opt. 簡稱之
Greedy 簡單說就是:經由一連串的選擇,找出全局最佳解,每次
選擇時,是找出當時的最佳解,這種 heuristic 的方法,就稱為 Greedy.
Greedy 異於 Dynamic 的解法,不像 Dynamic 找出所有 subproblems 的解,再慢慢
用 table 記下,累積到全局:而是先把問題分成一個 greedy choice + subproblem ,
這個 greedy choice 是一個局部最佳解,並且用 recursive 方式繼續 下去解 subproblem。
最後的結果,使得 Greedy 不一定得到 Opt.,但是很快,而且結果可以接近 Opt.
很多時候,由於這種早期決定的性質,Greedy 無法找到 Opt.
當然,如果能證明,這個 greedy choice 是全局 Opt. 的一環,那麼就可以確保,一系列
的 greedy choice 可以找到全局 Opt.
下面是從 [Wiki] 節錄的,Greedy 的兩個重要成份:
Greedy choice property
We can make whatever choice seems best at the moment and then solve
the subproblems that arise later. The choice made by a greedy algorithm
may depend on choices made so far but not on future choices or all the
solutions to the subproblem. It iteratively makes one greedy choice
after another, reducing each given problem into a smaller one.
In other words, a greedy algorithm never reconsiders its choices.
This is the main difference from dynamic programming, which is exhaustive
and is guaranteed to find the solution.
After every stage, dynamic programming makes decisions based on all
the decisions made in the previous stage, and may reconsider the
previous stage's algorithmic path to solution.
Optimal substructure
"A problem exhibits optimal substructure if an optimal solution
to the problem contains optimal solutions to the sub-problems."
以下列出一些例子來討論:
例一 0-1 knapsack problem : 假設有 n 種礦石,以 1,2...n 代表這 n 種礦石,
第 i 種重量是 w[i] ,價值是 v[i],限定載重上限為 W 的情形下,
怎樣拿會最有價值?
例二 fractional knapsack problem : 假設有 n 種礦石粉末,以 1,2...n 代表這 n 種礦石,
第 i 種重量是 w[i] ,價值是 v[i],限定載重上限為 W 的情形下,
怎樣拿會最有價值?
這兩種問題,都有 Optimal substructure 的特性 ----- 假設最有價值的裝法 載重是 W,
則裝入礦石 j 後,對於剩下的 n-1 種礦石 ( 就是 j 已拿光後 ) 最有價值的載重
剩下 W-W[j]
fractional knapsack problem 可以用 Greedy 找到 Opt. :
因為當每次選擇 價值/重量 比最高的礦石粉 j 時,可以確定把 j 拿完,
是最好地選擇。若 j 拿完還有多餘的載重量,就選擇 價值/重量 比
次高的礦石粉,此時 所作的 greedy selection 就是符合 global Opt. 的選擇。
0-1 knapsack problem 無法用 Greedy 找到 Opt. :
反例 :考慮三種礦石,都只有一個:
v[1]=60 , w[1]=10
v[2]=100, w[2]=20
v[3]=120, w[3]=30
upper weight W= 50
1 的 價值/重量 比最高,選取 1 剩下可載重 40,但是選 1 不會得到 Opt.
Opt. 是選 2+3, total value =220
但是 0-1 knapsack problem 可以用 Dynamic 找到 Opt.
另外,有漂亮的數學證明, 符合 matroid 結構的問題,可以用 greedy 得到 Opt.
( matroid 是比較深的主題,有空再來研究吧!)
#
2011年3月28日 星期一
simple & clean daemon code
Here the little trick is :
Use option '-D' as a 'differentiate mark' of daemon process
from the parent process.
The second time it self exec(), means the daemon process
starting with -D option, which make it goes to the 'else' block :
It's a Clean and Simple example!
#







