2011年7月20日 星期三

Date Formatter Example

In case you are saving a date in MySQL's "datetime" type, you can parse it as a string and store it as a NSDate object like this:


NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:28800]];
NSString * date_string = [NSString stringWithFormat:@"%@", [yourdata objectForKey:@"a_date"]];
NSDate * ed = [formatter dateFromString: date_string];
...
[formatter release];

That is! It is really tedious if you have to look back the reference for this convention! This can be a useful remainder.


2011年2月24日 星期四

Update IP / hostname in SCM of XCode

For anyone who would update / change the IP of their SCM in XCode, you can't finish your task if simply reconfigure the URL in your SCM setting. What you have to do is to update the URL manually in SVN by opening the terminal:

1. Close your projects and XCode

2. Open Terminal and "cd" to your project directory

3. Type "svn sw --relocate [old location] to [new location]"

For example: 

svn sw --relocate http://admin@www.old.com:3690/svn/prj http://admin@www.new.com:3690/svn/prj

4. Launch your project again and your SCM will work properly!



2011年2月13日 星期日

UITableView簡說 II

基於觀看的網友多是外國人,之後的文章都會以英文選寫:)


In the part I of UITableView, we are not really going to use the cells in the table. Now, we are trying to do so in a very simple way.


Firstly, create a View-based Application:




In Classes group, we add a new file for our simple table's view controller:



We select UIViewController subclass, and please do remember to tick UITableViewController subclass option in Options section:




Give it a name, say MyTableViewController:



Double-click MyTableViewController.xib to take a look on what it's inside:



You may right-click the Table View and see that it already has three relations with File's Owner (MyTableViewController). They are for table's data feeding and view management. If we don't tick UITableViewController subclass option in Options section while making a new view controller class, those Table View and relations won't be automatically generated, and you should make it yourself.


Let's go back to our newly created view controller MyTableViewController, and jump to numberOfSectionsInTableView: method:



This function will be called every time the table refresh / reload the data. It asks for the number of sections of the table. Let's return 1 for this method:



Let's jump to tableView:numberOfRowsInSection: method:



This method will be called by the table and we have to return the number of rows in a specified section of the table. In our case, we only have 1 section, so we should directly return the number of rows, without considering which section we are dealing with. For example, we return 10:



Now, we are really making our cells to print something out. Let's jump to tableView:cellForRowAtIndexPath: method:



Let's add a line just below "// Configure the cell..." comment:


This line set the text attribute of a UILabel instance to a NSString instance which is the index number (0 to 9) of a certain row.


Now, we will add a view controller class in MainWindow.xib. Double-click it in Resources folder:






 Open Library window from "Tools -> Library". Drop a View Controller class to MainWindow.xib:






With the newly added View Controller item selected, open Inspector window by clicking "Tools -> Inspector":



Assign NIB Name in View Controller Attributes tab:



Assign Class My Table View Controller Identity tab:



Let's add a class member of MyTableViewController in TableViewTestAppDelegate.h:



Also, add something for synthesizing getter and setter in TableViewTestAppDelegate.m:


Add a line:

@synthesize myTableViewController;

This will generated setter and getter of myTableViewController for you.


Also, jump to dealloc method for memory issue:



Let's add a line to release the view controller:



Go back to MainWindow.xib again. Make a relation between TableViewTest App Delegate with File's Owner. Dragging New Referencing Outlet onto it and select myTableViewController  we defined before in TableViewTestAppDelegate.h:




Go back to TableViewTestAppDelegate.m file and jump to application:didFinishLaunchingWithOptions: method:



Remark the original one as shown and add a new line to it:



Now, build and run the App on the simulator:


Finally, a table of 10 rows showing row indices is shown.









2011年2月7日 星期一

UITableView簡說 I

UITableView係一個非常之有用的component;你平時玩的Apps,好多都係用左佢來做!


要做一個有UITableView的實驗Apps:



選擇Navigation-Based Application:




在Resources內,已經預設了個RootViewController.xib內有個UITableView。當然,沒有的話你可以自己在Interface Builder內加入UITableView。




值得留意是numberOfRowsInSection這個方法:


它的return type是NSInteger,即是一個Section內有幾多個橫項。

另一個焦點是cellForRowAtIndexPath方法的實作:


每當個UITableView要顯示一個cell時,個方法都會被call一次,目的是拿取某個cell的資料或對cell的visual customization。

同時,你可見到code內的CellIndentifier,用途是重用已被配置的cell resources。這可提升使用效能。因為當cell數目太多時,每alloc一個cell,記憶體就用多了...而且,iPhone的screen size有限(跟desktop比),同一時間show晒cell的機會好少。

另外,indexPath是代表你正處理那一個cell的資料,即indexPath.row。你可看看row的數值是什麼,例如第一個row是0,第二個是1等等...


當某某cell被按下時,我們要跳去另一畫面,又怎handle?請參考didSelectRowAtIndexPath。


跟cellForRowAtIndexPath原理相同,indexPath代表第幾個cell被select了,都可以由indexPath.row內找到,由0到n-1個cell。如果現在的view controller係under另一個navigation controller,你可以直接把新的view controller加到現有的navigation controller。此外,你亦可使用presentModalViewController等方法顯現新的view controller。



2011年2月4日 星期五

Objective-C 跟 C++

Objective-C 內的member method C++有dd分別。C++內是用的實體的pointer去行member method, 但Objective-C用的機制是messaging, 所以在Objective-C內nil的pointer係可以call member method而唔會crash!

C++:

SomeClass * instance = null;
instance->SomeMethod();

CRASH @.@

Objective-C:

SomeClass * instance = nil;
[instance SomeMethod];

SAFE, but returns nil
^.^

Push Notification - sever記

iPhone上高有個功能叫 Push Notification,可以simulate短信形式發放通知。有d apps, 例如 What's App... TextMe 都會用佢simulate短信收發。

當中涉及起一個web server, 同可選擇使用Open-source的 APNS-PHP:

http://code.google.com/p/apns-php/

但佢要gei config都幾special,特別係pcntl這個extension! 
我使用Ubuntu 10.04,但default的PHP setting係冇install到這extension. 所以,只好下載PHP5的source,再make過:

mkdir php
cd php
apt-get source php5
cd php5-(WHATEVER_RELEASE)/ext/pcntl
phpize
./configure
make

注意的是"phpize"係要install的:

apt-get install php5-dev

然後再copy個pcntl.so到/usr/lib/php5/(extension_folder)/入面。

同埋,個pcntl.ini要加到/etc/php5/apache2/conf.d/內。

pcntl.ini有:
extension=pcntl.so

最後就係restart Apache2:

/etc/init.d/apache2 restart

行一行phpinfo, 會見到:

pcntl support enabled