<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Macfeteria &#187; advance</title>
	<atom:link href="http://www.macfeteria.com/blog/tag/advance/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.macfeteria.com</link>
	<description>All About Mac and iPhone Programming</description>
	<lastBuildDate>Tue, 09 Aug 2011 04:34:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Cocoa Programming II : NSTableView Advance</title>
		<link>http://www.macfeteria.com/blog/2008/08/08/cocoa-programming-nstableview-advance/</link>
		<comments>http://www.macfeteria.com/blog/2008/08/08/cocoa-programming-nstableview-advance/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 10:43:02 +0000</pubDate>
		<dc:creator>[Ter]</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[advance]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[NSTableView]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.macfeteria.com/?p=282</guid>
		<description><![CDATA[หลังจากได้ลองเขียน NSTableView ดูกันแล้ว วันนี้ก็ยังคงต่อด้วยเรื่องของ NSTableView เหมือนเดิมแต่ว่าเป็นขั้น Advance มากกว่าเดิม More about datasource จากโปรแกรมแสดงตารางรายชื่อครั้งก่อน จะเห็นว่าเราได้ set datasource มาที่ AppController และเราก็เขียน implement delegate ของ datasource ที่ AppController จะเห็นว่าวิธีแบบนี้ไม่ค่อยจะยืดหยุ่นมากนัก เพราะเนื่องจากว่าเราให้ AppController เป็นทั้งตัวควบคุม interface และยังให้เป็น datasource สมมติว่าเราต้องการมี table มากกว่า 1 table และแต่ละ table มี datasource ที่มีข้อมูลต่างกัน เราก็คงต้องเพิ่ม datasource ให้เท่ากับจำนวนของ table ซึ่งนั่นก็แปลว่าเราก็ต้องสร้าง AppController instance มาหลายๆอัน มันก็คงจะไม่ใช่วิธีที่ดีนัก และที่สำคัญตัวโปรแกรมจะมองว่า AppController instance ที่เราสร้างขึ้นมาใหม่ มันเป็นตัวเดียวกัน [...]]]></description>
			<content:encoded><![CDATA[<p>หลังจากได้ลองเขียน NSTableView ดูกันแล้ว วันนี้ก็ยังคงต่อด้วยเรื่องของ NSTableView เหมือนเดิมแต่ว่าเป็นขั้น Advance มากกว่าเดิม</p>
<h2><span style="color: #cccc33;">More about datasource</span></h2>
<p>จากโปรแกรมแสดงตารางรายชื่อครั้งก่อน จะเห็นว่าเราได้ set datasource มาที่ AppController และเราก็เขียน implement delegate ของ datasource ที่ AppController จะเห็นว่าวิธีแบบนี้ไม่ค่อยจะยืดหยุ่นมากนัก เพราะเนื่องจากว่าเราให้ AppController เป็นทั้งตัวควบคุม interface และยังให้เป็น datasource</p>
<p>สมมติว่าเราต้องการมี table มากกว่า 1 table และแต่ละ table มี datasource ที่มีข้อมูลต่างกัน เราก็คงต้องเพิ่ม datasource ให้เท่ากับจำนวนของ table ซึ่งนั่นก็แปลว่าเราก็ต้องสร้าง AppController instance มาหลายๆอัน มันก็คงจะไม่ใช่วิธีที่ดีนัก และที่สำคัญตัวโปรแกรมจะมองว่า AppController instance ที่เราสร้างขึ้นมาใหม่ มันเป็นตัวเดียวกัน ดังนั้นข้อมูลใน table A และ B ก็จะเหมือนกัน ( ดูรูปประกอบ ) ฉนั้นแล้วการที่เราให้ AppController เป็น datasource โดยตรงก็คงไม่ดีแน่</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2008/08/app_datasource.png" rel="lightbox[282]"><img class="size-medium wp-image-284 aligncenter" title="app_datasource" src="http://www.macfeteria.com/wp-content/uploads/2008/08/app_datasource-300x99.png" alt="" width="300" height="99" /></a></p>
<p style="text-align: left;">งั้นแทนที่เราจะให้ Class AppController เป็น datasource โดยตรง เราก็เปลี่ยนให้ AppController มี member เป็น datasource ก็น่าจะดีกว่า</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2008/08/datasource1.png" rel="lightbox[282]"><img class="size-medium wp-image-286 aligncenter" title="datasource1" src="http://www.macfeteria.com/wp-content/uploads/2008/08/datasource1-300x115.png" alt="" width="300" height="115" /></a></p>
<p style="text-align: left;">จากรูปข้างบน จะเห็นว่า เราก็สามารถมีหลายๆ datasource ที่ต่างกันได้ แล้วเราจะเขียน class ออกมาได้ยังไง ? งั้นมาดูตัวอย่างจริงกันเลยดีกว่า<span id="more-282"></span></p>
<p style="text-align: left;">สมมติว่า เราต้องการ เขียนโปรแกรมแสดงรายชื่อเหมือนเดิม แต่เราต้องการแยก class datasource ออกมาจาก AppController โดยให้ชื่อว่า StudentDataSource</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> StudentDataSource <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>m_arrayNameData;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> AddStudentRecord<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> name;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> numberOfRowsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSTableView</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> table;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>  tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSTableView</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aTableView 
        objectValueForTableColumn<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSTableColumn</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>
        aTableColumn row<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>rowIndex;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>จาก Class StudentDataSource ข้างบนเราก็ได้ให้ NSMutableArray เป็นตัวเก็บข้อมูล รายชื่อ และในส่วนของ implement ก็เขียนได้ลักษณะแบบนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> StudentDataSource
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
	m_arrayNameData <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> array<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>m_arrayNameData retain<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> dealloc
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>m_arrayNameData release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> numberOfRowsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSTableView</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> table
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>m_arrayNameData count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSTableView</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>table
      objectValueForTableColumn<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSTableColumn</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableColumn
      row<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> rows
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>m_arrayNameData objectAtIndex<span style="color: #002200;">:</span>rows<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> AddStudentRecord<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> name
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>name length<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#91;</span>m_arrayNameData addObject<span style="color: #002200;">:</span>name<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>ก็เป็นว่าตอนนี้เรามี Class ที่เป็น DataSource ต่อไปก็แก้ไข AppController ของเรา แบบนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> AppController <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
    IBOutlet <span style="color: #400080;">NSTableView</span> <span style="color: #002200;">*</span>m_tableView;
    IBOutlet <span style="color: #400080;">NSButton</span> <span style="color: #002200;">*</span>m_button;
    IBOutlet <span style="color: #400080;">NSTextField</span> <span style="color: #002200;">*</span>m_textFields;
&nbsp;
    StudentDataSource <span style="color: #002200;">*</span>m_dataSource;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> AddRecord<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>จะเห็นว่า เรามี member ที่เป็น StudentDataSource เข้ามาด้วย และในส่วน implement ก็จะได้แบบนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> AppController
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> init
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
	m_dataSource <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>StudentDataSource alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>m_dataSource retain<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> awakeFromNib
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>m_tableView setDataSource<span style="color: #002200;">:</span>m_dataSource<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> dealloc
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>m_dataSource release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> AddRecord<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> name <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>m_textFields stringValue<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>m_dataSource AddStudentRecord<span style="color: #002200;">:</span>name<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>m_tableView reloadData<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>จาก Code ข้างบนจะมีส่วนที่สำคัญคือ awakeFromNib จะเห็นว่าเมื่อโปรแกรมเริ่มทำงาน เราก็ Set DataSource ให้กับ m_tableView ในทันที และเราก็ไม่ต้องไป link ที่ interface builder เลย</p>
<p>ลอง <a href="http://www.macfeteria.com/wp-content/uploads/2008/08/studenttable.zip">Download Source</a> ไปลอง  Compile ดูครับ โปรแกรมก็ทำงานเหมือนเดิมแต่ ต่างกันตรงที่เราแยก datasource ออกมาให้เป็น member ของ AppController ต่อไปถ้าหากเราต้องการ มีหลายๆตาราง และ datasource ที่ต่างกัน ก็สามารถทำได้แล้วครับ</p>
<h2><span style="color: #cccc33;">Editable Cell</span></h2>
<p>โปรแกรมที่เราได้ลองเขียนนั้น เป็นแค่การอ่านข้อมูลมาแสดงผลอย่างเดียว ไม่สามารถ แก้ไขข้อมูลภายใน Cell ได้ ถ้าหากเราต้องการให้ table ของเราสามารถแก้ไขได้ เราต้อง implement delegate method เพิ่มอีก 1 นั่นก็คือ</p>
<p>- (void)<strong>tableView</strong> <img src='http://www.macfeteria.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> NSTableView *)aTable <strong>setObjectValue</strong> <img src='http://www.macfeteria.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> id)aData<br />
<strong>forTableColumn</strong> <img src='http://www.macfeteria.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> NSTableColumn *)aCol<br />
<strong>row</strong> <img src='http://www.macfeteria.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> int)aRow;</p>
<p>และจากโปรแกรมข้างบน ก็สามารถเขียนเพ่ิมให้ table ของเราสามารถแก้ไขข้อมูลได้ ดังตัวอย่าง code ข้างล่าง</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSTableView</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aTable setObjectValue<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>aData
        forTableColumn<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSTableColumn</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aCol
        row<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>aRow
<span style="color: #002200;">&#123;</span>
   <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>text <span style="color: #002200;">=</span> aData;
   <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>m_arrayNameData objectAtIndex<span style="color: #002200;">:</span> aRow<span style="color: #002200;">&#93;</span> isEqualToString<span style="color: #002200;">:</span>text<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">NO</span><span style="color: #002200;">&#41;</span>
   <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>m_arrayNameData replaceObjectAtIndex<span style="color: #002200;">:</span>aRow withObject<span style="color: #002200;">:</span>aData<span style="color: #002200;">&#93;</span>;
   <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>ก็ลองโหลด <a href="http://www.macfeteria.com/wp-content/uploads/2008/08/studenttableeditable.zip">Source Code</a> ไปลอง Compile เล่นดู</p>
<h2><span style="color: #cccc33;">More Column</span></h2>
<p>จากตัวอย่างที่เขียนไปเราได้สร้างตารางที่มีแค่ 1 column เท่านั้น ถ้าหากต้องการสร้าง หลายๆ Column แล้วเราก็เพียงแค่ไปเปลี่ยน ค่าของจำนวน column ใน interface builder แบบรูปข้างล่าง</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2008/08/inspector_column.png" rel="lightbox[282]"><img class="size-medium wp-image-291 aligncenter" title="inspector_column" src="http://www.macfeteria.com/wp-content/uploads/2008/08/inspector_column-249x300.png" alt="" width="249" height="300" /></a></p>
<p style="text-align: left;">การสร้าง interface  ก็คงไม่ยากอะไร แต่ปัญหาคือ เราจะเขียน Code ให้มันแสดง cell ที่ตำแหน่ง column ที่เราต้องการได้อย่างไร ?</p>
<p style="text-align: left;">วีธีการก็คือ เราต้อง กำหนดชื่อ ให้กับ Column ก่อน แล้วก็แก้ไข code ในส่วนของ</p>
<p style="text-align: left;">-(id) tableView:(NSTableView*)    table<br />
objectValueForTableColumn:(NSTableColumn*) tableColumn<br />
row:(int) rows</p>
<p style="text-align: left;">สมมติว่า เราต้องการแก้ไข โปรแกรม แสดงรายชื่อนักเรียน ให้มีอีก 1 Column เพื่อแสดงสาขาที่เรียนของนักเขียนด้วย หน้าตาโปรแกรม เป็นลักษณะแบบนี้</p>
<p style="text-align: left;">เราก็ทำการแก้ไข datasource ของเราให้มี array เพ่ิมขึ้นมาอีก 1 เพื่อเอาไว้เก็บ สาขาที่เรียนของนักเรียน อาจจะเขียนใหม่ได้ว่า</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> StudentDataSource <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
   <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>m_arrayNameData;
   <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>m_arrayDepartmentData;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> AddStudentRecord<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>name 
        andDepartment<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> department;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>และเราก็เขียน method เพิ่ิมเติม</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> AddRecord<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> name <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>m_textFields stringValue<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> department <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>m_textFields stringValue<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>m_dataSource AddStudentRecord<span style="color: #002200;">:</span>name andDepartment<span style="color: #002200;">:</span>department<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>m_tableView reloadData<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>และส่วนที่สำคัญที่สุดคือ tableView: objectValueForTableColumn: row: เราต้องเขียนให้มันส่ง ตำแหน่ง cell ใน column ที่ต้องการให้เป็นแบบนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSTableView</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>	table
      objectValueForTableColumn<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSTableColumn</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableColumn
      row<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> rows
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>columnName;
	columnName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableColumn identifier<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>columnName isEqualToString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;name&quot;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>m_arrayNameData objectAtIndex<span style="color: #002200;">:</span>rows<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">else</span>
		<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>m_arrayDepartMentData objectAtIndex<span style="color: #002200;">:</span>rows<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>จาก code ข้างบนเราสามารถอ้างอิงถึง column ได้โดยการใช้ column identifier เป็นการตรวจสอบว่า เป็น column ของเราที่ต้องการหรือเปล่า  อย่างตัวอย่างเราก็ได้ให้ เปรียบเทียบว่าเป็น column &#8220;name&#8221; หรือไม่ และการใส่ค่าให้กับ column identifier เราทำได้โดยการใช้ interface builder และใส่ค่าให้กับ column เหมือนรูปข้างล่าง</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2008/08/column_identifier.png" rel="lightbox[282]"><img class="size-medium wp-image-294 aligncenter" title="column_identifier" src="http://www.macfeteria.com/wp-content/uploads/2008/08/column_identifier-300x104.png" alt="" width="300" height="104" /></a></p>
<p style="text-align: left;">แล้วก็ Compile ดูครับ เพียงเท่านี้ก็เป็นอันเรียบร้อย โปรแกรมก็จะออกมาแบบนี้</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2008/08/morecolumn.png" rel="lightbox[282]"><img class="size-medium wp-image-295 aligncenter" title="morecolumn" src="http://www.macfeteria.com/wp-content/uploads/2008/08/morecolumn-300x223.png" alt="" width="300" height="223" /></a></p>
<p style="text-align: left;">สำหรับ Source ก็โหลดได้นี่เลย <a href="http://www.macfeteria.com/wp-content/uploads/2008/08/studentdepartmenttable.zip">Download Source Code</a></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/advance' rel='tag' target='_self'>advance</a>, <a class='technorati-link' href='http://technorati.com/tag/cocoa' rel='tag' target='_self'>cocoa</a>, <a class='technorati-link' href='http://technorati.com/tag/NSTableView' rel='tag' target='_self'>NSTableView</a>, <a class='technorati-link' href='http://technorati.com/tag/objective-c' rel='tag' target='_self'>objective-c</a>, <a class='technorati-link' href='http://technorati.com/tag/programming' rel='tag' target='_self'>programming</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.macfeteria.com/blog/2008/08/08/cocoa-programming-nstableview-advance/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ojective-C Programming &#8211; Copying Object</title>
		<link>http://www.macfeteria.com/blog/2008/06/22/ojective-c-programming-copying-object/</link>
		<comments>http://www.macfeteria.com/blog/2008/06/22/ojective-c-programming-copying-object/#comments</comments>
		<pubDate>Sun, 22 Jun 2008 16:53:11 +0000</pubDate>
		<dc:creator>[Ter]</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[advance]]></category>
		<category><![CDATA[NSCopying]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://maccafe.wordpress.com/?p=74</guid>
		<description><![CDATA[นี่ก็น่าจะเป็นเรื่อง objective-c แบบเพียวๆเกือบๆจะสุดท้ายละ ก็เหลืออีกไม่กี่เรื่องที่ผมคิดว่าน่าจะเพียงพอสำหรับการเริ่มต้นกับ cocoa เอาละเข้าเรื่องเลยดีกว่า โดยปกติเราเวลาที่เราประกาศตัวแปรขึ้นมาสักตัว ถ้าไม่ใช่ class ก็จะเป็น primitive type เป็นต้นว่า int , char , double การที่เราจะ copy ข้อมูลจาก ตัวแปรหนึ่งไปยังอีกตัวแปร หนึ่งนั้นทำได้ง่ายมาก เพียงแค่ใช้เครื่องหมาย = แค่นี้เองเช่นเป็นต้นว่า int a = 10; int b; b = a; a = a + 15; b = b+10; &#160; NSLog&#40;@&#34;%d %d&#34;,a,b&#41;; จาก code เบื้องต้นจะเห็นว่า บรรทัดที่ 3 เราได้ให้ b มีค่าเท่ากับ a [...]]]></description>
			<content:encoded><![CDATA[<p>นี่ก็น่าจะเป็นเรื่อง objective-c แบบเพียวๆเกือบๆจะสุดท้ายละ ก็เหลืออีกไม่กี่เรื่องที่ผมคิดว่าน่าจะเพียงพอสำหรับการเริ่มต้นกับ cocoa เอาละเข้าเรื่องเลยดีกว่า</p>
<p>โดยปกติเราเวลาที่เราประกาศตัวแปรขึ้นมาสักตัว ถ้าไม่ใช่ class ก็จะเป็น primitive type เป็นต้นว่า int , char , double การที่เราจะ copy ข้อมูลจาก ตัวแปรหนึ่งไปยังอีกตัวแปร หนึ่งนั้นทำได้ง่ายมาก เพียงแค่ใช้เครื่องหมาย = แค่นี้เองเช่นเป็นต้นว่า</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> a <span style="color: #002200;">=</span> <span style="color: #2400d9;">10</span>;
<span style="color: #a61390;">int</span> b;
b <span style="color: #002200;">=</span> a;
a <span style="color: #002200;">=</span> a <span style="color: #002200;">+</span> <span style="color: #2400d9;">15</span>;
b <span style="color: #002200;">=</span> b<span style="color: #002200;">+</span><span style="color: #2400d9;">10</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%d %d&quot;</span>,a,b<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>จาก code เบื้องต้นจะเห็นว่า</p>
<ul>
<li>บรรทัดที่ 3 เราได้ให้ b มีค่าเท่ากับ a</li>
<li>บรรทัดที่ 4 เพิ่มค่า a ด้วย 15</li>
<li>บรรทัดที่ 5 เพิ่มค่า b ด้วย 10</li>
<li>แน่นอนว่า จาก code ข้างบนนี้ ค่า a กับ b มีค่าต่างกันคือ 20 กับ 25</li>
<li>สรุปง่ายๆว่า เราได้ให้ค่า b เท่ากับ ค่า a ในบรรทัดที่ 3 แต่เมื่อเราเปลี่ยนค่า a ในบรรทัดที่ 4 ก็ไม่ได้มีผลกระทบอะไรกับค่า b</li>
<li>และในทำนองเดียวกัน ถ้าเราทำการแก้ไขค่าของ a ก็ไม่ได้มีผลอะไรกับ b</li>
</ul>
<p>แต่การประกาศ class instance นั้นถ้าเราเขียนโปรแกรมในลักษณะเหมือนๆกับ ข้างบน ดังเช่นตัวอย่างข้างล่าง</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Cocoa/Cocoa.h&gt;</span>
<span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>hello <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> alloc<span style="color: #002200;">&#93;</span> initWithCString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;Hello&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>temp;
temp <span style="color: #002200;">=</span> hello ;
<span style="color: #002200;">&#91;</span>temp appendString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; World&quot;</span><span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%s&quot;</span>, <span style="color: #002200;">&#91;</span>hello UTF8String<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> ;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%s&quot;</span>, <span style="color: #002200;">&#91;</span>temp UTF8String<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> ;
<span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>จาก code ข้างบน</p>
<ul>
<li>เราได้ประกาศ instance ของ NSMutableString มา 2 ตัวคือ hello กับ temp</li>
<li>ในบรรทัดที่ 8 และเราก็ได้ให้ ค่า temp มีค่าเท่ากับ hello นั่นก็แปลว่า temp ควรจะมีค่า เป็น Hello</li>
<li>จากนั้นในบรรทัดที่ 9 เราก็ได้เปลี่ยนแปลงค่า ของ temp โดยการต่อท้ายด้วย World</li>
<li>และบรรทัดที่ 10 เราก็ให้พิมพ์ค่าตัวแปร hello ออกมา</li>
<li>แต่ผลปรากฎว่า มันกลับพิมพ์คำว่า Hello World ออกมา ทั้งๆที่เราก็ไม่ได้ไปทำการแก้ไขค่าใน ตัวแปร hello เลยสักนิด</li>
</ul>
<p>ทำไมถึงเป็นแบบนี้ ?</p>
<p>เพราะว่า การประกาศ class instance นั้นเป็นการประกาศ pointer ( คือตัวแปรที่เก็บค่าของตำแหน่ง memory ) ของ class นั้นๆ ฉนั้นการที่เขียน code ว่า</p>
<p>temp = helloString</p>
<p>ก็เป็นการบอกว่า ให้ temp นั้นชี้ไปยังตำแหน่ง memory เดียวกันกับ hello ฉนั้นแล้วเมื่อมันเป็น ตำแหน่ง memory เดียวกัน ถ้าหากเราทำการแก้ไข temp จึงทำให้ hello ต้องเปลี่ยนไปด้วย</p>
<p>เหมือนในภาษา c/c++ถ้าดู code ต่อไปนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">char</span> hello<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">64</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;Hello World&quot;</span>;
<span style="color: #a61390;">char</span> temp<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">64</span><span style="color: #002200;">&#93;</span>;
temp <span style="color: #002200;">=</span> hello;
<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;%s&quot;</span>, temp<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>การที่เราเขียน code แบบนี้เป็นการให้ temp ชี้ไปที่ตำแหน่ง memory ของ hello ไม่ได้เป็นการ copy ค่า ( เราต้องการให้ temp มีค่าเป็นคำว่า Hello World!! ) ฉนั้นแล้วเมื่อเราสั่ง printf ตัวแปร temp ออกมาก็จะไม่ได้คำว่า Hello World!! เพราะเป็นการชี้ตำแหน่ง memory ไปยังตำแหน่งใหม่ ถ้าหากเราจะ ให้ค่า temp เป็น Hello World เหมือนกับ hello เราต้องใช้ ฟังชั่นที่ชื่อว่า strcpy ทำการ copy string ถ้าเป็นภาษา c/c++ อาจจะเขียนได้แบบนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">char</span> hello<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">64</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;Hello World&quot;</span>;
<span style="color: #a61390;">char</span> temp<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">64</span><span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">strcpy</span><span style="color: #002200;">&#40;</span>temp, hello<span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;%s&quot;</span>, temp<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>ปล. ใครที่ยังไม่เข้าใจเรื่อง pointer ให้ไปอ่านเพิ่มเติมน่ะครับ เดี๋ยวจะงง<br />
<a href="http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=1&amp;pageid=7">http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=1&amp;pageid=7</a></p>
<p>แล้วเราจะ copy มันได้ยังไง ?</p>
<p>ใน Foundation class นั้นจะมี method ที่ชื่อว่า copy และ mutableCopy เพื่อให้เราสามารถ clone ค่าจาก instance หนึ่งไปยังอีก instance หนึ่งได้ และสอง method นี้ต่างกันกันตรงที่ว่า ถ้าเป็นหากเราต้องการจะคัดลอกไปแล้วให้มันแก้ไขได้ ก็ต้องใช้ mutableCopy ( สำหรับ mutable object นั้นก็ควรจะใช้ mutableCopy )</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Cocoa/Cocoa.h&gt;</span>
<span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>helloString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> alloc<span style="color: #002200;">&#93;</span> initWithCString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;Hello&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>temp;
&nbsp;
	<span style="color: #002200;">&#91;</span>temp mutableCopy<span style="color: #002200;">:</span>helloString<span style="color: #002200;">&#93;</span> ;
	<span style="color: #002200;">&#91;</span>temp appendString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; World&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%s&quot;</span>, <span style="color: #002200;">&#91;</span>helloString UTF8String<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> ;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%s&quot;</span>, <span style="color: #002200;">&#91;</span>temp UTF8String<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> ;
&nbsp;
	<span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>มาดูกันว่าจากข้างบนเราจะเปลี่ยนให้ temp มันเท่ากับ helloString ได้ยังไง</p>
<p>ถ้าเรา compile และ run ดูจะเห็นว่าผลลัพธ์ที่ได้จะออกมาเป็น</p>
<p>Hello<br />
HelloWorld</p>
<p>ซึ่งจะต่างจากโปรแกรมแรกที่เราเขียน เพราะว่า เราทำการ copy ค่าจาก helloSting มาให้ยัง temp ( การ copy ในที่นี้หมายถึงการคัดลอกข้อมูลในตำแหน่ง memory ที่ต้องการไปยังอีกที่) นั่นก็แปลว่าค่าของตัวแปรทั้งสองอันนี้แยกจากกัน ฉนั้นถ้าเราแก้ไขตัวแปร temp ก็ไม่ได้เกี่ยวอะไรกับ helloString</p>
<h2><span style="color: #c1baa7;">Shallow &#8211; Deep</span></h2>
<p>อย่างที่บอกไปแล้วว่า instance นั้นคือ pointer ดีๆนี่เอง ลองพิจารณา code ข้างล่างนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Cocoa/Cocoa.h&gt;</span>
<span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
<span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> pool      <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>helloString  <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> alloc<span style="color: #002200;">&#93;</span> initWithCString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;Hello&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>arrayOne <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>arrayTwo;
&nbsp;
<span style="color: #002200;">&#91;</span>arrayOne addObject<span style="color: #002200;">:</span>helloString<span style="color: #002200;">&#93;</span>;
&nbsp;
arrayTwo <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>arrayOne mutableCopy<span style="color: #002200;">&#93;</span> ;
&nbsp;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>arrayTwo objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> appendString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; World&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%s&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>arrayOne objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> UTF8String<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> ;
<span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>จากโปรแกรมข้างบน</p>
<ul> เราเริ่มด้วยการประกาศตัวแปรมาทั้ง 3 ตัวคือ arrayOne arrayTwo และ helloString </p>
<li>หลังจากนั้น เราก็ได้เอาตัวแปร helloString ไปเก็บไว้ที่ arrayOne </li>
<li>จากนั้นก็ arrayTwo ก็ได้ mutableCopy จาก arrayOne </li>
<li>สรุปว่า ตอนนี้ arrayOne มี member 1 ตัวคือ helloString ส่วน arrayTwo ก็มี member 1 ตัวเหมือนกันเพราะว่าไปก๊อปจาก arrayOne มา</li>
<li>หลังจากนั้นเราได้ทำการแก้ไข ค่าของ member ใน arrayTwo โดยเพิ่มคำว่า World เข้าไป</li>
<li>สุดท้ายเราพิมพ์ค่าของ arrayOne ออกมาร แต่ผลที่ได้คือ มันพิมพ์คำว่า Hello World !!!!</li>
<p>อ้าวทำไมมันเป็นแบบนี้ละ ทั้งๆที่เราแก้ไขตัวแปร ใน arrayTwo แล้วมันเกี่ยวอะไรกับ arrayOne ?</p>
<p>จริงอยู่ครับว่า arrayTwo นั้นก๊อปปี้ arrayOne มา นั่นก็แปรได้ว่ามันใช้ memory คนละส่วนกันแล้วดังนั้นมันก็ควรจะไม่เกี่ยวกัน ก็ถูกต้องแล้วละครับ ว่ามันไม่ได้เกียวกันการแก้ไข member ใน arrayOne ก็ไม่ได้เกียวกับ arrayTwo</p>
<p>แต่เผอิญว่า ตัวแปร ที่ arrayOne นั้นเก็บไว้มันเป็น pointer ที่ชี้ไปยัง helloString นั่นก็แปลได้ง่ายๆว่า member ใน array นั้นไม่ได้ถูก clone มาด้วย เป็นแค่การ copy reference มาแค่นั้น ฉนั้นการแก้ไข ตัวแปรใน arrayOne ย่อมมีผลกระทบต่อ arrayTwo สิ่งที่มันเป็นลักษณะ นี้เรียกว่า shallow copy</p>
<p>แล้วจะทำยังไงให้มันแยกขาดจากกันเลย ?</p>
<p>คำตอบคือ เราต้อง ทำเองครับ เพราะว่ามันไม่ได้ทำให้เรา แต่จะทำยังไงละ ?</p>
<h2>Copying Class Instance</h2>
<p>จริงอยู่ว่าเราสามารถใช้ copy , mutableCopy ได้แต่ถ้าหากเป็น class ที่เราเขียนขึ้นมาเองนั้นไม่สามารถทำได้ เราต้องเขียนเองเหมือนกัน</p>
<p>สรุปว่าตอนนี้ก็มีตัวอย่างที่เราต้องทำเอง ถึง 2 เคสด้วยกัน วิธีการก็ไม่ได้ยากเย็นอะไรนัก เราเพียงแค่ implement interface &lt;NSCopy&gt; หรือ &lt;NSMutableCopy&gt; เท่านั้นเอง มาดูกันเลยดีกว่า ว่ามันหน้าตาเป็นยังไง</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import  &lt;Cocoa/Cocoa.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> MyClass <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> &lt;NSCopying&gt;
<span style="color: #002200;">&#123;</span>
 <span style="color: #a61390;">int</span> m_number;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> SetValue<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> num;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> GetValue;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> MyClass
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> SetValue<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> num
<span style="color: #002200;">&#123;</span>
 m_number <span style="color: #002200;">=</span> num;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> GetValue
<span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">return</span> m_number;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> copyWithZone<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSZone</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> zone
<span style="color: #002200;">&#123;</span>
 MyClass <span style="color: #002200;">*</span>temp <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyClass allocWithZone<span style="color: #002200;">:</span>zone<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
 <span style="color: #002200;">&#91;</span>temp SetValue<span style="color: #002200;">:</span> m_number<span style="color: #002200;">&#93;</span>;
 <span style="color: #a61390;">return</span> temp;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">int</span> main <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
MyClass <span style="color: #002200;">*</span>myclass_A <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyClass alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
MyClass <span style="color: #002200;">*</span>myclass_B;
<span style="color: #002200;">&#91;</span>myclass_A SetValue<span style="color: #002200;">:</span><span style="color: #2400d9;">9</span><span style="color: #002200;">&#93;</span>;
&nbsp;
myclass_B <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>myclass_A copy<span style="color: #002200;">&#93;</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%d&quot;</span>,<span style="color: #002200;">&#91;</span>myclass_B GetValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>ก็ดูจาก code ข้างบนจะเห็นว่า ตอนที่ประกาศ class จะมีส่วนเพิ่มเติมเข้ามาคือ &lt;NSCopying&gt; เมื่อเรามี interface NSCopying แล้วสิ่งที่ต้องทำต่อมาก็คือ implement ส่วนของ method ที่มีชื่อว่า -(<span>id</span>) copyWithZone: (<span>NSZone</span>*) zone แล้วภายใน method ก็ประกาศตัวแปร และก็ copy ค่าต่างๆมาให้ยัง object ใหม่ที่ทำการประกาศ เพียงเท่านี้ก็เสร็จ แล้วครับ</p>
<p>ลอง compile และ run ดูจะเห็นว่า myclass_B นั้นมีค่าเหมือนกับ myclass_A ทุกอย่าง</p>
<p>งงตรงไหน ติชม คอมเม้นกันมาได้นะครับ</p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/advance' rel='tag' target='_self'>advance</a>, <a class='technorati-link' href='http://technorati.com/tag/NSCopying' rel='tag' target='_self'>NSCopying</a>, <a class='technorati-link' href='http://technorati.com/tag/objective-c' rel='tag' target='_self'>objective-c</a>, <a class='technorati-link' href='http://technorati.com/tag/programming' rel='tag' target='_self'>programming</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.macfeteria.com/blog/2008/06/22/ojective-c-programming-copying-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ojective-C Programming &#8211; Thread II</title>
		<link>http://www.macfeteria.com/blog/2008/06/16/ojective-c-programming-thread-ii/</link>
		<comments>http://www.macfeteria.com/blog/2008/06/16/ojective-c-programming-thread-ii/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 13:44:41 +0000</pubDate>
		<dc:creator>[Ter]</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[advance]]></category>
		<category><![CDATA[NSLock]]></category>
		<category><![CDATA[NSThread]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Synchronized]]></category>

		<guid isPermaLink="false">http://maccafe.wordpress.com/?p=71</guid>
		<description><![CDATA[หลังจากเขียน Thread พอเป็นแล้วก็ยังมีเรื่องเกี่ยวกับ Thread ที่ต้องรู้อีก อยู่อย่างหนึ่งก็คือ การ Synchronizes ระหว่าง Thread อย่างที่บอกไปแล้ว ว่าการเขียนโปรแกรมโดยการใช้ Thread นั้นมีข้อดีมากมาย แต่ก็มีข้อเสียเหมือนกัน และหนึ่งในข้อเสียนั้นก็คือการเขียนโปรแกรมแบบที่มีหลายๆ thread นั้นทำให้โปรแกรมของเราซับซ้อนมากขึ้น และลำบากต่อการจัดการข้อมูลที่มีการใช้งานร่วมกันระหว่าง thread เช่นเป็นต้นว่า ถ้าโปรแกรมที่เราเขียนขึ้นมีหลายๆ Thread โดยแต่ละ Thread นั้นมีการแชร์ข้อมูลร่วมกัน ถ้าหากเราไม่ได้เขียนโปรแกรมให้มันมีการควบคุมการทำงานของแต่ละ Thread หรือเขียนได้ไม่ดี อาจจะเกิดเหตการณ์ในลักษณะแบบนี้ สมมติว่า โปรแกรม ของเรามี 2 thread ที่มีการใช้ตัวแปรร่วมกันคือ Money และแต่ละ thread นั้นก็จะทำการอ่านค่า Money และเขียนผลลัพธ์ที่ได้จากการคำนวนใหม่ลงไป Thread A : อ่านตัวแปร Money ได้มีค่าเป็น 10$ และระหว่างการคำนวนนี้ ใช้เวลาประมาณ 10 นาโนวินาที และระหว่างที่ A [...]]]></description>
			<content:encoded><![CDATA[<p>หลังจากเขียน Thread พอเป็นแล้วก็ยังมีเรื่องเกี่ยวกับ Thread ที่ต้องรู้อีก อยู่อย่างหนึ่งก็คือ การ Synchronizes ระหว่าง Thread</p>
<p>อย่างที่บอกไปแล้ว ว่าการเขียนโปรแกรมโดยการใช้ Thread นั้นมีข้อดีมากมาย แต่ก็มีข้อเสียเหมือนกัน และหนึ่งในข้อเสียนั้นก็คือการเขียนโปรแกรมแบบที่มีหลายๆ thread นั้นทำให้โปรแกรมของเราซับซ้อนมากขึ้น และลำบากต่อการจัดการข้อมูลที่มีการใช้งานร่วมกันระหว่าง thread เช่นเป็นต้นว่า</p>
<p>ถ้าโปรแกรมที่เราเขียนขึ้นมีหลายๆ Thread โดยแต่ละ Thread นั้นมีการแชร์ข้อมูลร่วมกัน ถ้าหากเราไม่ได้เขียนโปรแกรมให้มันมีการควบคุมการทำงานของแต่ละ Thread หรือเขียนได้ไม่ดี อาจจะเกิดเหตการณ์ในลักษณะแบบนี้</p>
<p>สมมติว่า โปรแกรม ของเรามี 2 thread ที่มีการใช้ตัวแปรร่วมกันคือ Money และแต่ละ thread นั้นก็จะทำการอ่านค่า Money และเขียนผลลัพธ์ที่ได้จากการคำนวนใหม่ลงไป</p>
<ol>
<li>Thread A : อ่านตัวแปร Money ได้มีค่าเป็น 10$ และระหว่างการคำนวนนี้ ใช้เวลาประมาณ 10 นาโนวินาที</li>
<li>และระหว่างที่ A กำลังคำนวนอยู่นั้น Thread B ก็ได้อ่านค่า Money เหมือนกัน ได้ค่า 10$ เหมือนกัน แต่ใช้เวลาในการคำนวนเพียงแค่ 5 นาโนวินาที</li>
<li>เนื่องจาก ว่า B นั้นทำงานเสร็จก่อน A ถึง 5 นาโนวินาที. Thread B  จึงทำการ เขียนค่า Money เข้าไปใหม่เป็น 30$</li>
<li>เมื่อ A ทำงานเสร็จ A ก็เลยเขียนค่า Money ที่ได้จากการคำนวนใหม่เป็น 15$</li>
</ol>
<p>**** จะเห็นว่าค่าผลลัพธ์ที่ได้นั้น มันเกิดการ เขียนทับกันขึ้นมา ลองจินตนาการว่า สมมติว่า มี คนสองคนทำการฝากเงิน ในเวลาพร้อมๆกัน เข้าบัญชีเดียวกัน ขณะที่นาย A กำลังฝากเงินต้องใช้เวลาในการประมวลเสร็จใช้เวลา 10 วินาทีและในระหว่างนี้ นาย B ก็ฝากเงินมาเหมือนกัน แต่ใช้เวลา 5 วินาที ถ้าหากเราไม่มีการจัดการตรงนี้ นั่นก็แปรว่า เมื่อข้อมูลในบัญชี update จากการฝากเงินของ B ก็จะถูกเขียนทับด้วย ข้อมูลใหม่ที่นาย A ได้ฝากเข้ามา นันก็แปลว่า ข้อมูลเงินฝากนั้นผิดพลาด <span style="text-decoration: underline;">สิ่งที่ควรจะเป็นก็คือ ต้องรอให้ A ทำงาน เสร็จก่อน แล้ว B จึงอ่านข้อมูล</span> ****</p>
<p>ลองมาเขียนโปรแกรมสักโปรแกรม โดยโปรแกรมนี้มี 2 Thread โดยที่<br />
thread เป็นการเพ่ิมจำนวน member ใน array, ส่วนอีก thread นั้นทำหน้าที่ดึงข้อมูลจาก array แล้วทำการเปลี่ยนแปลงค่า</p>
<p>ลองดูโปรแกรม ข้างล่างนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span>
<span style="color: #11740a; font-style: italic;">// MyArray Class</span>
<span style="color: #a61390;">@interface</span> MyArray <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSMutableArray</span><span style="color: #002200;">*</span> m_array;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> init;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> dealloc;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> AddMember<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> text;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> AddMoreText<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> text;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> PrintAllMember;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> MyArray
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> init
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
    m_array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> dealloc
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>m_array release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> PrintAllMember
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">int</span> total <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>m_array count<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span> ; i &lt; total ; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;%d %s<span style="color: #2400d9;">\n</span>&quot;</span>,i,<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>m_array objectAtIndex<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span> UTF8String<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Add Member Thread Function</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> AddMember<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> text
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span> ; i &lt; <span style="color: #2400d9;">100</span> ; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>m_array addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> alloc<span style="color: #002200;">&#93;</span>initWithString<span style="color: #002200;">:</span>text<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Add Member Thread Function</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> AddMoreText<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> text
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span> ; i &lt; <span style="color: #002200;">&#91;</span>m_array count<span style="color: #002200;">&#93;</span> ; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>m_array objectAtIndex<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span> appendString<span style="color: #002200;">:</span>text<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// MAIN PROGRAM</span>
<span style="color: #a61390;">int</span> main <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    MyArray <span style="color: #002200;">*</span>myarray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyArray alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> detachNewThreadSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>AddMember<span style="color: #002200;">:</span> <span style="color: #002200;">&#41;</span>
                  toTarget<span style="color: #002200;">:</span>myarray  withObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hello&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> detachNewThreadSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>AddMoreText<span style="color: #002200;">:</span> <span style="color: #002200;">&#41;</span>
                  toTarget<span style="color: #002200;">:</span>myarray  withObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; World!!&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> sleepForTimeInterval<span style="color: #002200;">:</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>myarray PrintAllMember<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>และจากการ Compile &amp; Run โปรแกรม ผลลัพธ์ที่ได้จะมีเป็นประมาณนี้</p>
<p><strong>0 Hello World!!<br />
 1 hello World!!<br />
 .<br />
 .<br />
 8 Hello World!!<br />
 9 Hello World!!<br />
 10 Hello World!! World!!<br />
 11 Hello World!! World!!<br />
 12 Hello World!! World!!<br />
 13 Hello World!! World!!<br />
 14 Hello World!! World!!<br />
 15 Hello World!!<br />
 16 Hello World!!<br />
 17 Hello World!!<br />
 18 Hello<br />
 .<br />
 .<br />
 98 Hello<br />
 99 Hello</strong></p>
<p>จากผลลัพธ์จะเห็นว่า เนื่องจาก Thread ทั้งสองคือ thread ที่เรียกใช้งาน AddMember ที่ทำหน้าที่เพิ่มจำนวน member ใน m_array  และ อีก thread ทำหน้าที่ AddMoreText ทำหน้าที่เรียกใช้ ตัวแปร m_array และแก้ไขค่า ในแต่ละ member ของ m_array ซึ่งสอง thread ทำงานแยกจากกันและเริ่มทำงานพร้อมกัน</p>
<p>ดังนั้น ในขณะที่ Thread ทั้งสองทำงานและมีการใช้งานข้อมูลร่วมกัน โดยไม่มีการ synchronize ข้อมูลระหว่างกันว่า m_array ว่าตอนนี้ข้อมูลเป็นอย่างไร จึงเป็นเหตุทำให้โปรแกรมทำงานผิดพลาดได้ ( มันควรจะเป็น Hello World!! ทั้งหมด ไม่ใช่ Hello World!! World!! หรือ Hello อย่างเดียว )</p>
<p>วิธีการแก้ปัญหา หรือป้องกัน สิ่งที่เกิดขึ้น มักนิยมใช้ คือ การ Lock ตัวแปร หรือ code ให้ทำงานได้ทีละ Thread เพื่อกันไม่ให้ Thread อื่นๆเข้าใช้จนกว่าจะใช้งานเสร็จ</p>
<h2>NSLock</h2>
<p>เราจะใช้ class ที่ชื่อ NSLock เพื่อจะเข้ามาช่วยในการจัดการ การทำงานของแต่ละ thread โดยมันจะทำหน้าที่ lock code ในส่วนที่ต้องการให้สามารถเข้าทำงานได้ทีละ Thread ไว้ ส่วน thread อื่นๆ ก็ต้องทำการรอ จนกว่า จะมีการ unlock เกิดขึ้น</p>
<p>การใช้งาน NSLock นั้น ก็เพียงแค่ประกาศ NSLock ขึ้นมา และเรียกใช้ lock หรือ unlock เท่านั้นเอง</p>
<p>จากตัวอย่าง code ข้างบน ก็แก้ไข ส่วนของ interface โดยการเพิ่มตัวแปร NSLock และในส่วนของ method ก็ทำการ เรียกใช้ lock และ unlock</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyArray <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSLock</span> <span style="color: #002200;">*</span>m_lock;
    <span style="color: #400080;">NSMutableArray</span><span style="color: #002200;">*</span> m_array;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>ส่วน Method ก็ทำการแก้ไขได้เป็น</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> AddMember<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> text
<span style="color: #002200;">&#123;</span>
     <span style="color: #002200;">&#91;</span>m_lock<span style="color: #002200;">&#93;</span>;
     <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span> ; i &lt; <span style="color: #2400d9;">100</span> ; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span>
     <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>m_array addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> alloc<span style="color: #002200;">&#93;</span>initWithString<span style="color: #002200;">:</span>text<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
     <span style="color: #002200;">&#125;</span>
     <span style="color: #002200;">&#91;</span>m_unlock<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> AddMoreText<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> text
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>m_lock<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span> ; i &lt; <span style="color: #002200;">&#91;</span>m_array count<span style="color: #002200;">&#93;</span> ; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>m_array objectAtIndex<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span> appendString<span style="color: #002200;">:</span>text<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#91;</span>m_unlock<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>เพียงเท่านี้ก็แก้ปัญหาได้แล้ว สำหรับผลลัพธ์ ที่่ออกมาก็คือ</p>
<p><strong>0 Hello World!!<br />
1 Hello World!!<br />
.<br />
.<br />
98 Hello World!!<br />
99 Hello World!!</strong></p>
<p>จะเห็นว่า จะไม่เกิดการซ้อนทับ เหมือนกับ ตัวอย่างแรก</p>
<h2>@synchronized</h2>
<p>ยังมีอีกวิธี ที่สามารถทำการ lock ในส่วนที่ต้องการได้ ถ้าไม่ต้องการใช้ NSLock เราเพียงแค่เขียน code ที่ต้องการจะ lock ไว้ภายในส่วนของ @synchronized { } ก็ทำได้เหมือนกัน</p>
<p>เป็นต้นว่า</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> AddMoreText<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> text
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">@synchronized</span><span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
         <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span> ; i &lt; <span style="color: #002200;">&#91;</span>m_array count<span style="color: #002200;">&#93;</span> ; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span>
         <span style="color: #002200;">&#123;</span>
             <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>m_array objectAtIndex<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span> appendString<span style="color: #002200;">:</span>text<span style="color: #002200;">&#93;</span>;
         <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>ก็ติดปัญหาอะไร ถามได้น่ะครับ</p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/advance' rel='tag' target='_self'>advance</a>, <a class='technorati-link' href='http://technorati.com/tag/NSLock' rel='tag' target='_self'>NSLock</a>, <a class='technorati-link' href='http://technorati.com/tag/NSThread' rel='tag' target='_self'>NSThread</a>, <a class='technorati-link' href='http://technorati.com/tag/objective-c' rel='tag' target='_self'>objective-c</a>, <a class='technorati-link' href='http://technorati.com/tag/programming' rel='tag' target='_self'>programming</a>, <a class='technorati-link' href='http://technorati.com/tag/Synchronized' rel='tag' target='_self'>Synchronized</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.macfeteria.com/blog/2008/06/16/ojective-c-programming-thread-ii/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Ojective-C Programming &#8211; Thread</title>
		<link>http://www.macfeteria.com/blog/2008/06/09/ojective-c-programming-thread/</link>
		<comments>http://www.macfeteria.com/blog/2008/06/09/ojective-c-programming-thread/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 19:31:10 +0000</pubDate>
		<dc:creator>[Ter]</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[advance]]></category>
		<category><![CDATA[foundation]]></category>
		<category><![CDATA[NSThread]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://maccafe.wordpress.com/?p=68</guid>
		<description><![CDATA[ก็ยังคงต่อเนื่องด้วย Objective-C เหมือนเดิม ว่าจะขึ้น cocoa มา 3 รอบแล้วก็ยังไม่ได้ขึ้นสักที บอกตั้งแต่ iPhone SDK เพิ่งจะออกจนตอนนี้มันเป็น Beta 6 ไปละ หวังไม่โกรธกันนะครับ เอาละวันนี้เข้าเรื่องเลยดีกว่า Thread คืออะไร ?  ก็อธิบายได้ง่ายๆว่า มันคือส่วนย่อยๆของ Process หรือชุดคำสั่งนั่นเหละ โดยทำงานแยกจากกัน โดยปกติแล้วโปรแกรมที่เขียนขึ้นมาง่ายๆมักจะเป็นลักษณะ 1 thread หรืออาจจะเรียกได้ว่า &#8220;Single Thread&#8221; ถ้ามีหลายๆ thread ก็เรียกว่า &#8220;Multithread&#8221; สำหรับโปรแกรมใหญ่ๆแล้วจะมีการใช้ thread มากกว่า 1 การใช้งาน thread มีข้อดีหลายอย่างตั้งแต่ การใช้ทรัพยาการร่วมกัน การสนับสนุนการทำงานของ multiprocessor และอื่นๆอีกมากมาย สำหรับใครที่ไม่เคยรู้เรื่อง thread นั้นแนะนำให้ไปอ่านเพิ่มเติม http://www.thaiall.com/os/os04.html http://en.wikipedia.org/wiki/Thread_(computer_science) เมื่อพอเข้าใจเบื้องต้นแล้ว เข้าสู้เนื้อหาของเราเลยดีกว่า ว่าเราจะสร้าง Thread [...]]]></description>
			<content:encoded><![CDATA[<p>ก็ยังคงต่อเนื่องด้วย Objective-C เหมือนเดิม ว่าจะขึ้น cocoa มา 3 รอบแล้วก็ยังไม่ได้ขึ้นสักที บอกตั้งแต่ iPhone SDK เพิ่งจะออกจนตอนนี้มันเป็น Beta 6 ไปละ หวังไม่โกรธกันนะครับ เอาละวันนี้เข้าเรื่องเลยดีกว่า</p>
<h2>Thread</h2>
<p>คืออะไร ?  ก็อธิบายได้ง่ายๆว่า มันคือส่วนย่อยๆของ Process หรือชุดคำสั่งนั่นเหละ โดยทำงานแยกจากกัน โดยปกติแล้วโปรแกรมที่เขียนขึ้นมาง่ายๆมักจะเป็นลักษณะ 1 thread หรืออาจจะเรียกได้ว่า &#8220;Single Thread&#8221; ถ้ามีหลายๆ thread ก็เรียกว่า &#8220;Multithread&#8221; สำหรับโปรแกรมใหญ่ๆแล้วจะมีการใช้ thread มากกว่า 1</p>
<p>การใช้งาน thread มีข้อดีหลายอย่างตั้งแต่ การใช้ทรัพยาการร่วมกัน การสนับสนุนการทำงานของ multiprocessor และอื่นๆอีกมากมาย สำหรับใครที่ไม่เคยรู้เรื่อง thread นั้นแนะนำให้ไปอ่านเพิ่มเติม</p>
<p><a href="http://www.thaiall.com/os/os04.htm">http://www.thaiall.com/os/os04.html</a></p>
<p><a href="http://www.thaiall.com/os/os04.htm"> </a></p>
<p><a href="http://en.wikipedia.org/wiki/Thread_(computer_science)">http://en.wikipedia.org/wiki/Thread_(computer_science)</a></p>
<p><a href="http://en.wikipedia.org/wiki/Thread_(computer_science)"></a></p>
<p>เมื่อพอเข้าใจเบื้องต้นแล้ว เข้าสู้เนื้อหาของเราเลยดีกว่า ว่าเราจะสร้าง Thread และใช้งานมันได้อย่างไร</p>
<h2>NSThread</h2>
<p>ใน objective-c นั้นสามารถทำได้ตั้งแต่การเรียกใช้ thread ของ ภาษา c เช่น pthread แต่ไหนๆเราจะเขียน objective-c กันแล้ว จะไปใช้ pthread กันทำไม เพราะใน objective-c เองนั้นก็มี class ที่อำนวยความสะดวกในการสร้าง thread อยู่แล้วนั่นคือ NSThread</p>
<p>การสร้าง thread นั้นทำได้ อยู่ 2 วิธีคือ</p>
<ul>
<li>เรียกใช้ class method ที่มีชื่อว่า detachNewThreadSelector:toTarget:withObject:</li>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> detachNewThreadSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>sampleMethod<span style="color: #002200;">:</span> <span style="color: #002200;">&#41;</span> 
                 toTarget<span style="color: #002200;">:</span>self withObject<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

</ul>
<p>การสร้าง thread ด้วย detachNewThreadSelector แบบนี้เราไม่จำเป็นต้องประกาศ ตัวแปรอะไรเลย จะมีก็แต่ parameter ที่ต้องใส่เข้าไป มาดูการประกาศ thread อีกแบบกันเลย</p>
<ul>
<li>สร้าง NSThread ขึ้นมาแล้วเรียก start

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSThread</span><span style="color: #002200;">*</span> myThread <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>self
        selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>myThreadMainMethod<span style="color: #002200;">:</span> <span style="color: #002200;">&#41;</span>
        object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>myThread start<span style="color: #002200;">&#93;</span>;</pre></div></div>

</li>
</ul>
<p>ส่วนแบบวิธีนี้ต่างจากอันแรกคือ เราสร้างตัวแปร NSThread ขึ้นมาแล้วก็ใส่ค่า parameter ให้กับมัน หลังจากนั้นก็เรียก start สำหรับการสร้างแบบนี้มีข้อดี ต่างจากอย่างแรกก็คือ เราสร้างไว้ก่อนแล้วค่อยเรียกให้ thread start ทีหลังได้</p>
<p>ก็หลังจากสร้างเป็นแล้ว มาดูการใช้งาน กันเลยดีกว่า</p>
<p>สมมติว่า เราจะเขียนโปรแกรม ที่เอาไว้เขียน console แบบง่ายๆกัน แต่เป็นแบบ หลายๆ thread</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&nbsp;
<span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//--------------------------------------</span>
<span style="color: #11740a; font-style: italic;">// Console Class</span>
<span style="color: #11740a; font-style: italic;">//--------------------------------------</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #a61390;">@interface</span> Console <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span> <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> PrintToConsole <span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> text;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> Console
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> PrintToConsole <span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> text;
<span style="color: #002200;">&#123;</span>  
   <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span> ; i &lt; <span style="color: #2400d9;">5</span> ; i<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>  
   <span style="color: #002200;">&#123;</span>   
        <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;%d: &quot;</span>,i<span style="color: #002200;">&#41;</span>; 
        NSLog<span style="color: #002200;">&#40;</span>text<span style="color: #002200;">&#41;</span>;  
   <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span> 
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//--------------------------------------</span>
<span style="color: #11740a; font-style: italic;">// MAIN PROGRAM</span>
<span style="color: #11740a; font-style: italic;">//--------------------------------------</span>
&nbsp;
<span style="color: #a61390;">int</span> main <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
<span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
Console <span style="color: #002200;">*</span>sampleConsole <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Console alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
Console <span style="color: #002200;">*</span>helloConsole <span style="color: #002200;">=</span>  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Console alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
Console <span style="color: #002200;">*</span>okConsole <span style="color: #002200;">=</span>     <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Console alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSThread</span> <span style="color: #002200;">*</span>helloThread <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>helloConsole 
                        selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>PrintToConsole<span style="color: #002200;">:</span> <span style="color: #002200;">&#41;</span> 
                        object<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hello&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> detachNewThreadSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>PrintToConsole<span style="color: #002200;">:</span> <span style="color: #002200;">&#41;</span>  
                                  toTarget<span style="color: #002200;">:</span>sampleConsole 
                                  withObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sample&quot;</span><span style="color: #002200;">&#93;</span>;     
&nbsp;
<span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> detachNewThreadSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>PrintToConsole<span style="color: #002200;">:</span> <span style="color: #002200;">&#41;</span>  
                                  toTarget<span style="color: #002200;">:</span>okConsole 
                                  withObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Ok&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;<span style="color: #2400d9;">\n</span>Hello, World From Main thread!<span style="color: #2400d9;">\n</span>&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#91;</span>helloThread start<span style="color: #002200;">&#93;</span>;  
&nbsp;
<span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> sleepForTimeInterval<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;           
<span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>จากโปรแกรม ข้างบน อธิบายคร่าวๆนะครับว่า เราประกาศ  class ชื่อ Console ขึ้นมาโดยที่มี method  เพียงแค่ PrintToConsole โดยการทำงานของ method นี้ก็คือให้พิมพ์ข้อความทั้งหมด 5 ครั้ง</p>
<p>หลังจากนั้นก็ทำการประกาศ ตัวแปร Console ขึ้นมาอีก 3 โดยแต่ละ object นั้นก็ทำการพิมพ์ข้อความออกมาไม่เหมือนกันนั่นก็คือ Hello , Sample , Ok</p>
<p>ลำดับต่อไปก็ทำการ สร้าง Thread ขึ้นมา โดยจากตัวอย่าง ผมได้ทำการใช้วิธีการประกาศทั้ง 2 แบบคือทั้งแบบ ประกาศ ตัวแปร NSThread ขึ้นมาก่อน แล้วค่อยเรียก Start และอีกแบบก็คือไม่ต้องประกาศ ตัวแปร แต่เรียกใช้ class method ขึ้นมาเลย</p>
<p>ยังมีอีก method ที่น่าสนใจก็คือ sleepForTimeInterval โดยทำหน้าที่คือ หยุดการทำงานตามเวลาที่กำหนด สาเหตุก็เพราะว่า เนื่องจากว่า Thread นั้นทำงานแยกกัน อาจจะมีบาง Thread  ที่ทำงานเสร็จก่อน หรือเสร็จหลัง Main Program ก็ได้ ฉนั้นจึงทำการรอให้ แต่ละ Thread ทำงานเสร็จก่อน แล้วค่อยจบโปรแกรม เพื่อที่ว่าป้องกันปัญหา Memory Leak หรือโปรแกรมทำงานผิดพลาด</p>
<p>และหลังจาก compile และ run แล้วผลลัพธ์ที่ได้ จะได้ประมาณแบบนี้</p>
<pre>0: 0: 2008-06-09 00:52:54.252 Thread[724:1103] Sample
1: 2008-06-09 00:52:54.253 Thread[724:1203] Ok
1: 2008-06-09 00:52:54.252 Thread[724:10b]
Hello, World From Main thread!
0: 2008-06-09 00:52:54.258 Thread[724:1103] Sample
2: 2008-06-09 00:52:54.258 Thread[724:1203] Ok
2: 2008-06-09 00:52:54.259 Thread[724:2c03] Hello
1: 2008-06-09 00:52:54.260 Thread[724:1103] Sample
3: 2008-06-09 00:52:54.260 Thread[724:1203] Ok
3: 2008-06-09 00:52:54.261 Thread[724:2c03] Hello
2: 2008-06-09 00:52:54.262 Thread[724:1103] Sample
4: 2008-06-09 00:52:54.262 Thread[724:1203] Ok
4: 2008-06-09 00:52:54.263 Thread[724:2c03] Hello
3: 2008-06-09 00:52:54.264 Thread[724:1103] Sample
2008-06-09 00:52:54.265 Thread[724:1203] Ok
2008-06-09 00:52:54.265 Thread[724:2c03] Hello
4: 2008-06-09 00:52:54.267 Thread[724:2c03] Hello</pre>
<p>จะเห็นว่า ลำดับ แต่อันจะไม่เรียงกัน อาจจะสลับกันไปมา ก็เนื่องจากว่า Thread แต่ละอันนั้นทำงานแยกจากกัน</p>
<p><strong><span style="color: #8f7a2f;">*** ข้อระวังสำหรับการใช้ Thread ใน Objective-C ***</span></strong><br />
<strong><span style="color: #8f7a2f;">ใน Thread ถ้ามีการประกาศตัวแปร หรือว่าใช้งานในลักษณะที่ต้องมีการจอง memory ต้องมี NSAutoreleasePool ด้วยเสมอ </span></strong></p>
<p>เช่น</p>
<p>- (void) PrintToConsole : NSString* text  ถ้าเราจะเปลี่ยน code การทำงานให้มันพิมพ์ออกที่หน้า console เหมือนกันแต่เราไม่อยากใช้ NSLog ก็อาจจะเขียนใหม่ได้ว่า</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> PrintToConsole<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> text;
<span style="color: #002200;">&#123;</span> 
   <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span> ; i &lt; <span style="color: #2400d9;">5</span> ; i<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span> 
   <span style="color: #002200;">&#123;</span> 
     <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;%d: %s &quot;</span>,i, <span style="color: #002200;">&#91;</span>text UTF8String<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
   <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>การเขียนแบบนี้เวลา run จะเกิด error น่ะครับ เพราะว่า [text UTF8String] มันจะไปจอง memory ใหม่แล้วส่งค่า char* กลับมา ฉนั้น ถ้าไม่มี NSAutoreleasePool มันจะ error</p>
<p>การเขียนที่ถูกต้องควรจะเป็น</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> PrintToConsole<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> text;
<span style="color: #002200;">&#123;</span> 
  <span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> threadPool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span> ; i &lt; <span style="color: #2400d9;">5</span> ; i<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>  
  <span style="color: #002200;">&#123;</span>   
    <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;%d: %s &quot;</span>,i, <span style="color: #002200;">&#91;</span>text UTF8String<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #002200;">&#125;</span>  
  <span style="color: #002200;">&#91;</span>threadPool drain<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>สำหรับ tutorial นี้จริงๆผมกะว่าจะเป็น guide ง่ายๆสำหรับคนที่รู้เรื่อง thread อยู่แล้ว ก็ถ้าใครยังไม่เข้าใจ ต้องการรู้เรื่องเกี่ยวกับ thread เพิ่มมากขึ้นก็แนะนำให้ไปอ่าน link ที่ข้างบน แล้วก็ค้นหาจาก google ก็ได้ครับ</p>
<p>ครั้งหน้า ก็ยังอยู่กับ objective-c น่ะครับ ส่วน source สำหรับ วันนี้ก็โหลดได้ที่นี่เลย
<div><a href="http://www.macfeteria.com/wp-content/uploads/2008/07/foundation_framework_thread.zip">Download Foundation Framework Thread Source Files</a></div>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/advance' rel='tag' target='_self'>advance</a>, <a class='technorati-link' href='http://technorati.com/tag/foundation' rel='tag' target='_self'>foundation</a>, <a class='technorati-link' href='http://technorati.com/tag/NSThread' rel='tag' target='_self'>NSThread</a>, <a class='technorati-link' href='http://technorati.com/tag/objective-c' rel='tag' target='_self'>objective-c</a>, <a class='technorati-link' href='http://technorati.com/tag/programming' rel='tag' target='_self'>programming</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.macfeteria.com/blog/2008/06/09/ojective-c-programming-thread/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Objective-C Programming Part III.</title>
		<link>http://www.macfeteria.com/blog/2008/03/03/objective-c-programming-part-iii/</link>
		<comments>http://www.macfeteria.com/blog/2008/03/03/objective-c-programming-part-iii/#comments</comments>
		<pubDate>Sun, 02 Mar 2008 19:26:01 +0000</pubDate>
		<dc:creator>[Ter]</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[advance]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://maccafe.wordpress.com/?p=15</guid>
		<description><![CDATA[Polymorphisms Polymorphisms สรุปง่ายๆว่าเป็นการอนุญาติให้ derived class ( child class ) สามารถใช้ function ของ parent ได้ สำหรับ polymorphisms นั้นเห็นเด่นชัดมากที่สุดก็คงจะเป็น Overriding / Overloading Overriding เป็นการเขียน function ของ Child class ใช้เองโดยไม่ต้องใช้ function ของ Parent Class ถ้าจะยกตัวอย่างง่ายๆ ก็เป็นต้นว่า ถ้าเรามี class ชื่อว่า Car แล้ว Car นั้นมีฟังชั่นขื่อว่า drive สมมติว่าเราจะเขียน class ใหม่ขึ้นมาโดยสืบทอด (inherit) มาจาก Car โดยชื่อว่า Tank มีฟังชั่นชื่อว่า drive เหมือนกัน แต่ว่า class ทั้งสองนั้น [...]]]></description>
			<content:encoded><![CDATA[<h2>Polymorphisms</h2>
<p>Polymorphisms สรุปง่ายๆว่าเป็นการอนุญาติให้ derived class ( child class ) สามารถใช้ function ของ parent ได้ สำหรับ polymorphisms นั้นเห็นเด่นชัดมากที่สุดก็คงจะเป็น Overriding / Overloading</p>
<ul>
<li><strong>Overriding</strong><br />
เป็นการเขียน function ของ  Child class ใช้เองโดยไม่ต้องใช้ function ของ Parent Class  ถ้าจะยกตัวอย่างง่ายๆ ก็เป็นต้นว่า ถ้าเรามี class ชื่อว่า Car แล้ว Car นั้นมีฟังชั่นขื่อว่า drive  สมมติว่าเราจะเขียน class ใหม่ขึ้นมาโดยสืบทอด (inherit) มาจาก Car โดยชื่อว่า Tank มีฟังชั่นชื่อว่า drive เหมือนกัน แต่ว่า class ทั้งสองนั้น Drive ทำงานไม่เหมือนกัน  คือพูดง่ายๆว่า รถยนต์ก็<span style="color: #a0410d;"> ขับ(drive)</span>ได้ รถถังก็<span style="color: #a0410d;"> ขับ(drive)</span> ได้ แต่วิธีการที่จะขับไม่เหมือนกัน</li>
<li><strong>Overloading</strong><br />
เป็นการเขียน Function ชื่อเหมือนกัน ใน class เดียวกัน แต่รับ parameter คนละอย่างกัน เช่นว่า เรามี class ชื่อว่า Rectangle และมี function ในการคำนวนพื้นที่ เราอาจจะต้องการให้มันรับ parameter ได้ทั้ง int และ float ในลักษณะแบบนี้เราก็จะใช้ overloading เข้ามาให้เกิดประโยชน์</li>
</ul>
<p>อาจจะงง เล็กน้อย สรุปสั้น Overriding ก็คือ child class ที่มีการเขียน function ที่ชื่อเหมือนกับ parent ขึ้นมาเองโดยไม่ต้องสืบทอดจาก parent และ Overloading คือ method ที่สามารถรับ parameter ได้หลายๆรูปแบบ </p>
<p>แต่มาดู code อาจจะเข้าใจง่ายกว่า</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Car.h</span>
<span style="color: #6e371a;">#import </span>
<span style="color: #a61390;">@interface</span> Car<span style="color: #002200;">:</span>Object <span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">int</span> m_speed;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> drive;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> drive<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>speed;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>และในส่วน implement</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Car.m</span>
<span style="color: #6e371a;">#import </span>
<span style="color: #a61390;">@implementation</span> Car;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> drive
<span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Car drive n&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> drive<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>speed
<span style="color: #002200;">&#123;</span>
m_speed <span style="color: #002200;">=</span> speed;
<span style="color: #a61390;">printf</span> <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Car drive at speed %d n&quot;</span>,m_speed<span style="color: #002200;">&#41;</span>; <span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>จากตัวอย่างข้างบนเป็นการ แสดงให้เห็นถึง Overloading คือ จะเห็นว่า Car นั้นมี Function ชื่อเหมือนกัน นั่นคือ drive แต่รับ parameter มาไม่เหมือนกัน โดยที่ drive แรกนั้น ไม่ได้รับ parameter มาเลย ส่วนตัวที่สอง รับ int เข้ามาและ ก็ในส่วนการทำงานของแต่ละฟังชั่นก็ต่างกัน</p>
<p>เวลาเรียกใช้เราอาจจะเขียนได้ว่า</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;car.h&quot;</span>
<span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
Car <span style="color: #002200;">*</span>honda <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Car alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>honda drive<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>honda drive<span style="color: #002200;">:</span><span style="color: #2400d9;">80</span><span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>ส่วนผลลัพธ์ที่ออกมาก็จะเป็น</p>
<pre>Car drive
Car drive at speed 80</pre>
<p>ทีนี้เรามาดู Overriding กันบ้าง ก็อย่างที่บอกไปแล้วว่า เราสารมารถเขียน ให้ child class ทำงานต่างจาก parent โดยที่ ขื่อเหมือนกันได้มาดู กันเลยดีกว่า</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Tank.h</span>
<span style="color: #6e371a;">#import &quot;car.h&quot;</span>
<span style="color: #a61390;">@interface</span> Tank<span style="color: #002200;">:</span>Car <span style="color: #002200;">&#123;</span>
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>ก็จะเห็นว่าใน class tank นั้นไม่มีอะไรเลย คือ inherit มาจาก car อย่างเดียว แต่เราสารมารถเขียน implement ของตัวเองโดยอิงจาก parent ได้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;tank.h&quot;</span>
<span style="color: #a61390;">@implementation</span> Tank
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> drive
<span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">printf</span> <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Tank Driven&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>ก็เป็นอันเสร็จ คือเรา เขียน funtion drive ขึ้นมาใหม่ แทนที่ ตัวเก่าที่มีอยู่ใน Car</p>
<p>และก็มาลองเรียกใช้งานกันดู</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;car.h&quot;</span>
<span style="color: #6e371a;">#import &quot;tank.h</span>
<span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
Car <span style="color: #002200;">*</span>honda <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Car alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
Tank <span style="color: #002200;">*</span>tank <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Tank alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>honda drive<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>tank drive<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>tank drive<span style="color: #002200;">:</span><span style="color: #2400d9;">20</span><span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>ส่วนผลลัพธ์ที่ได้ก็จะได้ว่า</p>
<pre>Car drive
Tank drive
Car drive at speed 20</pre>
<p>จากผลลัพธ์ก็จะเห็นว่า Tank drive ที่ออกมา โปรแกรมจะไปเรียก function ใน class Tank</p>
<p>ส่วนบรรทัดสุดท้าย ตัวโปรแกรมจะไปเรียกใช้ parent ของ tank นั่นก็คือ Car เพราะเนื่องจากว่า tank นั้น Overriding เพียงแค่ฟังชั่น</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> drive;</pre></div></div>

<p>แต่สำหรับ</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> drive<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>speed;</pre></div></div>

<p>ไม่ได้ทำการ overriding</p>
<p>มันจึงไปใช้งาน parent ของมันแทน</p>
<p>เพิ่มเติม <a href="http://gd.tuwien.ac.at/languages/c/c++oop-pmueller/">http://gd.tuwien.ac.at/languages/c/c++oop-pmueller/</a><br />
และ <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/OOP_ObjC/Introduction/chapter_1_section_1.html">http://developer.apple.com/documentation/Cocoa/Conceptual/OOP_ObjC/Introduction/chapter_1_section_1.html</a></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/advance' rel='tag' target='_self'>advance</a>, <a class='technorati-link' href='http://technorati.com/tag/class' rel='tag' target='_self'>class</a>, <a class='technorati-link' href='http://technorati.com/tag/objective-c' rel='tag' target='_self'>objective-c</a>, <a class='technorati-link' href='http://technorati.com/tag/programming' rel='tag' target='_self'>programming</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.macfeteria.com/blog/2008/03/03/objective-c-programming-part-iii/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Pure Virtual</title>
		<link>http://www.macfeteria.com/blog/2008/02/28/pure-virtual/</link>
		<comments>http://www.macfeteria.com/blog/2008/02/28/pure-virtual/#comments</comments>
		<pubDate>Thu, 28 Feb 2008 16:44:42 +0000</pubDate>
		<dc:creator>[Ter]</dc:creator>
				<category><![CDATA[Technic]]></category>
		<category><![CDATA[advance]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://maccafe.wordpress.com/?p=10</guid>
		<description><![CDATA[คือจาก เมื่อวันที่ผ่านมา เนื่องจากว่า งานที่ทำอยู่มีส่วนต้องให้ port โปรแกรมจาก Win มาสู่ mac และบังเอิญว่า ผม compile แล้วมันผ่าน แต่ว่าตอน Runtime มันดัน Error สิ่งที่มันบอกมาก็คือว่า pure virtual method called terminated called with out an active exception ปัญหาที่เกิดขึ้น ก็ยัง งงๆๆว่าเป็นเพราะอะไร แต่พอหลังจากให้เพื่อนมาดูแล้วก็นั่งถกกัน ว่ามันเกิด เฉพาะกับ mac หรือเปล่าเพราะว่าใน win ไม่เห็นมันจะเกิด ก็เลยทำการเขียน code ที่ทำงานแบบเดียวกัน ก็ัดังตัวอย่างข้างล่าง #include &#60;stdio.h&#62; class Base; void foo&#40;Base*&#41;; class Base &#123; public: Base&#40;&#41;&#123;&#125;; virtual [...]]]></description>
			<content:encoded><![CDATA[<p>คือจาก เมื่อวันที่ผ่านมา  เนื่องจากว่า งานที่ทำอยู่มีส่วนต้องให้ port โปรแกรมจาก Win มาสู่ mac และบังเอิญว่า ผม compile แล้วมันผ่าน แต่ว่าตอน Runtime มันดัน Error สิ่งที่มันบอกมาก็คือว่า  <span style="color: #808000;"><strong>pure virtual method called terminated called with out an active exception</strong></span> ปัญหาที่เกิดขึ้น ก็ยัง งงๆๆว่าเป็นเพราะอะไร แต่พอหลังจากให้เพื่อนมาดูแล้วก็นั่งถกกัน ว่ามันเกิด เฉพาะกับ mac หรือเปล่าเพราะว่าใน win ไม่เห็นมันจะเกิด ก็เลยทำการเขียน code ที่ทำงานแบบเดียวกัน ก็ัดังตัวอย่างข้างล่าง</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;stdio.h&gt;</span>
<span style="color: #0000ff;">class</span> Base<span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> foo<span style="color: #008000;">&#40;</span>Base<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">class</span> Base
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
		Base<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">virtual</span> ~Base<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			foo<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> banana<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> foo<span style="color: #008000;">&#40;</span>Base<span style="color: #000040;">*</span> base<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	base<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>banana<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Derived<span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Base
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
		Derived<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
		~Derived<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">void</span> banana<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Banana&quot;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
&nbsp;
	Derived<span style="color: #000040;">*</span> test <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Derived<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Hello, World!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">delete</span> test<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>ก็ถ้า ดูตาม code ก็จะเห็นว่า มี Base กับ Derived  <span style="color: #808000;"><strong>เนื่องจากว่า Base Class มันเป็น Pure Virtual (Abtract Class) เราต้อง implement ส่วนของ code การทำงานใน derived class เสมอไม่อาจจะเขียน implement ใน Base Class ได้  </strong></span> และปัญหาจากโปรแกรมข้างบนเกิดขึ้นเพราะในขณะที่ base class ได้เรียก destructor ของตัวเองนั้น มันกลับไปเรียกใช้งานฟังชั่นใน derived class ด้วย ซึ่งนั่นก็ทำให้มันเกิด error เพราะว่า derived class ได้ถูกทำลายไปก่อนแล้ว  ทำให้ไม่สามารถเรียกได้</p>
<p>ลองดูตรง</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">virtual</span> ~Base<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	foo<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>จาก destructor นี้จะเห็นว่า มีการเรียกฟังก์ชั่น foo และแน่นอนว่า foo ไปเรียก</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">base<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>banana<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>อีกที สืบเนื่องจากว่ามันเป็น pure virtual แปลว่าตัว Base Class เองไม่อาจจะเขียน implement code ได้มันจึงต้องไปเรียก banana ในตัว Derived Class ที่มีการเขียน implement code และ Derived นั้นได้ทำลายจาก destructor ไปเรียบร้อยแล้ว มันเลยไม่เจอ banana ทำให้เกิด error นั่นเอง</p>
<p>ลองศึกษา เพิ่มเติมเกี่ยวกับ Virtual  Function ได้ที่ <a title="Vitual Function" href="http://en.wikipedia.org/wiki/Virtual_function">http://en.wikipedia.org/wiki/Virtual_function</a></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/advance' rel='tag' target='_self'>advance</a>, <a class='technorati-link' href='http://technorati.com/tag/c%2B%2B' rel='tag' target='_self'>c++</a>, <a class='technorati-link' href='http://technorati.com/tag/class' rel='tag' target='_self'>class</a>, <a class='technorati-link' href='http://technorati.com/tag/programming' rel='tag' target='_self'>programming</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.macfeteria.com/blog/2008/02/28/pure-virtual/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

