<?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>结网 &#187; Objective C</title>
	<atom:link href="http://blog.liuhongwei.cn/tag/objective-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.liuhongwei.cn</link>
	<description>Agile, Startup, 产品管理, Google开发, iPhone开发</description>
	<lastBuildDate>Fri, 30 Jul 2010 10:31:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>10个iPhone开发网站、论坛、博客</title>
		<link>http://blog.liuhongwei.cn/iphone/10-iphone-dev/</link>
		<comments>http://blog.liuhongwei.cn/iphone/10-iphone-dev/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 14:57:06 +0000</pubDate>
		<dc:creator>Harry</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Objective C]]></category>

		<guid isPermaLink="false">http://blog.liuhongwei.cn/?p=102</guid>
		<description><![CDATA[整理了一下手中的iPhone开发网站、论坛和博客，精选了这10个常去的网站。其中有2篇教学文章，由于非常经典，于是单列出来，与其网站并列。本文的短网址为 http://bit.ly/3llz7
建议新入行的朋友在看教学文章的同时多上论坛，与大家交流，偶尔看看别人的自曝，自己也会信心倍增。另外，实践最重要，首先行动起来。
http://cocoachina.com/ 大量iPhone开发资料，教学，经验，还有自曝。
http://www.javaeye.com/forums/board/mobile iPhone开发论坛，人气鼎盛，大量自曝，还有Android的讨论
http://developer.apple.com/iphone/ 苹果iPhone开发官方主页，提供大量实例、文档和教学视频
http://www.stanford.edu/class/cs193p/ 斯坦福大学的iPhone课程，2009年4～6月，有pdf和视频下载
http://bit.ly/RuaKq 飞机游戏iFighter的作者dr_watson写的iPhone游戏开发经典教学系列，有源码
http://www.iphoneside.com/ iPhone达人的博客，很多有价值的资源下载，新闻和教程
http://lichen1985.com/blog/ iPhone应用的作者，雨雪霏霏的iPhone博客，很多开发手记和心得
http://blog.liuhongwei.cn/category/iphone/ iPhone开发博客，提供教学文章，经验分享，还有GAE
http://www.javaeye.com/topic/422948 iPhone日本市场高手的自曝贴，第1个月就赚了2千刀，大量详细的数字和图片
http://dev.iphonetw.net/ 台湾iPhone开发者论坛，有些教学文章和讨论
]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-103" title="iPhone SDK" src="http://blog.liuhongwei.cn/wp-content/uploads/2009/08/iphone-sdk.png" alt="iPhone SDK" width="102" height="95" />整理了一下手中的iPhone开发网站、论坛和博客，精选了这10个常去的网站。其中有2篇教学文章，由于非常经典，于是单列出来，与其网站并列。本文的短网址为 <a href="http://bit.ly/3llz7">http://bit.ly/3llz7</a></p>
<p>建议新入行的朋友在看教学文章的同时多上论坛，与大家交流，偶尔看看别人的自曝，自己也会信心倍增。另外，实践最重要，首先行动起来。</p>
<p><a href="http://cocoachina.com/">http://cocoachina.com/</a> 大量iPhone开发资料，教学，经验，还有自曝。</p>
<p><a href="http://www.javaeye.com/forums/board/mobile">http://www.javaeye.com/forums/board/mobile</a> iPhone开发论坛，人气鼎盛，大量自曝，还有Android的讨论</p>
<p><a href="http://developer.apple.com/iphone/">http://developer.apple.com/iphone/</a> 苹果iPhone开发官方主页，提供大量实例、文档和教学视频</p>
<p><a href="http://www.stanford.edu/class/cs193p/">http://www.stanford.edu/class/cs193p/</a> 斯坦福大学的iPhone课程，2009年4～6月，有pdf和视频下载</p>
<p><a href="http://bit.ly/RuaKq">http://bit.ly/RuaKq</a> 飞机游戏iFighter的作者dr_watson写的iPhone游戏开发经典教学系列，有源码</p>
<p><a href="http://www.iphoneside.com/">http://www.iphoneside.com/</a> iPhone达人的博客，很多有价值的资源下载，新闻和教程</p>
<p><a href="http://lichen1985.com/blog/">http://lichen1985.com/blog/</a> iPhone应用的作者，雨雪霏霏的iPhone博客，很多开发手记和心得</p>
<p><a href="http://blog.liuhongwei.cn/category/iphone/">http://blog.liuhongwei.cn/category/iphone/</a> iPhone开发博客，提供教学文章，经验分享，还有GAE</p>
<p><a href="http://www.javaeye.com/topic/422948">http://www.javaeye.com/topic/422948</a> iPhone日本市场高手的自曝贴，第1个月就赚了2千刀，大量详细的数字和图片</p>
<p><a href="http://dev.iphonetw.net/">http://dev.iphonetw.net/</a> 台湾iPhone开发者论坛，有些教学文章和讨论</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.liuhongwei.cn/iphone/10-iphone-dev/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>zt Objective C 入门诗，iPhone程序结构</title>
		<link>http://blog.liuhongwei.cn/iphone/objective-c-poetry-iphone-structure/</link>
		<comments>http://blog.liuhongwei.cn/iphone/objective-c-poetry-iphone-structure/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 00:21:35 +0000</pubDate>
		<dc:creator>Harry</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective C]]></category>
		<category><![CDATA[教学]]></category>

		<guid isPermaLink="false">http://blog.liuhongwei.cn/?p=68</guid>
		<description><![CDATA[Objective C 快速入门诗
C没有类
这让人很疲惫
对象的说法很时髦
不就是继承封装组合人人会
右走是C++，这个大众都熟悉它
左走就是objective-c，躲在僻静僻静的麦金塔
本是同根生的C
如何高举面向对象的大旗
求同存异标新立异且听一一细分清

对象的C
是不同的C
类的处理与众不同重点要区分

不重复是我的口头禅
任何时候我只说一次告诉你我的地盘
    [@begincommand] &#124; @end就是我的指示
        @interface classname  声明类
        @implementation classname  实现类
        @protocol interfacename  声明接口
就是这么简单，你的一定要明白

我先声明对成员变量和方法的关系有远近
    成员变量大大括在我的怀
        关系远近 public [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Objective C 快速入门诗</strong></p>
<pre style="line-height: 22px; padding: 0px; margin: 0px;">C没有类
这让人很疲惫
对象的说法很时髦
不就是继承封装组合人人会
右走是C++，这个大众都熟悉它
左走就是objective-c，躲在僻静僻静的麦金塔
本是同根生的C
如何高举面向对象的大旗
求同存异标新立异且听一一细分清

对象的C
是不同的C
类的处理与众不同重点要区分

不重复是我的口头禅
任何时候我只说一次告诉你我的地盘
    [@begincommand] | @end就是我的指示
        @interface classname  声明类
        @implementation classname  实现类
        @protocol interfacename  声明接口
就是这么简单，你的一定要明白

我先声明对成员变量和方法的关系有远近
<span style="line-height: 22px; color: #0000ff; padding: 0px; margin: 0px;"><span style="line-height: 22px; color: #000000; padding: 0px; margin: 0px;">    </span><strong>成员变量大大括在我的怀</strong></span>
        关系远近 public / protected / private 来区分
        若不声明全默默受着保护
        静态成员变量请不要放在此
    成员方法我不太爱
        前面拴着绳子置于类的变量列表外
<span style="line-height: 22px; color: #0000ff; padding: 0px; margin: 0px;"><span style="line-height: 22px; color: #000000; padding: 0px; margin: 0px;">            </span><strong>减号就是普通函数</strong></span>
<span style="line-height: 22px; color: #0000ff; padding: 0px; margin: 0px;"><span style="line-height: 22px; color: #000000; padding: 0px; margin: 0px;">            </span><strong>加号就是静态函数</strong></span>莫怪
        函数的声明有点怪
            我要一眼叫出名，包括函数名和参数名
            其余的<span style="line-height: 22px; color: #0000ff; padding: 0px; margin: 0px;"><strong>类型括起来</strong></span>，我有空再来慢慢看
            函数名和参数名之间用：分隔开
        多个参数的函数那就是更怪
            逗号其实不是我的爱
            后续的每个参数分隔用空白
            每个参数形式如 <span style="line-height: 22px; color: #0000ff; padding: 0px; margin: 0px;"><strong>标签：(参数类型)参数名</strong></span>
            其实只要把函数名看作是第一个参数标签
            你就能领悟其中的奥妙并在调用时喜爱
        成员方法太多我才懒得区分亲疏远近
            它们一律都是公之于众要好好的干活
            谁若不服胆敢出头犯上
            就踢出头文件打入冷宫永世不能露面试试看
<span style="line-height: 22px; color: #0000ff; padding: 0px; margin: 0px;"><span style="line-height: 22px; color: #000000; padding: 0px; margin: 0px;">        </span><strong>所有的成员方法都是虚函数</strong></span>
            改则重载，不改就继承
            不用任何标记请放心大胆随便使用

对象的类，在继承层次上考虑才显得重要
    所有的类最终都继承于NSObject
        因为它给你创建自己生命的方法

    不继承于NSObject的只能是接口protocol
        它没有成员变量只有方法
        它只有声明不用实现
        这点和java很相似

当然类还有一些绝活可以让你开开眼
    可以动态的扩充一个类 而不总是用继承
        @interface CClassA(Category)|@end
        @implementation CClassA (Category)|@end
        源代码外包含该 类的头文件
        就可以使用该Category中的方法

也可以动态的替换一个类
    但是只限于用继承类来替换基类功能
        [CInheritClass poseAsClass: [CBaseClass class]];
    声明之后用基类创建的对象实际上行为都是继承类

所以一个类的完整声明如下：
<span style="line-height: 22px; color: #0000ff; padding: 0px; margin: 0px;"><span style="line-height: 22px; color: #000000; padding: 0px; margin: 0px;">    </span><strong>@interface CClassA(Category): CBaseClass&lt;IA,IB...&gt;</strong></span>
    类别，基类，接口一一具全
    加上静态 虚函数
    所有特性和C++均可对应
    再加上所独有的特性
    这一特性编译类语言很少有
    设计得当完全可以实现动态调用dll切换功能

类的定义和使用，也有点怪
    想想看一个对象的生命周期，该是如何办
        创建很是别扭
            这样就定义了一个类对象，
            CClassName* pA = [[CClassName alloc] init];
            能且只能返回对象指针
    释放分为两种
        自动释放声明后 [pA autorelease]; 以后可以不管
        否则就必须配对 [pA release];
    注意类的构造函数init可以捕获
    但是<span style="line-height: 22px; color: #0000ff; padding: 0px; margin: 0px;"><strong>析构函数却没有</strong></span>这点很让人意外

    使用类也有多种情况
        调用成员方法用中括号     int nRtn = [pA method:param1];
        而静态成员方法不需要对象 int nRtn = [CClassName method];
        调用成员变量还是C格式    pA-&gt;m_nCount = 1;

    所有的类，都可以对应一个id类型
        用来引用一个对象               id     cur = sq;
        或者用来引用一个对象指定的接口 id&lt;IA&gt; cur = sq;
        然后就可以调用该对象的方法和指定接口方法</pre>
<p><strong>Iphone APP程序结构</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.liuhongwei.cn/iphone/objective-c-poetry-iphone-structure/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>从 C 到 Objective C 入门2</title>
		<link>http://blog.liuhongwei.cn/iphone/objective-c-2/</link>
		<comments>http://blog.liuhongwei.cn/iphone/objective-c-2/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 07:39:22 +0000</pubDate>
		<dc:creator>Harry</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective C]]></category>
		<category><![CDATA[教学]]></category>

		<guid isPermaLink="false">http://blog.liuhongwei.cn/?p=54</guid>
		<description><![CDATA[在Objective C入门中我们了解了如何定义类，类的实现，以及调用类函数的方式。现在再来看一看关于类的高级概念。
id，动态绑定

#import &#60;stdio.h&#62;
#import &#34;Bank.h&#34;
&#160;
int main&#40; int argc, const char *argv&#91;&#93; &#41; &#123;
    Bank *bank1= &#91;&#91;Bank alloc&#93; init&#93;;
    Bank *bank2= &#91;&#91;Bank alloc&#93; init&#93;;
    id b; //可以把任何对象赋值给b
&#160;
    &#91;bank1 addAmount: 1&#93;;
    b = bank1;
    &#91;b print&#93;; //调用bank1的print函数
    [...]]]></description>
			<content:encoded><![CDATA[<p>在<a href="http://blog.liuhongwei.cn/iphone/objective-c/">Objective C入门</a>中我们了解了如何定义类，类的实现，以及调用类函数的方式。现在再来看一看关于类的高级概念。</p>
<h2>id，动态绑定</h2>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;stdio.h&gt;</span>
<span style="color: #6e371a;">#import &quot;Bank.h&quot;</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>
    Bank <span style="color: #002200;">*</span>bank1<span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Bank alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    Bank <span style="color: #002200;">*</span>bank2<span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Bank alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">id</span> b; <span style="color: #11740a; font-style: italic;">//可以把任何对象赋值给b</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>bank1 addAmount<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
    b <span style="color: #002200;">=</span> bank1;
    <span style="color: #002200;">&#91;</span>b print<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//调用bank1的print函数</span>
    b <span style="color: #002200;">=</span> bank2;
    <span style="color: #002200;">&#91;</span>b print<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//调用bank2的print函数</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// true</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>bank1 isMemberOfClass<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span>Bank class<span style="color: #002200;">&#93;</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: #002200;">&#123;</span>
        <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">&quot;bank1是Bank类的一个对象<span style="color: #2400d9;">\n</span>&quot;</span> <span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// true</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>bank1 respondsToSelector<span style="color: #002200;">:</span> <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span> addAmount<span style="color: #002200;">:</span> <span style="color: #002200;">&#41;</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: #002200;">&#123;</span>
        <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">&quot;bank1响应addAmount:函数<span style="color: #2400d9;">\n</span>&quot;</span> <span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// free memory</span>
    <span style="color: #002200;">&#91;</span>bank1 <span style="color: #a61390;">free</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>b <span style="color: #a61390;">free</span><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>id类型可以接受各种类型的对象，然后调用对应的函数。调用函数时，Objective C在编译的时候并不会检查该对象是否有该函数，需要开发人员自己确保调用的有效。另外Objective C也提供功能，在运行时检查某对象是否具有某函数。</p>
<h2>类的高级使用：继承、类别、扮演、协议</h2>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">////////////// BankMath.h</span>
<span style="color: #6e371a;">#import &quot;Bank.h&quot;</span>
<span style="color: #a61390;">@interface</span> Bank<span style="color: #002200;">&#40;</span>Math<span style="color: #002200;">&#41;</span> <span style="color: #11740a; font-style: italic;">//为类Bank定义一个类别Math</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> add<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> b; <span style="color: #11740a; font-style: italic;">//Math中的函数add</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> sub<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> b; <span style="color: #11740a; font-style: italic;">//Math中的函数sub</span>
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@protocol</span> Auditable <span style="color: #11740a; font-style: italic;">//定义一个协议</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> audit; <span style="color: #11740a; font-style: italic;">//其中包括一个函数audit的定义</span>
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@interface</span> ExBank<span style="color: #002200;">:</span> Bank&lt;Auditable&gt; <span style="color: #11740a; font-style: italic;">//ExBank类继承Bank，同时实现Auditable协议</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> print; <span style="color: #11740a; font-style: italic;">//定义一个新的函数print，覆盖父类Bank中的print函数</span>
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">////////////// 实现部分 BankMath.m</span>
<span style="color: #6e371a;">#import &quot;BankMath.h&quot;</span>
<span style="color: #a61390;">@implementation</span> Bank <span style="color: #002200;">&#40;</span>Math<span style="color: #002200;">&#41;</span> <span style="color: #11740a; font-style: italic;">//Bank的类别Math的实现部分</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> add<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> f <span style="color: #002200;">&#123;</span>
    amount <span style="color: #002200;">++</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> sub<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> f <span style="color: #002200;">&#123;</span>
    amount <span style="color: #002200;">--</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> ExBank
<span style="color: #11740a; font-style: italic;">// 实现新定义的函数print</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> print <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;ExBank amount=%i&quot;</span>, amount<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #11740a; font-style: italic;">// 实现协议中包含的函数audit</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> audit <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;audit: amount=%i&quot;</span>, amount<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">////////////// main.m</span>
<span style="color: #6e371a;">#import  &lt;stdio.h&gt;</span>
<span style="color: #6e371a;">#import &quot;Bank.h&quot;</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>
    Bank <span style="color: #002200;">*</span>bank <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Bank alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
     <span style="color: #002200;">&#91;</span>bank add<span style="color: #002200;">:</span> <span style="color: #2400d9;">10</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//直接使用Bank对象类别Math中的函数</span>
     <span style="color: #002200;">&#91;</span>bank sub<span style="color: #002200;">:</span> <span style="color: #2400d9;">5</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//直接使用Bank对象类别Math中的函数</span>
     <span style="color: #002200;">&#91;</span>bank print<span style="color: #002200;">&#93;</span>;
&nbsp;
     <span style="color: #002200;">&#91;</span>ExBank poseAsClass<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span>Bank class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//使用ExBank来扮演Bank</span>
     Bank <span style="color: #002200;">*</span>bank2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Bank alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//对Bank的调用，就是对ExBank的调用</span>
     <span style="color: #002200;">&#91;</span>bank2 print<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//调用ExBank的print函数</span>
    <span style="color: #002200;">&#91;</span>bank2 audit<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//调用ExBank的audit函数</span>
&nbsp;
    <span style="color: #a61390;">id</span> auditable <span style="color: #002200;">=</span> bank2; <span style="color: #11740a; font-style: italic;">//为auditable赋值bank2</span>
    <span style="color: #002200;">&#91;</span>auditable audit<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//调用bank2的函数audit</span>
    <span style="color: #11740a; font-style: italic;">//[auditable print]; //不行，因为Auditable类型中没有该函数</span>
&nbsp;
     <span style="color: #002200;">&#91;</span>bank <span style="color: #a61390;">free</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// 释放对象</span>
    <span style="color: #002200;">&#91;</span>bank2 <span style="color: #a61390;">free</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// 释放对象</span>
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<ul>
<li>
<div lang="objc">类定义@interface，相当于Java和C++中的class</div>
</li>
<li>
<div lang="objc">继承，与Java继承类似，没有C++中继承的那么多繁琐的访问范围差别</div>
</li>
<li>
<div lang="objc">类别（categories），是用来扩展类的好办法，比继承更方便，因为你不需要有源代码就可以扩充某个类。</div>
</li>
<li>
<div lang="objc">扮演（Posting），可以在运行时指定用某个类A来替代另一个类B，这样此后所有对B的调用都是对A的调用。</div>
</li>
<li>
<div lang="objc">协议（@protocol），相当于Java的接口，或C++的abstract class。定义在其中的函数只有定义而不需要实现。使用协议的时候，一个类可以实现多个协议，多个协议之间用逗号分隔列出。</div>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.liuhongwei.cn/iphone/objective-c-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>从 C 到 Objective C 入门1</title>
		<link>http://blog.liuhongwei.cn/iphone/objective-c/</link>
		<comments>http://blog.liuhongwei.cn/iphone/objective-c/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 00:35:30 +0000</pubDate>
		<dc:creator>Harry</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective C]]></category>
		<category><![CDATA[教学]]></category>

		<guid isPermaLink="false">http://blog.liuhongwei.cn/?p=34</guid>
		<description><![CDATA[进军iPhone开发，最大的难点之一就是怪异的Objective C语法了。不过，了解之后才发现，原来仅仅是形式上与C++、Java有差异，属于换汤不换药。如果有C语言基础，又用过C++、Java，2天之内掌握Objective C，没有问题，至少看例子程序会感觉顺眼很多。
Objective C脱胎于C语言，苹果版的开发编译环境就是大名鼎鼎的Xcode，其中的Cocoa就是Objective C库函数。
与C语言一致的部分

int main&#40; int argc, const char *argv&#91;&#93; &#41; &#123;
    printf&#40; &#34;hello worldn&#34; &#41;;
    return 0;
&#125;


数据类型，表达式，各种运算符
循环：for, while, do while, break, continue
分支：if, else, switch

类定义@interface

#import &#60;Foundation/NSObject.h&#62;
&#160;
@interface Bank: NSObject &#123;
    NSString *name;
    int amount;
&#125;
&#160;
-&#40;void&#41; print;
-&#40;void&#41; addAmount: &#40;int&#41; a;
-&#40;int&#41; amount;
-&#40;Bank *&#41; initWithName: &#40;NSString *&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>进军iPhone开发，最大的难点之一就是怪异的Objective C语法了。不过，了解之后才发现，原来仅仅是形式上与C++、Java有差异，属于换汤不换药。如果有C语言基础，又用过C++、Java，2天之内掌握Objective C，没有问题，至少看例子程序会感觉顺眼很多。</p>
<p>Objective C脱胎于C语言，苹果版的开发编译环境就是大名鼎鼎的Xcode，其中的Cocoa就是Objective C库函数。</p>
<h2>与C语言一致的部分</h2>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><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: #a61390;">printf</span><span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">&quot;hello worldn&quot;</span> <span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<ul>
<li>数据类型，表达式，各种运算符</li>
<li>循环：for, while, do while, break, continue</li>
<li>分支：if, else, switch</li>
</ul>
<h2>类定义@interface</h2>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Foundation/NSObject.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> Bank<span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>name;
    <span style="color: #a61390;">int</span> amount;
<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> print;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> addAmount<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> a;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> amount;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>Bank <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> initWithName<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> n andAmount<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> a;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<ul>
<li>写在Bank.h中</li>
<li>#import是引入库函数</li>
<li>@interface是表明定义了一个类，冒号后面是父类类型</li>
<li>两个大括号之间的部分就是类的成员变量，只有类函数能够直接访问</li>
<li>- 表明要定义一个类对象级别的函数；如果是+，表明要定义类级别的函数</li>
<li>返回值与参数的类型放在()内</li>
<li>函数定义中的: 表明后面会跟一个参数</li>
<li>如果函数有多个参数，要写多个xxx:</li>
<li>类的函数定义结束后，使用@end关闭</li>
</ul>
<h2>类实现@implementation</h2>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;Bank.h&quot;</span>
<span style="color: #6e371a;">#import &lt;Foundation/NSObject.h&gt;</span>
<span style="color: #6e371a;">#import &lt;Foundation/NSString.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> Bank
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> print <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Total amount is %i&quot;</span>, amount<span style="color: #002200;">&#41;</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> addAmount<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> a <span style="color: #002200;">&#123;</span>
    amount <span style="color: #002200;">+=</span> a;
<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> amount<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> amount;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>Bank <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> initWithName<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> n andAmount<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> a<span style="color: #002200;">&#123;</span>
    self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
    name <span style="color: #002200;">=</span> n;
    amount <span style="color: #002200;">=</span> a;
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<ul>
<li>写在Bank.m</li>
<li>@implementation定义函数的具体内容，后面接着类名</li>
<li>函数头按照定义来写</li>
<li>最后用@end来结束所有函数体</li>
</ul>
<h2>调用写好的类和类函数</h2>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Foundation/NSObject.h&gt;</span>
<span style="color: #6e371a;">#import &lt;Foundation/NSString.h&gt;</span>
<span style="color: #6e371a;">#import &quot;Bank.h&quot;</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: #11740a; font-style: italic;">//创建新对象，使用缺省初始化函数</span>
    Bank <span style="color: #002200;">*</span>bankDefault <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Bank alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
   <span style="color: #11740a; font-style: italic;">//创建新对象，使用自定义初始化函数</span>
   Bank <span style="color: #002200;">*</span>bank <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Bank alloc<span style="color: #002200;">&#93;</span> initWithName<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Harry&quot;</span> andAmount<span style="color: #002200;">:</span> <span style="color: #2400d9;">28</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>bank addAmount<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>bank print<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>bank <span style="color: #a61390;">free</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// 释放对象</span>
   <span style="color: #002200;">&#91;</span>bankDefault <span style="color: #a61390;">free</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// 释放对象</span>
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<ul>
<li>调用类函数的时候，首先创建并初始化对象。这里我们使用缺省的init和我们自定义的initWithName来分别初始化一个对象；</li>
<li>使用[]来调用函数，按照次序分别是：对象名，函数名，: 参数值（如果有），函数名第二部分（如果有），:第二参数值（如果有）；</li>
</ul>
<h2>参考：</h2>
<ul>
<li>Objective C编程，书的精简版：<a href="http://www.otierney.net/objective-c.html.zh-tw.big5">http://www.otierney.net/objective-c.html.zh-tw.big5</a></li>
<li>Objective-C语法快速入门：<a href="http://www.cocoachina.com/index.php/archives/cocoachina_122.html">http://www.cocoachina.com/index.php/archives/cocoachina_122.html</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.liuhongwei.cn/iphone/objective-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
