2010年7月30日 星期五

iPhone stringWithContentsOfFile is Deprecated

如果在使用stringWithContentsOfFile時候
出現Deprecated的警告滿煩人的
雖然編譯會過,但是程式run到那邊鐵定crash
通常會出現警告都是用法上的問題
1
[NSString stringWithContentsOfFile:filePath];

請替換成
1
2
3
[NSString stringWithContentsOfFile:filePath
                          encoding:NSUTF8StringEncoding
                             error:nil];

很簡單的小技巧
資料來源:keyvisuals

2011/04/21 Updates:
都已經提了stringWithContentOfFile沒有講講怎麼拿到file的path好像說不過去
方法如下:

1
2
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"foo"
                                                    ofType:@"html"];

2010年7月28日 星期三

Olivia Ong - All out of love

記得我好幾年前(2006)推薦了Olivia的basanova專輯
沒想到近來因為電視台的歌唱選秀節目而聲名大噪

這首翻唱的經典名歌
在她詮釋下別有一番味道
簡單的吉他和弦搭配清澈嗓音
帶有那麼一點點失意與惆悵感
主旋律沒有用太過高亢強烈的唱法
娓娓道來,百聽不厭...



I'm lying alone with my head on the phone
Thinking of you till it hurts
I know you hurt too but what else can we do
Tormented and torn apart

I wish I could carry your smile in my heart
For times when my life seems so low
It would make me believe what tomorrow can bring
When today doesn't really know, doesn't really know

I'm all out of love, I'm so lost without you
I know you were right, believing for so long
I'm all out of love, what am I without you
I can't be too late to say that I was so wrong

I want you to come back and carry me home
Away from these long, lonely nights
I'm reaching for you, are you feeling it too?
Does the feeling seem oh, so right?

And what would you say if I called on you now?
And said that I can't hold on?
There's no easy way, it gets harder each day
Please love me or I'll be gone, I'll be gone

I'm all out of love, I'm so lost without you
I know you were right, believing for so long
I'm all out of love, what am I without you
I can't be too late to say that I was so wrong

Ooh, what are you thinking of
What are you thinking of
What are you thinking of
What are you thinking of

I'm all out of love, I'm so lost without you
I know you were right, believing for so long
I'm all out of love, what am I without you
I can't be too late to say that I was so wrong

I'm all out of love, I'm so lost without you
I know you were right, believing for so long
I'm all out of love, what am I without you
I can't be too late to say that I was so wrong

I'm all out of love, I'm so lost without you
I know you were right, believing for so long
I'm all out of love, what am I without you
I can't be too late to say that I was so wrong, so wrong


2010年7月21日 星期三

iPhone 檢查飛航模式是否為開啟狀態

要在程式中檢查iPhone是不是有把 flight mode開啟
作法其實很簡單,連半行code都不用加

首先,找到專案的.plist
接著新增一個欄位 key值為SBUsesNetwork


然後在右邊的value空白處按右鍵
變更value type為boolean並且確定是勾選的狀態
                         為integer並且設定值為3

附註:在xocde開發過程中 simulator並沒有飛航模式可以選擇
因此,我還在研究要怎麼驗證這個功能 :p
參考連結:blog.shallarp.com

2010年7月20日 星期二

iPhone 使用NSMutableURLRequest 參數TimeoutInterval沒起作用

通常有用到http connection的人都會需要處理connection timeout問題

但是最近卻遇到了TimeoutInterval沒起作用的問題
這是一般的用法:

1
2
3
4
NSMutableURLRequest* request = [[NSMutableURLRequest alloc
          initWithURL: [NSURL URLWithString: serverurl]  
          cachePolicy:NSURLRequestReturnCacheDataElseLoad 
          timeoutInterval:240];

但是根據Apple官方的論壇提到 timeout預設值為240秒
只要低於這個值都會被自動忽略


所以想把timeout時間設短的人就需要自行做一個timer

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
SEL sel = @selector(customCancel);
NSMethodSignature* sig = [self methodSignatureForSelector:sel];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:sig];
 
[invocation setTarget:self];
[invocation setSelector:sel];
 
NSTimer *timer = [NSTimer timerWithTimeInterval:CUSTOM_TIMEOUT
        invocation:invocation
        repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer
     forMode:NSDefaultRunLoopMode];

把上面這段程式跟NSMutableURLRequest放在一起
接著實作 customCancel 這個部分

1
2
3
4
5
6
- (void)customCancel{
 if (connection) {
  [connection release];
  connection = nil;
        }
}

這樣就可以了!
參考資料:stackoverflow

2010年4月21日 星期三

Build Failed with Link Error (開發iPhone過程中遇到Link error 的解決方法)

由於專案需要,從農曆年後開始接觸 iPhone程式的開發
開發過程中正巧遇到一個很奇怪的 bug 一直編不成功
試著從紅色驚嘆號來追看看兇手到底藏在哪邊

結果xcode的debug只告訴我
Link /Users/administrator/...
 ".objc_class_name_NSManageObject", referenced from:
.objc_class_name_Camera in Camera.o
ld: symbol(s) not found

上google查了的結果真的少的可憐...
不過很明顯是Link出了問題

後來搞清楚是某個.m檔 有import <CoreData/CoreData.h>
但是整個專案的Frameworks裡面沒有加進去啦

解決方法:

step1.


step2. (我就是少了CoreData這一味啦)

2010年4月15日 星期四

How to update file's version automatically in MFC (如何在MFC專案中自動更新版號)

最近工作上被argue在軟體的版本編號方式要長得像這樣:
X.YY (build ZZ)
而我原本在wiki上面參考來的編法是這樣的:
a.b.yy.mmdd
不過customer永遠是對的,那就來改一下吧
只是這樣一來,我的程式資訊需要紀錄我build的情況
光有svn再到project 的rc檔裡面慢慢改實在很沒效率

幸好網路上有找到相關資源,而且一試就成功 (it works like a charm!)
我這邊簡單中文筆記一下~
1. 首先找到專案資料夾下的 "ProjectName.rc",用文字編輯器打開

2. 接著在同樣的路徑下應該還會有個 res檔案夾 打開裡面有個 "ProjectName.rc2" ,
同樣也是用文字編輯器打開

3. 把"ProjectName.rc"裡頭的這個部份通通剪下來
//////////////////////////////////
//
//Version
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,0,0
PRODUCTVERSION 1,0,0,0
FILEFLAGMASK 0x3fl
#ifdef _DEBUG
...
...
BEGIN
VALUE "Translation", 0x409, 1200
END
END

然後貼到"ProjectName.rc2" "Add manually edited resources here..."註解之後
不過"ProjectName.rc2"在編輯完,記得在最後一行按個Enter(回車鍵)讓他換個行
不然編譯時就會出現 fatal error RC1004: unexpected end or file found 的錯誤訊息
害我走了好多冤枉路阿...

4. 回頭在"ProjectName.rc"裡面 找到
#ifdef APSTUDIO_INVOKED
#include "targetver.h"
#endif
#include "afxres.h"
#include "res\ProjectName.rc2" //<-- add this line
然後將rc與rc2兩個檔案儲存

5. 快完成了~接下來在專案的header files資料夾下新增一個VersionNo.h的檔頭
#define FILEVER 1,0,0,1
#define PRODUCTVER 1,0,0,1
#define STRFILEVER "1, 0, 0, 1\\0"
#define STRPRODUCTVER "1, 0, 0, 1\\0"
兩個反斜線是為了我要抓後面的build number,網路上參考資料只有一條

6. 然後我們需要新增一個marco (tool->Marcos->New Marco Project)
Module1的project名稱可以一自己喜好更換,建立好雙擊那個module會開啟一個Marco 的IDE

7. 過來就是複製貼上囉~
Imports EnvDTE
Imports System.Diagnostics

Public Module BuildDone '<--------------- Your module name




Function GetProjectDir(ByVal FullName)

Dim proj_path

proj_path = Split(StrReverse(FullName), "\", -1, 1)



Dim count

count = UBound(proj_path)



Dim full_path

full_path = ""

Dim i



For i = 1 To count

full_path = full_path & "\" & proj_path(i)

Next



GetProjectDir = StrReverse(full_path)



End Function



Sub ReplaceText(ByVal objSel As TextSelection, _

ByVal count As Integer, ByVal incrementby As Integer, _

ByVal Type As Integer)



'selection represents the TextSelection object

'count represents the position of the version number

'to be incremented

'incrementally represents a number that will be added

'to the existing version number



Dim strTemp As String

Dim i



strTemp = ""

objSel.EndOfLine()



If Type = 0 Then

For i = 1 To count

If strTemp.StartsWith(",") = True Then

Exit For

Else

objSel.CharLeft(True, 1)

strTemp = objSel.Text

End If

Next

strTemp = strTemp.Remove(0, 1)

strTemp = strTemp + incrementby

objSel.Text = "," & strTemp

Else

For i = 1 To count

If strTemp.StartsWith("\") = True Then

Exit For

Else

objSel.CharLeft(True, 1)

strTemp = objSel.Text

End If

Next

strTemp = strTemp.Remove(0, 1)

strTemp = strTemp.Remove(strTemp.Length - 1, 1)

strTemp = strTemp + incrementby

objSel.Text = "\" & strTemp & """"

End If



End Sub



Dim WithEvents bldevents As BuildEvents

Dim applicationObject As EnvDTE.DTE



Sub BuildDoneEvents()

Dim addInInstance As EnvDTE.AddIn



applicationObject = CType(Application, EnvDTE.DTE)

bldevents = CType(applicationObject.Events. _

BuildEvents, EnvDTE.BuildEvents)

End Sub



Private Sub bldevents_OnBuildDone(ByVal _

Scope As EnvDTE.vsBuildScope, _

ByVal Action As EnvDTE. _

vsBuildAction) Handles _

bldevents.OnBuildDone

'This event will be triggered after every build

'of a project



'Obtain the full path of the active project

Dim full_path

full_path = GetProjectDir(DTE.ActiveDocument.Path)



full_path = full_path & "VersionNo.h" '<----------Your header name



'Open the VersionNo.h file

Dim doc As Document

DTE.ItemOperations.OpenFile(full_path)



Dim objDoc As TextDocument

'Obtain the TextSelection object

objDoc = DTE.ActiveDocument.Object("TextDocument")



Dim objSel As TextSelection = _

DTE.ActiveDocument.Selection



objSel.StartOfDocument()



'Increment the version information

ReplaceText(objSel, 5, 1, 0)

objSel.LineDown()

objSel.StartOfLine()

ReplaceText(objSel, 5, 1, 0)

objSel.LineDown()

objSel.StartOfLine()

ReplaceText(objSel, 5, 1, 100)

objSel.LineDown()

objSel.StartOfLine()

ReplaceText(objSel, 5, 1, 100)



ActiveDocument.Save()

ActiveDocument.Close()

End Sub





End Module



8. save之後回到原本VC介面 在Marco Explorer裡面對我們的module右鍵按下選擇run check 一下VersionNo.h ,成功的話會發現數字隨著build次數增加 嗯~至於如何在程式當中取得version 可以使用VS_FIXEDFILEINFO ,VerQueryValue ,GetFileVersionInfoSize ,GetFileVersionInfo 幾個配合,並透過 HIWORD(fixed_file_info->dwFileVersionMS)
LOWORD(fixed_file_info->dwFileVersionMS)
HIWORD(fixed_file_info->dwFileVersionLS)
LOWORD(fixed_file_info->dwFileVersionLS)
來取得version number ,還有很多詳細的作法網路上很好找到

AfxExtractSubString 可以取得特定位置的字串
ex: AfxExtractSubString(str, FullString, 1, '\\');
這邊的例子是將字串以 "\" (反斜線)切割, 取位置1的字串
也就是說, 如果我的FullString=1, 0, 0, 1\7 ,那我就會取到 7


2009年10月6日 星期二

很多的"不得不..."

很久沒回來看看我長滿雜草的網誌了
這段期間發生了好多事情
有些如過眼煙雲,稍縱即逝
有些刻骨銘心,至今回想仍嚼然有味
於是好好的來整理一下吧~
我想這是我第一個"不得不"做的事情

"不得不"的狂暴寫論文
如果用"飛快"還不足以形容兩年研究所生涯的時間之快
那...可能是我都把時間的感知機能給關閉了吧
不過為這兩年畫下的句點的過程可說是驚險刺激
原以為早早謀畫的論文主題能夠讓我最後這段畢業時光輕鬆點
不過好大的一個陷阱跟千層派一樣,掉進一個還有一個
真的要對我身旁的人好好說聲謝謝
謝謝他們忍受我的無病呻吟,躁鬱情緒
更謝謝幾個好友,學長以及最親的人還適時的打氣與討論
我才能夠及時而順利的畢業 (當然老闆肯簽名也是必然的條件:P)

"不得不"的Oral exam
我不會忘記口試當天的世紀日蝕奇景
跟舜哥兩人還偷閒,用實驗室1.4吋的磁碟片觀賞日蝕
清楚的日蝕景觀讓我們兩個都感覺滿驚奇的
恩~口試過程中最擔心的李強爺爺,問題犀利不過人真的很和善阿
(希望魚羊鮮豆的咖啡跟起司條有合口委的胃口)
哈~還有我意外發現的西式盒餐 Daily Deli 都供奉給口委
(中西式混搭的還不賴,口味清淡,質感高,店員又正,強力推薦!! XD)
雖然我還是不太滿意自己present的結果
不過委員們的分數滿會安慰人的

"不得不"的最後一役
然後我也不會忘記那個一延再延的conference ISCIT 2009
那是我在畢業之前最後想完成的一件事居然也是一波三折
從一開始的5/8截稿到5/22又到6/22最後...7/11
(喂~嘛幫幫忙,我都押寶在這個conference上了,眼睜睜看著其他也不錯
的conference都截稿了,不要這樣玩我吧...)
還害我白趕了好幾天的稿...人都快掛了
恩~我可能就是愛自找麻煩吧:P
想轟轟烈烈的從碩士班畢業就多來 WikiCFP 找自己甲意的onference吧
(conference資訊主要都是資工領域的,尤其又以communication跟ai為大宗)

"不得不"的逃兵韓國行
剛剛提到的conference還有一個讓我相當頭大的事情
就是會議日期正巧撞上入伍的第一天...
想跟國防部請假都不行,只好再把入伍日子往後推囉~
不過這趟韓國行真的是把我口袋都燒出個洞囉
撇開機票因為時間上的一些考量買到8800多元
首爾住的地方我也挑了個夠便宜的Bebop House(一個晚上23000W約630元)
可是conference註冊跟我要了US$400,
還有仁川海埔新生地那鳥不生蛋的地方,
最便宜的五星級飯店兩個晚上打完折要價18萬韓圜(約5000元)
可憐的是我前往韓國時已不復學生身分
所以討不到一毛錢阿~嗚嗚

這是第二次來韓國
他們有跟郝斌斌一樣的口號
"一年一條地鐵通車" O.O
全新的9號線挺舒適的,跟內湖線一樣都有新車車的味道
周末先跟朋友去了首爾大公園 還有 首爾跑馬場
很有趣的是我的韓國朋友們從來沒來過這些地方
所以她們每次都很期待我們台灣朋友過去旅遊順便一起去觀光
(就像你問我台南有哪裡好玩的,我也是一頭霧水啦:P)

很鮮豔的花圃配上大熱天 O.O

動物園門口的獨照(mark挺不賴的吧 ^^)

人山人海的賽馬場
我發現阿~每場賽馬時間間隔不長
但是場場賭注都累積上億台幣
我們賭100韓圜是不是有點像在鬧場阿XD

周末與朋友道別後,我跑到江邊的테크노마트 (TM21:TechnoMart)
據朋友的描述這邊比起龍山的電子街,
對於語言不通的外國人來說比較不易被敲竹槓
ㄏㄏ~至於我的戰利品嘛
cowon s9 入手 (螢幕是3.3" AMOLED, 解析度480x272 全觸控面板並支援藍芽耳機)
此外老闆還問我需不需要可以收看DMB數位電視的版本
我心想~雖然這功能回台灣就廢掉了,不過很想體驗一下人家數位電視的魅力阿
後來我在飯店看了一整晚的連續劇:P

搭了一個半小時的地鐵來到仁川的松島
下榻的飯店就在仁川地鐵線仁川大學站的對面
斜對角還有外型搶眼的투모로우시티(Tomorrow City)
BENIKEA premier Songdo Metro Hotel目前還獨自聳立在一片曠野間
稍顯孤獨但是可遠眺即將完工的仁川大橋,仁川大學以及仁川城市慶典的會場
北邊有中央公園,NEATT大樓,貴到不行的希爾頓飯店
和這次會議的舉行的地點 Convensia (外型跟雪梨歌劇院有點相似:P)

28mm廣角怎麼拍就是沒辦法全部入鏡,可能要到對街吧

然後很開心在晚宴上與北海道大學來的Jaeseong聊了很多東西
此外,整個會議期間只有遇到中央做音視訊處理的張寶基老師跟他的學生
ㄏㄏ~雖然偶爾會有點孤單的感覺,不過卻也最放得開去認識其他人

恩~已經寫了滿多了,
之後再找時間陸陸續續把心得感想補齊吧
我要說這上班時間寫網誌真的是很痛苦的一件事情XD

內容回應