September 2008 Archives

Book Four Coding

Book Five Maintenance
第五篇 維護

Thus speak the master programmer:
"Though a program be three lines long, someday it have to be maintained. "
編程大師如是說:
"即使一個程序只有三行長, 總有一天它也不得不需要維護. "

5.1
A well-used door needs no oil on its hinges.
A swift-flowing stream does not grow stagnant.
Neither sound nor thoughts can travel through a vacuum.
Software rots if not used.
These are great mysteries.
一扇經常開啟的門的鉸鏈不需要潤滑油.
一條湍急的河流不會變得污濁.
無論是聲音還是想法都不可能在真空中傳播.
軟件如果不用就會腐朽.
這世界真奇妙.

5.2
A manager asked a programmer how long it would take him to finish the program on which he was working.
"I will be finished tomorrow, " the programmer promptly replied.
"I think that you are being unrealistic, " said the manager, "Truthfully, how long will it take? "
The programmer thought for a moment. "I have some features that I wish to add. This will take at least two weeks, " he finally said.
"Even that is too much to expect, " insisted the manager,
"I will be satisfied if you simply tell me when the program is complete."
The programmer agree to this.
Several years later, the manager retired. On the way to his retirement lunch, he discovered the programmer asleep at his terminal. He had been programming all night.
一個程序員正在編寫他的程序, 經理問他還需要多長時間完成.
"明天就可以完成. " 那個程序員立刻回答.
"我想你這是不切實際的, " 經理說, "實話實說, 這需要多長時間? "
程序員想樂一會, "我還想加進一些新的特色, 這至少需要花兩個星期的時間. " 他最後說.
"即使那樣也期望過高, " 經理堅持說, "只要你編寫完程序時告訴我一聲, 我也就滿足樂. "
程序員答應樂.
幾年以後, 那位經理要退休樂. 在他去退休午餐會的路上, 發現那位程序員趴在他的終端機前睡著樂. 他整個晚上都在忙於編寫那個程序.

5.3
A novice programmer was once assigned to code  a simple financial package.
The novice worked furiously for many days, but when his master reviewed the program, he discovered that it contained a screen editor, a set of generalized graphics routines, an artificial intelligence interface, but not the slightest mention of anything financial.
When the master asked about this, the novice became indignant. "Don't be so impatient, " he said, "I'll put in the financial stuff eventually."
一次一位初學編程者被指派編寫一個簡單的財務軟件包. 這位初學者大張旗鼓地工作樂許多天. 當他的師傅檢查這套程序時, 發現其中包含有一個屏幕編輯器, 一套通用圖表程序, 一個人工智能界面, 然而卻沒有任何財務方面的東西.
 當師傅問及此事時, 初學者顯得憤憤不平. "不要這樣沒有耐心嘛, " 他說, "我最後加些財務的素材進去就是樂. "

5.4
Does a good farmer neglect a crop he has planted?
Does a good teacher overlook even the most humble student?
Does a good father allow a single child to starve?
Does a good programmer refuse to maintain his code?
難道一位好農民會漏掉他所種的一株莊稼嗎?
難道一位好老師會忽視哪怕是最差的學生嗎?
難道一位好父親會允許他唯一的孩子挨餓嗎?
難道一位好程序員會不願意維護他的程序嗎?

Safari鎖定單窗口模式

| No Comments | No TrackBacks
原文链接:http://www.macosxhints.com/article.php?story=2008032109273043

(粘贴到Terminal中执行):

defaults write com.apple.Safari TargetedClicksCreateTabs -bool true

要撤消該设置:

defaults delete com.apple.Safari TargetedClicksCreateTabs

注意运行这两个命令前一定要关闭Safari
第一計 瞞天過海
第二計 圍魏救趙

原文
共敵不如分敵, 敵陽不如敵陰.

譯文
樹敵不要太多, 對待敵人要本著各個擊破的原則, 
對於現在還無法消滅的敵人, 要隱藏自己的意圖.

釋析
和敵人作戰, 就像治理洪水一樣: 敵人勢頭強大, 就要躲過沖擊, 用疏導的方法分流;
對待弱小的敵人, 則要抓住時機消滅它. 對敵人要避實就虛, 
攻其要害, 使敵方受到挫折, 受到牽制. 圍困自然能夠解除.

戰例
公元前355年, 魏恵王想要奪回曾經丟失的中山, 於是派大將龐涓前去
攻打. 
中山這個地方原本是戰國時期魏國的一個鄰國, 後來被魏國收服. 可是趙國
趁魏國國喪伺機將中山占為己有. 魏國的大將龐涓認為中山只是一個小地方,
距離趙國又很近, 不如直接攻打趙國的都城邯鄲, 這樣既可以收回中山,
又可以報當初趙國奪城之仇. 魏國的君王采納樂龐涓的提議, 就命龐涓率
領大軍直奔趙國, 包圍樂趙國的都城邯鄲. 趙王危難之中治好向齊國求救.
齊威王答應樂, 並命令田忌為將軍, 孫臏為軍事, 領兵出發.當田忌和孫臏
率兵進入魏趙交界地時, 田忌想直接去趙國的都城邯鄲, 孫臏卻制止說: "現在
魏國的精兵都被派出去攻打趙國樂, 如果我們攻打魏國, 那麼龐涓一定會回來解救, 
這樣, 邯鄲之圍就會解除. 我們在龐涓回來的途中伏擊他, 那麼他的軍隊一定會大敗. "
田忌覺得很有道理, 就按照孫臏的計策部署. 當龐涓聽到魏國被攻打的消息後, 
立即班師回朝, 結果在途中中樂埋伏, 魏軍大敗. 這就是歷史上有名的"圍魏救趙"的故事.

第三計 借刀殺人
第一, 按照圖上選擇, 然後編譯, 必定遇到如下錯誤
CodeSign error: no certificate found in keychain for code signing identity 'iPhone Developer'Picture 2.png
此時, 打開工程文件夾下*.xcodeproj為後綴名的文件(右鍵點擊, 選擇"Show Package Contents")
一般會看到三個文件, 開打這個文件project.pbxproj(以文本方式), 查找iPhone Developer,找到後刪除(只有兩處), 保存! 重新進入Xcode編譯, ok!
注意: 修改過程中關閉Xcode!!

第二, 打包好的程序*.app文件傳到iPhone真機上用老辦法killall SpringBoard會失效...此時:
需要删除 /private/var/mobile/Library/Caches/com.apple.mobile.installation.plist 文件,重启机器来刷新程序列表, ok!

Makefile

| No Comments | No TrackBacks
INFOPLIST_FILE=Info.plist
LANG_FILES=$(wildcard *.lproj)
SOURCES=$(wildcard *.m)
RESPNG=$(wildcard *.png)
SND=$(wildcard *.mp3)
RESOURCES=Resources

CC=/usr/local/bin/arm-apple-darwin-gcc
CFLAGS=-g -O2 -Wall -fsigned-char
LD=$(CC)
LDFLAGS=-I/usr/local/arm-apple-darwin/include -lobjc -ObjC -framework CoreFoundation -framework Foundation -framework CoreGraphics -framework GraphicsServices -framework UIKit -framework LayerKit -framework PhotoLibrary  -framework MusicLibrary -framework CFNetwork

WRAPPER_NAME=$(PRODUCT_NAME).app
EXECUTABLE_NAME=$(PRODUCT_NAME)
SOURCES_ABS=$(addprefix $(SRCROOT)/,$(SOURCES))
RESPNG_ABS=$(addprefix $(SRCROOT)/,$(RESPNG))
LANG_FILES_ABS=$(addprefix $(SRCROOT)/,$(LANG_FILES))
SND_ABS=$(addprefix $(SRCROOT)/,$(SND))
INFOPLIST_ABS=$(addprefix $(SRCROOT)/,$(INFOPLIST_FILE))
OBJECTS=\
    $(patsubst %.c,%.o,$(filter %.c,$(SOURCES))) \
    $(patsubst %.cc,%.o,$(filter %.cc,$(SOURCES))) \
    $(patsubst %.cpp,%.o,$(filter %.cpp,$(SOURCES))) \
    $(patsubst %.m,%.o,$(filter %.m,$(SOURCES))) \
    $(patsubst %.mm,%.o,$(filter %.mm,$(SOURCES)))
OBJECTS_ABS=$(addprefix $(CONFIGURATION_TEMP_DIR)/,$(OBJECTS))
APP_ABS=$(BUILT_PRODUCTS_DIR)/$(WRAPPER_NAME)
PRODUCT_ABS=$(APP_ABS)/$(EXECUTABLE_NAME)

all: $(PRODUCT_ABS)

$(PRODUCT_ABS): $(APP_ABS) $(OBJECTS_ABS)
    $(LD) $(LDFLAGS) -o $(PRODUCT_ABS) $(OBJECTS_ABS)
    if ping -t 1 -c 1 $(IPHONE_IP) ; then ssh root@$(IPHONE_IP) '[ -d /Applications/$(PRODUCT_NAME).app ] && echo Application exists on iPhone || touch ~/.xcode_kill_sb'; scp -r $(APP_ABS) root@$(IPHONE_IP):/Applications; ssh root@$(IPHONE_IP) 'if [ -e ~/.xcode_kill_sb ]; then rm ~/.xcode_kill_sb; killall SpringBoard; else echo skip killing SpringBoard; fi'; else echo iPhone not available; fi    

$(APP_ABS): $(INFOPLIST_ABS)
    mkdir -p $(APP_ABS)
    mkdir -p $(APP_ABS)/$(RESOURCES)
    cp $(INFOPLIST_ABS) $(APP_ABS)/$(RESOURCES)
    cp $(RESPNG_ABS) $(APP_ABS)/
    cp $(SND) $(APP_ABS)
    cp -R $(LANG_FILES_ABS) $(APP_ABS)/$(RESOURCES)
    
$(CONFIGURATION_TEMP_DIR)/%.o: $(SRCROOT)/%.m
    mkdir -p $(dir $@)
    $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@

clean:
    rm -f $(OBJECTS_ABS)
    rm -rf $(APP_ABS)

Triggering Vibration

| No Comments | No TrackBacks
Applications running on iPhone--but not the iPod touch--can trigger vibration using System Audio 
Services. The vibrate option is identified using the kSystemSoundID_Vibrate identifier. To trigger 
it, use the AudioServicesPlaySystemSound function in the Audio Toolbox framework, as shown 
here. 

#import <AudioToolbox/AudioToolbox.h> 
#import <UIKit/UIKit.h> 
- (void)vibratePhone 

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 

10.5.5 Update OK!

| No Comments | No TrackBacks
10.5.5UpdateOK.png
呵呵, 隨著10.5.5升級的OK, 身上的蕁麻疹也不疼不癢樂~~ 打起精神, coding!

原文
備周而意怠, 常見則不疑, 陰在陽之內, 不在陽之對. 太陽, 太陰.

譯文
人們准備的越周到就越容易松懈; 對於平時習慣的事務, 往往不再懷疑. 因此秘技常隱藏在暴露的事務中, 而不是以公開的形式存在. 非常公開的事務中往往蘊藏著機密的東西.

釋析
"瞞天過海" 與 "欺上瞞下", "掩耳盜鈴" 等不同. 雖然二者在某種程度上都含有欺騙性, 但其動機, 性質, 目的是不同的, 因此不能混為一談. 這一計在兵法上的運用, 常常是人們在觀察處理世事中, 由於對某些事情的習見不疑而不自覺地產生樂疏漏和松懈, 因此兵家若能乘虛而入, 掩蓋某種軍事行動, 一定會出奇制勝.

戰例
南北朝後期, 北周的貴族楊堅掌握樂北周的軍政大權. 581年, 他建立樂隋朝. 隋朝建立後, 在江南唯一能與之抗衡的只有陳國. 於是楊堅派大將賀若弼前去征討陳國.
隨軍浩浩蕩蕩, 一舉攻下樂幾個重鎮, 這時, 大軍准備攻取京口. 為樂出奇制勝, 大將軍賀若弼竭力掩蓋自己的渡江意圖, 他先組織軍隊在沿江一帶駐防, 做出安營扎寨的假象, 借此麻痺陳國的軍隊. 陳國將士以為隨軍馬上就要渡江, 於是急忙調兵遣將, 准備應戰. 可是他們發現隨軍只是演習, 並沒有真正渡江. 於是, 就不再積極防守, 開始麻痺大意樂.
為樂更徹底地麻痺陳軍, 賀若弼讓部下將准備渡江的船只隱藏起來, 在江邊只留下一些小船. 陳軍一看到這些, 就更加放松樂戒備. 賀若弼經過一個月的周密觀察後, 認為渡江時機已經成熟, 下令全軍將士渡江, 發起攻擊. 陳國軍隊沒想到隨軍會突然襲擊, 猝不及防, 被打得落花流水. 隨後, 隨軍有乘勝追擊, 攻占樂陳國的國都建康, 消滅樂陳國.

首先是官方SDK的實現, 很簡單:
只需在Info.plist文件離添加如下一句話就ok
UIStatusBarHidden
fullscreen.png

以下是非官方的實現:
只要在 applicationDidFinishLaunching 的開始部份,
加上下面的 setStatusBarMode 代碼即可:
Quote:

- (void) applicationDidFinishLaunching: (id) unused
{
    [UIHardware _setStatusBarHeight:0.0f];
    [self setStatusBarMode:2 orientation:0 duration:0.0f fenceID:0];
大家在新建文件的时候默认在文件的开关都会生成这样的注释:
/*

*
*  Created by XX on yy-m-d.
*  Copyright 2008 __MyCompany__. All rights reserved.
*
*/
每次要拖动改__MyCompany__很麻烦,可以直接在終端(Terminal)下输入:
    defaults write com.apple.xcode PBXCustomTemplateMacroDefinitions '{ ORGANIZATIONNAME = "aiFengKnight"; }'
重启Xcode,这样新建文件的_MyCompany__就自动变成了aiFengKnight了

:)

Book Four Coding
第四篇 編碼

Thus speak the master programmer:
"A well-written program is its own heaven; a poorly-written program its own hell."
編程大師如是說:
"一個寫的完美的程序是其自身的天堂, 而一個寫的糟糕的程序則是其自身的地獄."

4.1
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be neither too litter nor too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity.
A program should follow the 'Law of Least Astonishment'. What is this law? It is simply that the program should always respond to the user in the way that astonishes him least. 
A program, no matter how complex, should act as a single unit. The program should be directed by the logic within rather than by outward appearances.
If the program fails in these requirements, it will be in a state of disorder and confusion. The only way to correct this is to rewrite the program.
一個程序應當是輕盈的, 靈活的, 它的子程序就像一串珍珠一樣連接著. 它的精神和意圖應該貫穿始終. 在程序中, 內容既不應太多, 也不應太少; 既不該有不需要的循環結構, 也不該有冗余的變量; 既不缺乏結構性, 又不過分僵化.
一個程序應當遵守"最少驚訝原則". 這條原則是什麼呢? 簡單地說, 就是指一個程序對用戶的指令所做出的反應, 應該總是使用戶最小程度地驚訝.
一個程序無論多麼復雜, 都應該以一個整體的方式運行. 程序應以其內在的邏輯為指引, 而非外在形態.
如果一個程序不能達到這些要求, 它將處於一種雜亂無章和混淆不清的狀態. 唯一的更正方法就是重寫這個程序.

4.2
A novice asked the master: "I have a program that sometimes runs and sometimes aborts. I have followed the rules of programming, yet I am totally baffled. What is the reason for this?"
The master replied: "You are confused because you do not understand Tao. Only a fool expects rational behavior from his fellow humans. Why do you expect it from a machine that humans have constructed? Computers simulate determinism; only Tao is perfect.
The rules of programming are transitory; only Tao is eternal. Therefore you must contemplate Tao before you receive enlightenment."
"But how will I know when I have received enlightenment?" asked the novice.
"Your program will then run correctly." replied the master.
一位初學者問大師: "我有一個程序, 時靈時不靈. 我一直都遵循著編程的規則, 結果卻整個兒搞糊塗樂. 這是什麼原因呢?"
大師回答說: "因為你沒領悟道, 所以你迷惑不解. 只有傻瓜才會指望從人類身上看到理性的行為. 你又能指望一台人類制造的機器怎麼樣呢? 計算機模仿的是決定論, 只有道才是盡善盡美.
編程用的那些條條框框式的規則僅僅是曇花一現, 只有道才是永恆的. 因此, 在你受到道的啟發之前, 你必須沉思於道."
"但我怎樣才能知道我已經受到啟發樂呢?" 初學者問.
"當你的程序運行無誤時." 大師回答說.

4.3
A master was explaining the nature of Tao to one of his novices. "The Tao is embodied in all software ---- regardless of how insignificant. " said the master.
"Is the Tao in a hand-held calculator? " asked the novice.
"It is, "came the reply.
"Is the Tao in a video game?" continued the novice.
"It is even in a video game," said the master.
"And is the Tao in the DOS for a personal computer?"
The master coughed and shifted his position slightly. "The lesson is over for today," he said.
一位大師正在向他的一名初學編程的弟子解釋道的真諦. "此道體現在所有的軟件當中 ---- 不管它看上去多麼無足輕重. " 大師說著.
"此道體現在手掌計算器中嗎? " 初學者問.
"是的."
"此道體現在電子游戲中嗎? " 初學者繼續問.
"此道也體現在電子游戲之中. " 大師說. 
"那麼此道體現在個人電腦的DOS系統中嗎? " 
大師輕咳一聲, 並稍稍挪動樂一下位置. "今天的課就到這裡吧. " 他說.

4.4
Prince Wang's programmer was coding software. His fingers danced upon the keyboard. The program compiled without an error message, and the program ran like a gentle wind.
"Excellent! " the Prince exclaimed, "Your technique is faultless!"
"Technique? " said the programmer turning from his terminal, "What  I follow is Tao ---- beyond all techniques! When I first began to program I would see before me the whole problem all in one mass. After three years I no longer saw this mass. Instead, I used subroutines. But now, I see nothing. My whole being exists in a formless void. My senses are idle.
My spirit, free to work without plan, follows its own instinct. In short, my program writes itself. True, sometimes there are difficult problems. I see them coming, I slow down, I watch silently. Then I change a single line of code and the difficulties vanish like puffs of idle smoke. I then compile the program. I sit still and let the joy of the work fill my being. I close my eyes for a moment and then log off. " Prince Wang said, "Would that all my  programmers were as wise!"
一位項目經理手下的一名程序員正在編寫軟件. 他的手指在鍵盤上飛舞著, 在程序的編譯過程中沒有出現任何錯誤的信息. 程序運行起來就像一陣和風.
"太好樂!" 經理高興地大叫樂起來, "你的技藝簡直完美無缺. "
"技藝? " 程序員說著便從他的終端機前轉過身來, "我遵循的是道 ---- 所有的技藝遠不能及! 當我剛開始編程時, 我眼前看見的是整個問題亂成一團. 三年之後, 我再也看不見這種一團糟的情形樂. 相反, 我用樂各種各樣的子程序. 但現在, 我什麼也看不見樂. 我的整個身心存在於一種無形的虛空離, 我的知覺是空蕩蕩的. 
我的精神隨其本能而動, 不需要計劃就能自由的工作. 總而言之, 是我的程序自己寫出樂自己. 誠然, 有時會有一些難題. 我看見那些難題向我走來, 於是我放慢樂速度, 默默地注視著它們. 然後我更改樂一行代碼, 那些問題就煙消雲散樂. 然後我完成程序的編譯. 我靜靜地坐著, 讓工作的歡愉遍布全身. 我閉上雙眼, 歇息片刻, 然後退出系統. "
經理說: '我希望所有的程序員都這麼聰明! "

Book Two The Ancient Masters

Book Three Design
第三篇 設計

Thus speak the master programmer:
"When the program is being tested, it is too late to make design changes."
編程大師如是說:
"當程序被測試時, 再修改設計方案就太遲樂."

3.1
There was once a man who went to a computer trade show. Each day as he entered, the man told the guard at the door: 
"I am a great thief, renowned for my feats of shoplifting. Be forewarned, for this trade show shall not escape unplundered."
This speech disturbed the guard greatly, because there were millions of dollars of computer equipment inside, so he watched the man carefully. But the man merely wandered from booth to booth, humming quietly to himself.
When the man left, the guard took him aside and searched his clothes, but nothing was to be found.
On the next day of the trade show, the man returned and chided the guard, saying: "I escaped with a vast booty yesterday, but today will be ever better." So the guard watched him more closely, but to no avail.
On the final day of the trade show, the guard could restrain his curiosity no longer. "sir Thief, " he said, "I am so perplexed, I cannot live in space. Please enlighten me. What is it that you are stealing?"
The man smiled. "I am stealing ideas." he said.
曾經有一個人去參加一次計算機交易展會, 每天當他進入展館時, 都對門衛說: "我是個大盜, 我偷盜的本領是出樂名的. 事先警告你, 這次展示會也在劫難逃."
這番話讓門衛坐立不安, 因為裡邊有價值數百萬元的電腦設備, 於是他緊緊盯住這個人, 但這個人只是從一個展攤逛到另一個展攤, 嘴裡輕輕地哼著小曲.
當這個人出門時, 門衛把他拉到一邊, 搜查他的衣服, 但一無所獲.
第二天, 這個人又來樂, 並對著門衛囂張地嚷著: "我昨天滿載而歸, 但今天的收獲會更大." 於是門衛盯他盯得更緊樂, 但仍一無所獲.
在展示會的最後一天, 門衛再也控制不住自己的好奇心樂, "大盜先生, "門衛說, "我被你搞糊塗樂, 實在想不明白. 請告訴我, 你究竟在偷什麼?"
這個人笑樂. "我在偷想法."他說.

3.2
There was once a master programmer who wrote unstructured programs. A novice programmer, seeking to imitate him, also began to write unstructured programs. When the novice asked the master to evaluate his progress, the master criticized him for writing unstructured programs, saying: "What is appropriate for the master is not appropriate for the novice. You must understand the Tao before transcending structure."
曾經有一位編程大師, 喜歡編寫非結構化的程序. 一位初學者試圖模仿他, 也開始編寫非結構化的程序. 當這位初學者請大師評價他的進展時, 大師批評樂他的做法. 他說: "對一位編程高手合適的, 對初學者來說並不一定合適. 在超越結構化之前, 你必須先領悟道."

3.3
There was once a programmer who was attached to the court of the warlord of Wu. The warlord asked the programmer: "Which is easier to design: an accounting package or an operating system?"
"An operating system," replied the programmer.
The warlord uttered an exclamation of disbelief.
"Surely an accounting package is trivial next to the complexity of an operating system," he said.
"Not so," said the programmer, "when designing an  accounting package, the programmer operates as a mediator between people having different ideas: how it must operate, how its reports must appear, and how it must conform to tax laws.
By contrast, an operating system is only limited by outward appearances. When designing an operating system, the programmer seeks the simplest harmony between machine and ideas. This is why an operating system is easier to design."
The warlord of Wu nodded and smiled. "That is all good and well,"he said, "but which is easier to debug?"
The programmer made no reply.
曾經有一位程序員被派到Wu的軍機大臣手下工作. 軍機大臣問程序員:  "設計一個財務軟件包, 和設計一個操作系統, 哪一個更容易?"
"操作系統."程序員回答說.
軍機大臣立刻發出一種不信任的驚歎, "與一個復雜的操作系統相比, 一個財務軟件包簡直是小巫見大巫." 他說.
"並非如此,"程序員說, "在設計一個財務軟件包時, 編程人員是作為一個中介者在觀念各異的人們之間起作用的: 這個軟件必須如何操作, 它的報表必須是什麼形式, 它必須如何與稅法一致, 等等."
相反, 一個操作系統則不為其外觀所限制. 當設計一個操作系統 時, 編程人員只要在機器與人的思維之間尋找一種最簡單的和諧就可以樂. 這就是為什麼操作系統更容易設計."
軍機大臣點點頭, 笑樂. "說來也是. 但要想檢測和糾正其中的錯誤, 哪個更容易呢?"
程序員沒有回答.

3.4
A manager went to the master programmer and showed him the requirements document for a new application. The manager asked the master: "How long will it take to design this system if I assign five programmers to it?"
"It will take one year," said the master promptly.
"But we need this system immediately or even sooner!
How long will it take if I assign ten programmer to it?"
The master programmer frowned. "In that case, it will take two years."
"And what if I assign a hundred programmers to it?"
The master programmer shrugged. "Then the design will never be completed," he said.
一位經理到編程大師那裡, 交給他一份有關一個新應用程序的需求說明. 經理問編程大師: "如果我分配五個程序員給你, 你需要多久能設計好這個系統?"
"那將花費一年時間." 大師立刻回答.
"但我們馬上需要這個系統. 甚至要求更快! 如果我分配十個程序員給你, 你需要多長事件?"
大師皺樂皺眉頭, "那樣的話, 需要兩年."
"如果我分配一百個程序員給你怎麼樣?"
大師聳樂聳肩膀, "那麼這項設計將永遠無法完成." 他說.


Book Two The Ancient Masters
第二篇 古代的大師

Thus speak the master programmer:
"After three days without programming, life becomes meaningless."
編程大師如是說:
"倘若三天不編程, 生活將變得毫無意義."

2.1
The programmers of old were mysterious and profound. We cannot fathom their thoughts, so all we can do is describe their appearance.
Aware, like a fox crossing the water.
Alert, like a general on the battlefield.
Kind, like a hostess greeting her guests.
Simple, like a uncarved blocks of wood.
Opaque, like black pools in darkened caves.
Who can tell the secrets of their hearts and mind?
The answer exists only in Tao.
老一輩的程序員是神秘的, 深奧的. 我們沒法揣摩他們的想法, 我們所能做的只是描述一下他們的表象.
他們是清醒的, 就像一只游過水面的狐狸;
他們是警惕的, 就像一位戰場上將軍;
他們是友善的, 就像一位招待客人的女主人;
他們是簡單的, 就像未經刻鑿的木頭;
他們還是難以琢磨的, 就像黑暗的洞穴中漆黑的池水.
誰能說出他們心中的秘密?
答案只存在於道中.

2.2
Grand Master Turing once dreamed that he was a machine.
When he awoke he exclaimed:
"I don't know whether I am Turing dreaming that I am a machine, or a machine dreaming that I am Turing!"
計算機科學巨擘圖靈曾經夢到他是一台機器.
當他醒來時, 他驚歎道:
"我不知道----我是夢到樂自己是台機器的圖靈, 還是一台夢到樂自己是圖靈的機器?"

2.3
A programmer from a very large computer company went to a software conference and then returned to report to his manager, saying: "What sort of programmers work for other computer companies? They behaved badly and were unconcerned with appearances. Their hair were long and unkempt and the clothes were wrinkled and old. They crashed our hospitality suite and they made rude noises during my presentation."
The manager said: "I should never sent you to the conference. Those programmers live beyond the physical world. They consider life absurd, and accidental coincidence. They come and go without knowing limitations. Without a care, they live only for their programs. Why should they bother with social conventions?
They are alive within the Tao."
一家大公司的一名程序員參加樂一次軟件研討會. 他回來後向經理匯報說: "為其他公司工作的那些程序員都是些什麼樣的人啊? 他們行為拙劣, 不顧及自己的儀表. 他們的頭發又長又亂, 衣服有皺又舊. 他們闖進我們的會客組, 還在我演講時發出粗魯的喧鬧."
經理說: "我本不應該派你去參加這個會議, 那些程序員是生活在物質世界之外的. 他們認為生活是荒唐的, 只是一種意外的巧合. 他們來去自如. 他們只為他們的程序而活著, 無憂無慮的活著. 為什麼要用社會習俗來約束他們呢?
他們活在此道之中."

2.4
A novice asked the master: "Here is a programmer who never designs, documents, or tests his programs. Yet all who know him consider him the one of the best programmers in the world. Why is this?"
The master replied: "The Programmer has mastered the Tao. He has gone beyond the need for design; he does not become angry when the system crashes, but accepts the universe without concern. He has gone beyond the need for documentation; he no longer cares if anyone sees his code. He has gone beyond the need for testing; each of his programs are perfect within themselves, serene and elegant, their purpose self-evident.
Truly, he has entered the mystery of Tao."
一個初學者問大師: "有位程序員, 從不構思, 編寫文檔或測試他的程序, 然而所有知道他的人都認為他是世界上最棒的程序員. 這是為什麼呢? "
大師回答說: "那個人掌握樂道. 他不需要事先進行構思, 當系統崩潰時, 他不會因此而悶悶不樂, 而是心平氣和的接受整個事實. 他還從編寫程序說明文檔的需要之中超脫樂出來, 不在意是否有人看他的編碼.
他也不需要進行測試, 他的每個程序的內在都完美無缺, 寧靜而優雅, 程序的用途也顯而易見.
的的確確, 他已經進入樂道的神奇境界."

mainapp_part1.png
mainapp_part2.pngmainapp_part3.png

FF存在的理由

| No Comments | No TrackBacks
明明已經不再喜歡FireFox, 干嘛還在保留在Dock中呢?
firefox in dock
僅僅因為safari尚不能支持126免費郵箱...而已...

126 report center
有朝一日, 下圖的對話框消失之日, 便是FireFox離開之時...
126 safari error
Book One The Silent Void
第一篇 寂靜的空宇

Thus speak the master programmer:
"When you have learned to snatch the error code from the trap frame, it will be time for you to leave."
編程大師如是說:
"當你有本事奪走我手上這塊水晶石時, 就是你出師的時候樂."

1.1
Something mysterious is formed, born in the silent void.
Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I don not know its name, so I will call it the Tao of Programming.
If the Tao is great, then the operating system is great. If the operating system is great, then the compiler is great. If the compiler is great, then the application is great. The user is pleased and there is harmony in the world.
The Tao of Programming flow far away and returns on the wind of morning.
在寂靜的空宇裡, 一種神奇的物質形成並誕生樂. 它立刻便靜止樂, 獨自守候著, 毫無動靜, 然而又處於永恆的運動之中. 它是所有程序的源頭, 我不知道它的名字, 所以我將它稱為編程之道.
如果此道是完美的, 那麼操作系統就是完美的; 如果操作系統是完美的, 那麼編譯程序就是完美的; 如果編譯程序是完美的, 那麼應用程序就是完美的. 用戶滿意之至----和諧便應運而生.
編程之道流逝遠去, 又乘著晨風而歸.

1.2
The Tao gave birth to machine language. Machine language gave birth to the assembler.
The assembler gave birth to the compiler. Now there are ten thousand languages.
Each language has its purpose, however humble. Each language expresses the yin and yang of software. Each language has its place with the Tao.
But do not program in Cobol if you can avoid it.
此道產生樂機器語言, 機器語言又產生樂匯編語言.
匯編語言產生樂編譯程序, 如今有樂上萬種的語言.
每一種語言都有其各自的卑微用途. 每一種語言都表達出軟件的陰和陽. 每一種語言都在此道之中有其一席之地.
但是, 應盡量避免用COBOL語言編寫程序.

1.3
In the beginning was the Tao.
The Tao gave birth to Space and Time.
Therefore Space and Time are the Yin and Yang of Programming.
Programmers that do not comprehend Tao are always running out of time and space for their programs.
Programmers that comprehend Tao always have enough time and space to accomplish their goals.
How could it be otherwise?
宇宙之初有道.
道產生的空間和時間.
空間和時間便是程序設計之陰陽.
不能領悟此道的編程者總是耗盡他們所要編寫的程序的時間和空間.
而領悟樂此道的編程者卻總有足夠的時間和空間來達到他們的目標.
除此之外, 難道還有其他的情形嗎?

1.4
The wise programmer is told about Tao and follows it. The average programmer is told about Tao and search for it. The foolish programmer is told about Tao and laughs at it.
If it were not for laughter, there would be no Tao.
The highest sounds are hardest to hear.
Going forward is a way to retreat.
Great talent shows itself late in life.
Even a perfect program still has bugs.
The Tao is hidden beyond all understanding.
精明的編程者聽說樂此道, 並遵循它; 平庸的編程者聽說樂此道, 並尋覓它; 愚蠢的編程者聽說樂此道, 卻嘲笑它.
要不是因為有嘲笑, 道也就不復存在樂.
最高的聲音最難被聽見. 
前進也是一種倒退.
大器晚成.
即使一個完美的程序也仍然會有隱患.
道深藏不露, 難於理解.


Melodies of Life

| No Comments | No TrackBacks
Melodies of Life
singer:Emiko Shiratori
Alone for a while I've been searching through the dark
For traces of the love you left inside my lonely heart
To weave by picking up the pieces that remain
Melodies of life - love's lost refain
Our paths they did cross
though I cannot say just why
We met, we laughed, we held on fast
and then we said goodbye
And who'll hear the echoes of stories never told
Let them ring out loud till they unfold
In my dearest memories
I see you reaching out to me
Though you're gone
I still believe that you can call out my name
A voice from the past
joining yours and mine
Adding up the layers of harmony
And so it goes, on and on
Melodies of life
To the sky beyond the flying birds
forever and beyond
If I should leave this lonely world behind
Your voice will still remember our melody
Now I know we'll carry on
Melodies of life
Come circle round and grow deep in our hearts
as long as we remember

核心應用程序框架

| No Comments | No TrackBacks
core program framework.png
NSResponder 響應對象
NSResponder是一個抽象類, 它使其所有的派生類具有事件處理(event handling)能力. 它定義了一套消息在不同的鼠標和鍵盤事件發生時調用. 它同樣定義了應用程序對象之間的事件處理機制. NSResponder類定義了應用程序中所有事件處理對象, 處理消息的默認行為. 相應對象模型基於一下三個概念:
事件消息
直接輸入與事件相配合的消息, 比如鼠標點擊或鍵盤按鍵等.
動作消息
這種消息描述更高級別的命令, 如cut: 或者paste: 等
響應對象鏈
響應對象鏈是指被事件或動作消息應用的一系列響應對象. 如果消息沒有被給定的響應對象處理, 就會傳給鏈中的下一個對象.
相應對象鏈允許相應對象把的自己職責委托給系統中的其他對象. 相應對象鏈中的對象順序由應用程序的視圖, 窗口和NSApp對象之間的相互關系絕定. 視圖的下一個相應對象往往是它的父視圖, 內容視圖的下一個相應對象則是窗口本身. 在窗口之後, 事件最終傳遞給NSApp對象.

動作消息的相應對象鏈要更長一些, 即消息一直沿著相應對象鏈一直傳到窗口, 如果該對象序列出於鍵窗口中, 消息還會在主窗口中沿同樣路徑傳遞, 直到最後傳給NSApp對象.

第一相應對象
第一響應對象是窗口中第一個對鍵盤事件做出響應的視圖對象, 應用程序的每個窗口都會跟蹤其視圖層次結構中的第一個響應對象的狀態. 例如在TextEdit的應用程序中, 新文檔的窗口就是第一響應對象, 因為它第一個接受來自鍵盤的事件. 窗口默認的第一響應對象就是它自己, 但鼠標在窗口中點擊的任何視圖都能使其成為第一相應對象. 如果該視圖不能處理事件, 就傳給相應對象鏈中的下一個對象.

在多窗口的桌面環境下, 用戶可以用鼠標點擊的方式激活窗口. 此時該窗口成為鍵窗口, 它的第一相應對象就成為任何用戶事件的目標. 如果又選中了別的窗口, 那麼當前窗口就成為鍵窗口, 它的第一響應對象也就成為當前的第一相應對象. 如果沒有選中對象或者窗口中沒有控件, 那麼窗口自身就是第一響應對象.

您可以用Interface Builder或通過編程的方式設置initialFirstResponder(初始響應對象), 讓最先能合乎邏輯地使用鍵盤的控件在窗口出現時獲得焦點, 成為第一相應對象.

視圖和控件可以拒絕使自己處於第一響應對象狀態, 例如顯示靜態圖像的視圖也許不接受成為第一相應對象的狀態, 相應對象可以通過實現acceptFirstResponder方法並返回NO來表示它不像處於第一響應對象的狀態.

事件路由
事件基於其類型進行路由. NSApp對象把大部分的事件消息發給發生用戶動作的窗口. 其中, 鼠標事件會轉發給該窗口的視圖層次結構中被鼠標點擊的視圖; 鍵盤事件會轉發給第一響應對象. 如果視圖對象可以響應該事件或者說它接受成為第一響應事件的狀態, 並且定義了與事件消息對應的NSResponder方法, 那麼它就負責處理事件, 否則, 這個事件就會轉發給對象響應鏈中的下一個相應對象.
NSWindow類自行處理了一些事件而不傳遞給視圖, 比如窗口移動(window-moved), 窗口尺寸變更(window-resized)和窗口暴露(window-exposed)事件. NSApp也會自行處理一些事件, 比如應用程序激活(application-active)和應用程序休眠(application-deactivate)事件.

NSWindow 窗口
每個NSWindow對象都管理屏幕上一個對應的實際窗口. 它繪制窗口的框架並響應用戶針對窗口的動作, 如關閉, 移動, 改變尺寸以及對窗口的其他操作.NSWindow的主要功能是在它的內容區域(content area)
......
(累了....看demo去了...下次繼續)







About this Archive

This page is an archive of entries from September 2008 listed from newest to oldest.

August 2008 is the previous archive.

October 2008 is the next archive.

Find recent content on the main index or look in the archives to find all content.