<?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>Voland.com.cn &#187; Objective-c</title>
	<atom:link href="http://www.voland.com.cn/category/coding/objectivec/feed" rel="self" type="application/rss+xml" />
	<link>http://www.voland.com.cn</link>
	<description>人生百年,诸事记之</description>
	<lastBuildDate>Thu, 02 Feb 2012 23:09:02 +0000</lastBuildDate>
	<language>zh</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>iphone中如何进行多线程编程</title>
		<link>http://www.voland.com.cn/iphone-in-the-multi-threaded-programming</link>
		<comments>http://www.voland.com.cn/iphone-in-the-multi-threaded-programming#comments</comments>
		<pubDate>Mon, 26 Apr 2010 03:29:12 +0000</pubDate>
		<dc:creator>Nortan</dc:creator>
				<category><![CDATA[Objective-c]]></category>
		<category><![CDATA[软件开发]]></category>
		<category><![CDATA[Iphone]]></category>
		<category><![CDATA[NSThread]]></category>
		<category><![CDATA[多线程]]></category>

		<guid isPermaLink="false">http://www.voland.com.cn/?p=427</guid>
		<description><![CDATA[多线程在各种编程语言中都是难点，很多语言中实现起来很麻烦，objective-c虽然源于c,但其多线程编程却相当简单，可以与java相媲美。这篇文章主要从线程创建与启动、线程的同步与锁、线程的交互、线程池等等四个方面简单的讲解一下iphone中的多线程编程。 一、线程创建与启动 线程创建主要有二种方式： - &#40;id&#41;init; // designated initializer - &#40;id&#41;initWithTarget:&#40;id&#41;target selector:&#40;SEL&#41;selector object:&#40;id&#41;argument; 当然，还有一种比较特殊，就是使用所谓的convenient method，这个方法可以直接生成一个线程并启动它，而且无需为线程的清理负责。这个方法的接口是： + &#40;void&#41;detachNewThreadSelector:&#40;SEL&#41;aSelector toTarget:&#40;id&#41;aTarget withObject:&#40;id&#41;anArgument 前两种方法创建后，需要手机启动，启动的方法是： - &#40;void&#41;start; 二、线程的同步与锁 要说明线程的同步与锁，最好的例子可能就是多个窗口同时售票的售票系统了。我们知道在java中，使用synchronized来同步，而iphone虽然没有提供类似java下的synchronized关键字，但提供了NSCondition对象接口。查看NSCondition的接口说明可以看出，NSCondition是iphone下的锁对象，所以我们可以使用NSCondition实现iphone中的线程安全。这是来源于网上的一个例子： SellTicketsAppDelegate.h 文件 // SellTicketsAppDelegate.h import &#60;UIKit/UIKit.h&#62; &#160; @interface SellTicketsAppDelegate : NSObject &#60;UIApplicationDelegate&#62; &#123; int tickets; int count; NSThread* ticketsThreadone; NSThread* ticketsThreadtwo; NSCondition* ticketsCondition; UIWindow *window; &#125; @property &#40;nonatomic, retain&#41; IBOutlet UIWindow *window; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.voland.com.cn/iphone-in-the-multi-threaded-programming/nsthread" rel="attachment wp-att-433"><img src="http://www.voland.com.cn/wp-content/uploads/2010/04/NSthread.png" alt="多线程编程" title="NSthread" width="580" height="159" class="aligncenter size-full wp-image-433" /></a><br />
多线程在各种编程语言中都是难点，很多语言中实现起来很麻烦，objective-c虽然源于c,但其多线程编程却相当简单，可以与java相媲美。这篇文章主要从线程创建与启动、线程的同步与锁、线程的交互、线程池等等四个方面简单的讲解一下iphone中的多线程编程。</p>
<p><strong>一、线程创建与启动</strong><br />
线程创建主要有二种方式：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>init<span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// designated initializer</span>
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>initWithTarget<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>target selector<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>SEL<span style="color: #009900;">&#41;</span>selector object<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>argument<span style="color: #339933;">;</span></pre></div></div>

<p>当然，还有一种比较特殊，就是使用所谓的convenient method，这个方法可以直接生成一个线程并启动它，而且无需为线程的清理负责。这个方法的接口是：<span id="more-427"></span></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>detachNewThreadSelector<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>SEL<span style="color: #009900;">&#41;</span>aSelector toTarget<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>aTarget withObject<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>anArgument</pre></div></div>

<p>前两种方法创建后，需要手机启动，启动的方法是：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>start<span style="color: #339933;">;</span></pre></div></div>

<p><strong>二、线程的同步与锁</strong><br />
要说明线程的同步与锁，最好的例子可能就是多个窗口同时售票的售票系统了。我们知道在java中，使用synchronized来同步，而iphone虽然没有提供类似java下的synchronized关键字，但提供了NSCondition对象接口。查看NSCondition的接口说明可以看出，NSCondition是iphone下的锁对象，所以我们可以使用NSCondition实现iphone中的线程安全。这是来源于网上的一个例子：<br />
SellTicketsAppDelegate.h 文件</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//  SellTicketsAppDelegate.h</span>
import <span style="color: #339933;">&lt;</span>UIKit<span style="color: #339933;">/</span>UIKit.<span style="color: #202020;">h</span><span style="color: #339933;">&gt;</span>
&nbsp;
@interface SellTicketsAppDelegate <span style="color: #339933;">:</span> NSObject <span style="color: #339933;">&lt;</span>UIApplicationDelegate<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #993333;">int</span> tickets<span style="color: #339933;">;</span>
     <span style="color: #993333;">int</span> count<span style="color: #339933;">;</span>
     NSThread<span style="color: #339933;">*</span> ticketsThreadone<span style="color: #339933;">;</span>
     NSThread<span style="color: #339933;">*</span> ticketsThreadtwo<span style="color: #339933;">;</span>
     NSCondition<span style="color: #339933;">*</span> ticketsCondition<span style="color: #339933;">;</span>
     UIWindow <span style="color: #339933;">*</span>window<span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
@property <span style="color: #009900;">&#40;</span>nonatomic<span style="color: #339933;">,</span> retain<span style="color: #009900;">&#41;</span> IBOutlet UIWindow <span style="color: #339933;">*</span>window<span style="color: #339933;">;</span>
@end</pre></div></div>

<p>SellTicketsAppDelegate.m 文件</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//  SellTicketsAppDelegate.m</span>
import <span style="color: #ff0000;">&quot;SellTicketsAppDelegate.h&quot;</span>
&nbsp;
@implementation SellTicketsAppDelegate
@synthesize window<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>applicationDidFinishLaunching<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>UIApplication <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>application <span style="color: #009900;">&#123;</span>
     tickets <span style="color: #339933;">=</span> <span style="color: #0000dd;">100</span><span style="color: #339933;">;</span>
     count <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
     <span style="color: #666666; font-style: italic;">// 锁对象</span>
     ticketCondition <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>NSCondition alloc<span style="color: #009900;">&#93;</span> init<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
     ticketsThreadone <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>NSThread alloc<span style="color: #009900;">&#93;</span> initWithTarget<span style="color: #339933;">:</span>self selector<span style="color: #339933;">:</span>@selector<span style="color: #009900;">&#40;</span>run<span style="color: #009900;">&#41;</span> object<span style="color: #339933;">:</span>nil<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#91;</span>ticketsThreadone setName<span style="color: #339933;">:</span>@<span style="color: #ff0000;">&quot;Thread-1&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#91;</span>ticketsThreadone start<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>  
&nbsp;
&nbsp;
     ticketsThreadtwo <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>NSThread alloc<span style="color: #009900;">&#93;</span> initWithTarget<span style="color: #339933;">:</span>self selector<span style="color: #339933;">:</span>@selector<span style="color: #009900;">&#40;</span>run<span style="color: #009900;">&#41;</span> object<span style="color: #339933;">:</span>nil<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#91;</span>ticketsThreadtwo setName<span style="color: #339933;">:</span>@<span style="color: #ff0000;">&quot;Thread-2&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#91;</span>ticketsThreadtwo start<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
     <span style="color: #666666; font-style: italic;">//[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];</span>
      <span style="color: #666666; font-style: italic;">// Override point for customization after application launch</span>
     <span style="color: #009900;">&#91;</span>window makeKeyAndVisible<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
&nbsp;
 <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>run<span style="color: #009900;">&#123;</span>
     <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>TRUE<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     	<span style="color: #666666; font-style: italic;">// 上锁</span>
         <span style="color: #009900;">&#91;</span>ticketsCondition lock<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
         <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>tickets <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
             <span style="color: #009900;">&#91;</span>NSThread sleepForTimeInterval<span style="color: #339933;">:</span><span style="color:#800080;">0.5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
             count <span style="color: #339933;">=</span> <span style="color: #0000dd;">100</span> <span style="color: #339933;">-</span> tickets<span style="color: #339933;">;</span>
             NSLog<span style="color: #009900;">&#40;</span>@<span style="color: #ff0000;">&quot;当前票数是:%d,售出:%d,线程名:%@&quot;</span><span style="color: #339933;">,</span>tickets<span style="color: #339933;">,</span>count<span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>NSThread currentThread<span style="color: #009900;">&#93;</span> name<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
             tickets<span style="color: #339933;">--;</span>
         <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
             <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
         <span style="color: #009900;">&#91;</span>ticketsCondition unlock<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>dealloc <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#91;</span>ticketsThreadone release<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#91;</span>ticketsThreadtwo release<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#91;</span>ticketsCondition release<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
     <span style="color: #009900;">&#91;</span>window release<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#91;</span>super dealloc<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
@end</pre></div></div>

<p><strong>三、线程的交互</strong><br />
线程在运行过程中，可能需要与其它线程进行通信，如在主线程中修改界面等等，可以使用如下接口：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>performSelectorOnMainThread<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>SEL<span style="color: #009900;">&#41;</span>aSelector withObject<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>arg waitUntilDone<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>BOOL<span style="color: #009900;">&#41;</span>wait</pre></div></div>

<p>由于在本过程中，可能需要释放一些资源，则需要使用NSAutoreleasePool来进行管理,如：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>startTheBackgroundJob <span style="color: #009900;">&#123;</span>
    NSAutoreleasePool <span style="color: #339933;">*</span>pool <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>NSAutoreleasePool alloc<span style="color: #009900;">&#93;</span> init<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// to do something in your thread job</span>
    ...
    <span style="color: #009900;">&#91;</span>self performSelectorOnMainThread<span style="color: #339933;">:</span>@selector<span style="color: #009900;">&#40;</span>makeMyProgressBarMoving<span style="color: #009900;">&#41;</span> withObject<span style="color: #339933;">:</span>nil waitUntilDone<span style="color: #339933;">:</span>NO<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#91;</span>pool release<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>如果你什么都不考虑，在线程函数内调用 autorelease 、那么会出现下面的错误：<br />
<code>NSAutoReleaseNoPool(): Object 0x********* of class NSConreteData autoreleased with no pool in place ….</code></p>
<p><strong>四、关于线程池，大家可以查看NSOperation的相关资料。</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.voland.com.cn/iphone-in-the-multi-threaded-programming/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>sqlite3中针对objective-c的持久层框架</title>
		<link>http://www.voland.com.cn/persistent-objects-for-cocoa-cocoa-touch-that-use-sqlite3</link>
		<comments>http://www.voland.com.cn/persistent-objects-for-cocoa-cocoa-touch-that-use-sqlite3#comments</comments>
		<pubDate>Mon, 19 Apr 2010 02:51:04 +0000</pubDate>
		<dc:creator>Nortan</dc:creator>
				<category><![CDATA[Objective-c]]></category>
		<category><![CDATA[软件开发]]></category>
		<category><![CDATA[Iphone]]></category>
		<category><![CDATA[sqlite3]]></category>
		<category><![CDATA[持久层框架]]></category>
		<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://www.voland.com.cn/?p=383</guid>
		<description><![CDATA[Cocoa与Cocoa Touch（主要是指iphone，ipad,ipod等等）都对sqlite3提供了良好的支持，但objective-c语言中的sqlite3持久层框架确没有java中那样丰富与强大、灵活，直到我发现sqlitepersistentobjects这个框架。这个框架利用反射机制与value objects的完美结合,对sqlite3进行了非常轻量级的对象封装（这点有点像我2007年时写的一个java持久层框架，过些时候我整理一下，开源出来），使用者在编程过程中，基本不用再写sql语句，也不需要再手工生成sqlite3数据库文件。你所关心的，就是值对象继承一个核心类，然后添加值对像的属性，实际他就是一款Objective-C实现的ActiveRecord&#8211;Rails社区红透半边天的ORM模式。 一、sqlite persistent objects的下载与安装 sqlitepersistentobjects是google code上的一个开源项目，大家可以出下载它，本项目地址是：http://code.google.com/p/sqlitepersistentobjects/ 下载后，得到的源码包src内的源文件，copy到自己的项目中，建议最好新建一个分类（new group）见注1，与其它源码分离开来，便于管理.然后再工程中添加”libsqlite3.dylib”库。见注2 二、sqlite persistent objects的使用 在使用前，我们先了解一下他是怎么工作的，首先要创建一个Objective-C类，这个类只需要继承自“SQLitePersistentObject”，然后，添加您想要持久化的Objective-C 2.0属性。当这个类被实例化后，调用”save”方法，会自动保存到数据库中。每一个“SQLitePersistentObject”的子类都会拥有数据库中对应的一张表。每一个属性（不包括集合类型，像，NSDictionary,NSArray,NSSet或这些类的可变类型）都会持久化到表中相应字段中。属性中如果含有指针对象是”SQLitePersistentObject”实例，也将会在关联表里新添条记录。注意，这里的属性必须是实现“NSCoding”的对象，如NSDate,NSString,NSData,NSMutableData,NSNumber与NSObject，而c-string，void pointer, strutct 或 union它们不是对象，将不会支持。但所有的数字(int,float等)都会保存在应的字段上。 我们简单要来做一个例子，定义一个类Person，继承SQLitePersistentObject，把这个类的属性保存到数据库中，代码如下： #import #import “SQLitePersistentObject.h” //Person.m的生成略，大家都应该知道的 @interface Person : SQLitePersistentObject &#123; NSString *iName; int iAge; &#125; @property &#40;nonatomic, retain&#41; NSString * iName; @property &#40;nonatomic&#41; int iAge; @end 生成类文件后，在其它使用的类中创建实例： Person *person = &#91;&#91;Person alloc&#93; init&#93;; person.iName = @”leeguoli”; [...]]]></description>
			<content:encoded><![CDATA[<p>Cocoa与Cocoa Touch（主要是指iphone，ipad,ipod等等）都对sqlite3提供了良好的支持，但objective-c语言中的sqlite3持久层框架确没有java中那样丰富与强大、灵活，直到我发现sqlitepersistentobjects这个框架。这个框架利用反射机制与value objects的完美结合,对sqlite3进行了非常轻量级的对象封装（这点有点像我2007年时写的一个java持久层框架，过些时候我整理一下，开源出来），使用者在编程过程中，基本不用再写sql语句，也不需要再手工生成sqlite3数据库文件。你所关心的，就是值对象继承一个核心类，然后添加值对像的属性，实际他就是一款Objective-C实现的ActiveRecord&#8211;Rails社区红透半边天的ORM模式。<span id="more-383"></span><br />
<br/></p>
<p><strong>一、sqlite persistent objects的下载与安装</strong></p>
<p>sqlitepersistentobjects是google code上的一个开源项目，大家可以出下载它，本项目地址是：<a href="http://code.google.com/p/sqlitepersistentobjects/">http://code.google.com/p/sqlitepersistentobjects/</a></p>
<p>下载后，得到的源码包src内的源文件，copy到自己的项目中，建议最好新建一个分类（new group）<a href="#a1">见注1</a>，与其它源码分离开来，便于管理.然后再工程中添加”libsqlite3.dylib”库。<a href="#a2">见注2</a></p>
<p><strong>二、sqlite persistent objects的使用</strong></p>
<p>在使用前，我们先了解一下他是怎么工作的，首先要创建一个Objective-C类，这个类只需要继承自“SQLitePersistentObject”，然后，添加您想要持久化的Objective-C 2.0属性。当这个类被实例化后，调用”save”方法，会自动保存到数据库中。每一个“SQLitePersistentObject”的子类都会拥有数据库中对应的一张表。每一个属性（不包括集合类型，像，NSDictionary,NSArray,NSSet或这些类的可变类型）都会持久化到表中相应字段中。属性中如果含有指针对象是”SQLitePersistentObject”实例，也将会在关联表里新添条记录。注意，这里的属性必须是实现“NSCoding”的对象，如NSDate,NSString,NSData,NSMutableData,NSNumber与NSObject，而c-string，void pointer, strutct 或 union它们不是对象，将不会支持。但所有的数字(int,float等)都会保存在应的字段上。</p>
<p>我们简单要来做一个例子，定义一个类Person，继承SQLitePersistentObject，把这个类的属性保存到数据库中，代码如下：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#import</span>
<span style="color: #339933;">#import “SQLitePersistentObject.h”</span>
<span style="color: #666666; font-style: italic;">//Person.m的生成略，大家都应该知道的</span>
@interface Person <span style="color: #339933;">:</span> SQLitePersistentObject <span style="color: #009900;">&#123;</span>
NSString <span style="color: #339933;">*</span>iName<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> iAge<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
@property <span style="color: #009900;">&#40;</span>nonatomic<span style="color: #339933;">,</span> retain<span style="color: #009900;">&#41;</span> NSString <span style="color: #339933;">*</span> iName<span style="color: #339933;">;</span>
@property <span style="color: #009900;">&#40;</span>nonatomic<span style="color: #009900;">&#41;</span> <span style="color: #993333;">int</span> iAge<span style="color: #339933;">;</span>
@end</pre></div></div>

<p>生成类文件后，在其它使用的类中创建实例：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">Person <span style="color: #339933;">*</span>person <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>Person alloc<span style="color: #009900;">&#93;</span> init<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
person.<span style="color: #202020;">iName</span> <span style="color: #339933;">=</span> @”leeguoli”<span style="color: #339933;">;</span>
person.<span style="color: #202020;">iAge</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">20</span><span style="color: #339933;">;</span></pre></div></div>

<p>调用”save”方法，保存到数据库中：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #009900;">&#91;</span>person save<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>加载表中的数据也相当的简单。任何一个”Person”类都有多个提供查询的类方法。例如，你可以查询所有姓“lee”的人：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">NSArray <span style="color: #339933;">*</span>people <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>Person findByIName<span style="color: #339933;">:</span>@<span style="color: #ff0000;">&quot;'lee%'&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//这里实际有点像like语句，where iName like 'lee%'</span></pre></div></div>

<p>或者更灵活的使用ＳＱＬ查询条件：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">Person <span style="color: #339933;">*</span>leeguoli <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>Person findFirstByCriteria<span style="color: #339933;">:</span>@<span style="color: #ff0000;">&quot;WHERE i_name = 'leeguoli' AND i_age = 20];</span></pre></div></div>

<p>注意大写字母区分的情况用下划线代替（ActiveRecord的惯例）。”findByCriteria:”返回一个NSArray数组，”findFirstByCriteria:”返回一个对象。</p>
<p>如果要更新，当然这里先要查询出来需要更新的对象，然后修改对象的值并保存就可以了，这里不再详写。</p>
<p><br/><br />
=========================================================</p>
<p><a name="a1">注1</a>、Xcode界面中，右键点击Classes目录，选择“Add &gt; New Group”，命名为SQLitePersistentObject。右键点击新建的 “SQLitePersistentObject”目录，选择“Add &gt; Existing Files”，将解压缩出来的src目录下所有文件导入。</p>
<p><a name="a2">注2</a>、请右键点击左部菜单中的”Frameworks”目录，选择 “Add &gt; Existing Frameworks…”，然后在本地目录中选择“/Developer/Platforms/iPhoneOS.platform/Developer /SDKs/iPhoneOS3.1.2.sdk/usr/lib/”，找到“libsqlite3.0.dylib”文件并双击。一个弹出窗口将出现，点击其中“Add”按钮将库添加入项目中。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voland.com.cn/persistent-objects-for-cocoa-cocoa-touch-that-use-sqlite3/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>国际化Iphone的应用程序名称</title>
		<link>http://www.voland.com.cn/iphone-international-application-name</link>
		<comments>http://www.voland.com.cn/iphone-international-application-name#comments</comments>
		<pubDate>Tue, 06 Apr 2010 05:00:07 +0000</pubDate>
		<dc:creator>Nortan</dc:creator>
				<category><![CDATA[Objective-c]]></category>
		<category><![CDATA[Iphone]]></category>
		<category><![CDATA[MultiLanguge]]></category>
		<category><![CDATA[国际化]]></category>
		<category><![CDATA[多语言]]></category>

		<guid isPermaLink="false">http://www.voland.com.cn/?p=293</guid>
		<description><![CDATA[iPhone应用程序名的国际化、本地化，比如，方都化工网出品的化工贸易行情软件i16ds，在英文环境下显示i16ds,在中文语言环境中显示“方都化工”]]></description>
			<content:encoded><![CDATA[<div id="attachment_297" class="wp-caption aligncenter" style="width: 335px"><a href="http://www.voland.com.cn/iphone-international-application-name"><img src="http://www.voland.com.cn/wp-content/uploads/2010/04/i16ds.png" alt="" title="iphone应用程序名的国际化" width="325" height="115" class="size-full wp-image-297" /></a><p class="wp-caption-text">iphone应用程序名的国际化</p></div>
<p>在前面的文章，有一文章叫做《<a href="http://www.voland.com.cn/iphone-use-nslocalizedstring-internationalization">iPhone 中使用NSLocalizedString实现国际化</a>》，它主要介绍了在应用程序中，字符等信息的国际化（本地化），很多情况下，我们希望应用程序的名称也能够做到国际化，让不同国家的人一看程序名，基本就能了解到本程序的用途，比如，方都化工网出品的化工贸易行情软件i16ds，如果看英文名，我们是没有办法了解它的用途的，除非对方都化工网非常熟悉。</p>
<p>iPhone应用程序名的国际化，实际还是比较简单的，笔者这里有一种比较简单、快速且不影响原有程序（不需要修改任何原代码）的方法。<span id="more-293"></span></p>
<p>第一步：在项目的Resources里新增一个strings文件名称叫InfoPlist.strings</p>
<p>第二步：察看此文件的Info，在General选项卡里Make file Localizable，然后回到General，Add Localization名字叫zh_CN，注意这里的zh_CN，如果要增加其它语言，直接增加对应国家的名字就成。</p>
<p>第三步：给zh_CN里新增两行文本，实际上只要CFBundleDisplayName就可以</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">   CFBundleName<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;中文名&quot;</span><span style="color: #339933;">;</span>   
   CFBundleDisplayName<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;中文名&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>第四步：给英文及其它同样增加这两行文本</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">   CFBundleName<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;EnglishName&quot;</span><span style="color: #339933;">;</span>   
   CFBundleDisplayName<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;EnglishName&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>重新编译一下这个工程，然后在模拟器中切换一下语言，看看效果，是不是已经可以了？</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voland.com.cn/iphone-international-application-name/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>使用NSTimer与UIView的动画，实现飘雪效果</title>
		<link>http://www.voland.com.cn/the-use-of-a-simple-nstimer-with-uiview-animation-effects-to-achieve-snowing</link>
		<comments>http://www.voland.com.cn/the-use-of-a-simple-nstimer-with-uiview-animation-effects-to-achieve-snowing#comments</comments>
		<pubDate>Mon, 22 Mar 2010 05:05:24 +0000</pubDate>
		<dc:creator>Nortan</dc:creator>
				<category><![CDATA[Objective-c]]></category>
		<category><![CDATA[Iphone]]></category>
		<category><![CDATA[NSTimer]]></category>
		<category><![CDATA[UIImageView]]></category>

		<guid isPermaLink="false">http://www.voland.com.cn/?p=257</guid>
		<description><![CDATA[使用NSTimer与UIImageView的简单动画，实现飘雪效果]]></description>
			<content:encoded><![CDATA[<p>使用NSTimer与iphone的简单动画，实现飘雪效果，这理原理比较简单，就是定时生成一定的雪花图片，然后使用动画的方式向下漂落（我在其它论坛，看到使用path的方式实现的一个云漂来漂去的效果，实际也可以用那种方式实现，这实际就是前面说的动画效果的两种应用）。所以，我们可以在viewDidLoad事件中，增加一个图片及定时器并启动,这里的pic请在头文件中定义。</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>viewDidLoad<span style="color: #009900;">&#123;</span>
 <span style="color: #009900;">&#91;</span>super viewDidLoad<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
 self.<span style="color: #202020;">pic</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>UIImage imageNamed<span style="color: #339933;">:</span>@<span style="color: #ff0000;">&quot;snow.png&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//初始化图片</span>
 <span style="color: #666666; font-style: italic;">//启动定时器，实现飘雪效果</span>
 <span style="color: #009900;">&#91;</span>NSTimer scheduledTimerWithTimeInterval<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color:#800080;">0.2</span><span style="color: #009900;">&#41;</span> target<span style="color: #339933;">:</span>self selector<span style="color: #339933;">:</span>@selector<span style="color: #009900;">&#40;</span>ontime<span style="color: #009900;">&#41;</span> userInfo<span style="color: #339933;">:</span>nil repeats<span style="color: #339933;">:</span>YES<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>然后再实现定时器定时调用的ontime方法：<span id="more-257"></span></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>ontime<span style="color: #009900;">&#123;</span>
 UIImageView <span style="color: #339933;">*</span>view <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>UIImageView alloc<span style="color: #009900;">&#93;</span> initWithImage<span style="color: #339933;">:</span>pic<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//声明一个UIImageView对象，用来添加图片</span>
 view.<span style="color: #202020;">alpha</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.5</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//设置该view的alpha为0.5，半透明的</span>
 <span style="color: #993333;">int</span> x <span style="color: #339933;">=</span> round<span style="color: #009900;">&#40;</span>random<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">%</span><span style="color:#800080;">320</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//随机得到该图片的x坐标</span>
 <span style="color: #993333;">int</span> y <span style="color: #339933;">=</span> round<span style="color: #009900;">&#40;</span>random<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">%</span><span style="color:#800080;">320</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//这个是该图片移动的最后坐标x轴的</span>
 <span style="color: #993333;">int</span> s <span style="color: #339933;">=</span> round<span style="color: #009900;">&#40;</span>random<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">%</span><span style="color:#800080;">15</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000dd;">10</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//这个是定义雪花图片的大小</span>
 <span style="color: #993333;">int</span> sp <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">/</span>round<span style="color: #009900;">&#40;</span>random<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">%</span><span style="color:#800080;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//这个是速度</span>
 view.<span style="color: #202020;">frame</span> <span style="color: #339933;">=</span> CGRectMake<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">50</span><span style="color: #339933;">,</span> s<span style="color: #339933;">,</span> s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//雪花开始的大小和位置</span>
 <span style="color: #009900;">&#91;</span>self.<span style="color: #202020;">view</span> addSubview<span style="color: #339933;">:</span>view<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//添加该view</span>
 <span style="color: #009900;">&#91;</span>UIView beginAnimations<span style="color: #339933;">:</span>nil context<span style="color: #339933;">:</span>view<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//开始动画</span>
 <span style="color: #009900;">&#91;</span>UIView setAnimationDuration<span style="color: #339933;">:</span><span style="color: #0000dd;">10</span><span style="color: #339933;">*</span>sp<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//设定速度</span>
 view.<span style="color: #202020;">frame</span> <span style="color: #339933;">=</span> CGRectMake<span style="color: #009900;">&#40;</span>y<span style="color: #339933;">,</span> <span style="color: #0000dd;">500</span><span style="color: #339933;">,</span> s<span style="color: #339933;">,</span> s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//设定该雪花最后的消失坐标</span>
 <span style="color: #009900;">&#91;</span>UIView setAnimationDelegate<span style="color: #339933;">:</span>self<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#91;</span>UIView commitAnimations<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.voland.com.cn/the-use-of-a-simple-nstimer-with-uiview-animation-effects-to-achieve-snowing/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Iphone中的网络请求</title>
		<link>http://www.voland.com.cn/httpconnection-for-iphone-in-the-network-request</link>
		<comments>http://www.voland.com.cn/httpconnection-for-iphone-in-the-network-request#comments</comments>
		<pubDate>Tue, 16 Mar 2010 03:49:07 +0000</pubDate>
		<dc:creator>Nortan</dc:creator>
				<category><![CDATA[Objective-c]]></category>
		<category><![CDATA[Iphone]]></category>
		<category><![CDATA[网络连接]]></category>

		<guid isPermaLink="false">http://www.voland.com.cn/?p=219</guid>
		<description><![CDATA[iphone的网络编程]]></description>
			<content:encoded><![CDATA[<p>一、简单的get请求</p>
<p>网络编程是我们经常遇到的，在IPhone中，SDK提供了良好的接口，主要使用的类有NSURL，NSMutableURLRequest，NSURLConnection等等。一般情况下建议使用异步接收数据的方式来请求网络连接，这种网络连接分为两步，第一步是新建NSURLConnection对象后，直接调用它的start方法来连接网络。第二步是使用delegate方式来接收数据，这里给一个常用的写法：<span id="more-219"></span></p>
<p>网络请求部分：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">NSString <span style="color: #339933;">*</span>urlString <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>NSString stringWithFormat<span style="color: #339933;">:</span>@<span style="color: #ff0000;">&quot;http://www.voland.com.cn:8080/weather/weatherServlet?city=%@&quot;</span><span style="color: #339933;">,</span>kcityID<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
NSURL <span style="color: #339933;">*</span>url <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>NSURL URLWithString<span style="color: #339933;">:</span>urlString<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
NSMutableURLRequest <span style="color: #339933;">*</span>request <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>NSMutableURLRequest  requestWithURL<span style="color: #339933;">:</span>url<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
NSURLConnection <span style="color: #339933;">*</span>aUrlConnection <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>NSURLConnection alloc<span style="color: #009900;">&#93;</span> initWithRequest<span style="color: #339933;">:</span>request delegate<span style="color: #339933;">:</span>self startImmediately<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
self.<span style="color: #202020;">urlConnection</span> <span style="color: #339933;">=</span> aUrlConnection<span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//这里的urlConnection在头文件中定义的变量</span>
<span style="color: #009900;">&#91;</span>self.<span style="color: #202020;">urlConnection</span> start<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//开始连接网络</span>
<span style="color: #009900;">&#91;</span>aUrlConnection release<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>UIApplication sharedApplication<span style="color: #009900;">&#93;</span> setNetworkActivityIndicatorVisible<span style="color: #339933;">:</span>YES<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>接收数据部分,接收到的数据主要是在这里处理</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>connection<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>NSURLConnection <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>connection didReceiveResponse<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>NSURLResponse <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>response  <span style="color: #009900;">&#123;</span>
NSLog<span style="color: #009900;">&#40;</span>@<span style="color: #ff0000;">&quot;接收完响应:%@&quot;</span><span style="color: #339933;">,</span>response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>connection<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>NSURLConnection <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>connection didReceiveData<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>NSData <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>data  <span style="color: #009900;">&#123;</span>
NSLog<span style="color: #009900;">&#40;</span>@<span style="color: #ff0000;">&quot;接收完数据:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>connection<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>NSURLConnection <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>connection didFailWithError<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>NSError <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>error  <span style="color: #009900;">&#123;</span>
NSLog<span style="color: #009900;">&#40;</span>@<span style="color: #ff0000;">&quot;数据接收错误:%@&quot;</span><span style="color: #339933;">,</span>error<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>connectionDidFinishLoading<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>NSURLConnection <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>connection  <span style="color: #009900;">&#123;</span>
NSLog<span style="color: #009900;">&#40;</span>@<span style="color: #ff0000;">&quot;连接完成:%@&quot;</span><span style="color: #339933;">,</span>connection<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>UIApplication sharedApplication<span style="color: #009900;">&#93;</span> setNetworkActivityIndicatorVisible<span style="color: #339933;">:</span>NO<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>二、Post请求<br />
进行post请求，主要是设置好NSMutableURLRequest对象，在get请求中，我们都使用了默认的，实际这些request内容都可以设置的。设置好后，其它与get方式同：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">NSString <span style="color: #339933;">*</span>content<span style="color: #339933;">=</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>NSString alloc<span style="color: #009900;">&#93;</span> initWithData<span style="color: #339933;">:</span> data encoding<span style="color: #339933;">:</span> NSUTF8StringEncoding<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span>request setHTTPBody<span style="color: #339933;">:</span> content<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#91;</span>request setHTTPMethod<span style="color: #339933;">:</span> @<span style="color: #ff0000;">&quot;POST&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#91;</span>request setValue<span style="color: #339933;">:</span>@<span style="color: #ff0000;">&quot;Close&quot;</span> forHTTPHeaderField<span style="color: #339933;">:</span>@<span style="color: #ff0000;">&quot;Connection&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#91;</span>request setValue<span style="color: #339933;">:</span>@<span style="color: #ff0000;">&quot;www.voland.com.cn&quot;</span> forHTTPHeaderField<span style="color: #339933;">:</span>@<span style="color: #ff0000;">&quot;Host&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#91;</span>request setValue<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>NSString stirngWithFormat@<span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span>content length<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> forHTTPHeaderField<span style="color: #339933;">:</span>@<span style="color: #ff0000;">&quot;Content-Length&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.voland.com.cn/httpconnection-for-iphone-in-the-network-request/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>iPhone中使用NSLocalizedString实现国际化</title>
		<link>http://www.voland.com.cn/iphone-use-nslocalizedstring-internationalization</link>
		<comments>http://www.voland.com.cn/iphone-use-nslocalizedstring-internationalization#comments</comments>
		<pubDate>Mon, 08 Feb 2010 03:41:11 +0000</pubDate>
		<dc:creator>Nortan</dc:creator>
				<category><![CDATA[Objective-c]]></category>
		<category><![CDATA[MultiLanguge]]></category>
		<category><![CDATA[NSLocalizedString]]></category>
		<category><![CDATA[国际化]]></category>
		<category><![CDATA[多语言]]></category>

		<guid isPermaLink="false">http://127.0.0.1/~nortan/?p=189</guid>
		<description><![CDATA[一、获取系统所支持的国际化信息 在国际化之前,你可以在iphone中的”设置->通用->多语言环境->语言”中来查看你的iphone支持哪些语言,当然也可以写一段代码测试一下你的iphone都支持哪些语言.测试代码如下: NSUserDefaults *defaults = &#91;NSUserDefaults standardUserDefaults&#93;; NSArray *languages = &#91;defaults objectForKey:@&#34;AppleLanguages&#34;&#93;; NSLog&#40;@&#34;%@&#34;, languages&#41;; 注:NSUserDefaults类用来取得用户人默认信息. 二、在Xcode中建立多语言文档 1.在Resources分类下新建文档(右鍵/Add/New File&#8230;) 2.在模板对话框中选择Other,然后再选择Strings File 3.将文件保存名设置为Localizable.strings 4.在Localizable.strings 文件上按右键并选择 Get Info 5.点击信息界面的Make File Localizable,然后再将Tab标签切换到General 6.输入新的语言名称 zh 後按 Add,些时有English与zh两种语言,你还可以增加其它语言. 三、在源代码中使用NSLocalizedString来引用国际化文件 //括号里第一个参数是要显示的内容,与各Localizable.strings中的id对应 //第二个是对第一个参数的注释,一般可以为空串 &#91;_alertView setTitle:NSLocalizedString&#40;@&#34;Submitted successfully&#34;,@&#34;&#34;&#41;&#93;; 四、使用Terminal的genstrings命令进行生成资源文件 打开Terminal,然后cd到工程所在的目录,然后使用genstrings来生成自动从源代码中生成资源文件. 例如，项目的目录为：/user/project/test01,则命令如下： genstrings -o English.lproj ./classes/*.m genstrings -o zh.lproj ./classes/*.m 五、编辑各Localizable.strings文件 从第四步中得到了与代码对应的资源文件,最后我们需要对这些资源文件翻译成对应的语言就可以了.如在Localizable.strings(zh)中,把等号后的文字进行编译成中文. &#34;Submitted successfully&#34; = &#34;提交成功&#34; [...]]]></description>
			<content:encoded><![CDATA[<p><strong>一、获取系统所支持的国际化信息</strong><br />
在国际化之前,你可以在iphone中的”设置->通用->多语言环境->语言”中来查看你的iphone支持哪些语言,当然也可以写一段代码测试一下你的iphone都支持哪些语言.测试代码如下:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">NSUserDefaults <span style="color: #339933;">*</span>defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>NSUserDefaults standardUserDefaults<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
NSArray <span style="color: #339933;">*</span>languages <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>defaults objectForKey<span style="color: #339933;">:</span>@<span style="color: #ff0000;">&quot;AppleLanguages&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
NSLog<span style="color: #009900;">&#40;</span>@<span style="color: #ff0000;">&quot;%@&quot;</span><span style="color: #339933;">,</span> languages<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>注:NSUserDefaults类用来取得用户人默认信息.<span id="more-189"></span></p>
<p><strong>二、在Xcode中建立多语言文档</strong><br />
1.在Resources分类下新建文档(右鍵/Add/New File&#8230;)<br />
2.在模板对话框中选择Other,然后再选择Strings File<br />
3.将文件保存名设置为Localizable.strings<br />
4.在Localizable.strings 文件上按右键并选择 Get Info<br />
5.点击信息界面的Make File Localizable,然后再将Tab标签切换到General<br />
6.输入新的语言名称 zh 後按 Add,些时有English与zh两种语言,你还可以增加其它语言.</p>
<p><strong>三、在源代码中使用NSLocalizedString来引用国际化文件</strong></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//括号里第一个参数是要显示的内容,与各Localizable.strings中的id对应</span>
<span style="color: #666666; font-style: italic;">//第二个是对第一个参数的注释,一般可以为空串</span>
<span style="color: #009900;">&#91;</span>_alertView setTitle<span style="color: #339933;">:</span>NSLocalizedString<span style="color: #009900;">&#40;</span>@<span style="color: #ff0000;">&quot;Submitted successfully&quot;</span><span style="color: #339933;">,</span>@<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>四、使用Terminal的genstrings命令进行生成资源文件</strong><br />
打开Terminal,然后cd到工程所在的目录,然后使用genstrings来生成自动从源代码中生成资源文件.<br />
例如，项目的目录为：/user/project/test01,则命令如下：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">genstrings <span style="color: #339933;">-</span>o English.<span style="color: #202020;">lproj</span>  .<span style="color: #339933;">/</span>classes<span style="color: #808080; font-style: italic;">/*.m
genstrings -o zh.lproj  ./classes/*.m</span></pre></div></div>

<p><strong>五、编辑各Localizable.strings文件</strong><br />
从第四步中得到了与代码对应的资源文件,最后我们需要对这些资源文件翻译成对应的语言就可以了.如在Localizable.strings(zh)中,把等号后的文字进行编译成中文.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #ff0000;">&quot;Submitted successfully&quot;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;提交成功&quot;</span></pre></div></div>

<p>重新编译整个工程后,就会在不同的语言环境下得到相应的语言显示.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voland.com.cn/iphone-use-nslocalizedstring-internationalization/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>iphone的动画效果类型及实现方法</title>
		<link>http://www.voland.com.cn/the-effect-of-the-type-and-implement-for-iphone</link>
		<comments>http://www.voland.com.cn/the-effect-of-the-type-and-implement-for-iphone#comments</comments>
		<pubDate>Fri, 05 Feb 2010 09:19:41 +0000</pubDate>
		<dc:creator>Nortan</dc:creator>
				<category><![CDATA[Objective-c]]></category>
		<category><![CDATA[CATransition]]></category>
		<category><![CDATA[Iphone]]></category>

		<guid isPermaLink="false">http://127.0.0.1/~nortan/?p=186</guid>
		<description><![CDATA[实现iphone漂亮的动画效果主要有两种方法，一种是UIView层面的，一种是使用CATransition进行更低层次的控制， 第一种是UIView，UIView方式可能在低层也是使用CATransition进行了封装，它只能用于一些简单的、常用的效果展现，这里写一个常用的示例代码，供大家参考。 &#91;UIView beginAnimations:@&#34;Curl&#34;context:nil&#93;;//动画开始 &#91;UIView setAnimationDuration:0.75&#93;; &#91;UIView setAnimationDelegate:self&#93;; &#91;UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:myview cache:YES&#93;; &#91;myview removeFromSuperview&#93;; &#91;UIView commitAnimations&#93;; 第二种方式相对复杂一些，但如果更好的进行控制，还是使用这种方法吧，基本使用方法可以看一下如下例子： CATransition *animation = &#91;CATransition animation&#93;; &#91;animation setDuration:1.25f&#93;; &#91;animation setTimingFunction:&#91;CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn&#93;&#93;; &#91;animation setType:kCATransitionReveal&#93;; &#91;animation setSubtype: kCATransitionFromBottom&#93;; &#91;self.view.layer addAnimation:animation forKey:@&#34;Reveal&#34;&#93;; 这里使用了setType与setSubtype组合，这使用个比较保险，因为他的参数就是官方API里定义的，他们的参数说明可以参考如下： setType:可以返回四种类型： kCATransitionFade淡出 kCATransitionMoveIn覆盖原图 kCATransitionPush推出 kCATransitionReveal底部显出来 setSubtype:也可以有四种类型： kCATransitionFromRight； kCATransitionFromLeft&#40;默认值&#41; kCATransitionFromTop； kCATransitionFromBottom 还有一种设置动画类型的方法，不用setSubtype，只用setType &#91;animation setType:@&#34;suckEffect&#34;&#93;; 这里的suckEffect就是效果名称，可以用的效果主要有： pageCurl 向上翻一页 pageUnCurl 向下翻一页 rippleEffect [...]]]></description>
			<content:encoded><![CDATA[<p>实现iphone漂亮的动画效果主要有两种方法，一种是UIView层面的，一种是使用CATransition进行更低层次的控制，</p>
<p>第一种是UIView，UIView方式可能在低层也是使用CATransition进行了封装，它只能用于一些简单的、常用的效果展现，这里写一个常用的示例代码，供大家参考。</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #009900;">&#91;</span>UIView beginAnimations<span style="color: #339933;">:</span>@<span style="color: #ff0000;">&quot;Curl&quot;</span>context<span style="color: #339933;">:</span>nil<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//动画开始</span>
<span style="color: #009900;">&#91;</span>UIView setAnimationDuration<span style="color: #339933;">:</span><span style="color:#800080;">0.75</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span>UIView setAnimationDelegate<span style="color: #339933;">:</span>self<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span>UIView setAnimationTransition<span style="color: #339933;">:</span>UIViewAnimationTransitionCurlUp forView<span style="color: #339933;">:</span>myview cache<span style="color: #339933;">:</span>YES<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span>myview removeFromSuperview<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span>UIView commitAnimations<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>第二种方式相对复杂一些，但如果更好的进行控制，还是使用这种方法吧，基本使用方法可以看一下如下例子：<span id="more-186"></span></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">CATransition <span style="color: #339933;">*</span>animation <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>CATransition animation<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span>animation setDuration<span style="color: #339933;">:</span><span style="color:#800080;">1.25f</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span>animation setTimingFunction<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>CAMediaTimingFunction
functionWithName<span style="color: #339933;">:</span>kCAMediaTimingFunctionEaseIn<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span>animation setType<span style="color: #339933;">:</span>kCATransitionReveal<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span>animation setSubtype<span style="color: #339933;">:</span> kCATransitionFromBottom<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span>self.<span style="color: #202020;">view</span>.<span style="color: #202020;">layer</span> addAnimation<span style="color: #339933;">:</span>animation forKey<span style="color: #339933;">:</span>@<span style="color: #ff0000;">&quot;Reveal&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>这里使用了setType与setSubtype组合，这使用个比较保险，因为他的参数就是官方API里定义的，他们的参数说明可以参考如下：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">setType<span style="color: #339933;">:</span>可以返回四种类型：
kCATransitionFade淡出
kCATransitionMoveIn覆盖原图
kCATransitionPush推出
kCATransitionReveal底部显出来
setSubtype<span style="color: #339933;">:</span>也可以有四种类型：
kCATransitionFromRight；
kCATransitionFromLeft<span style="color: #009900;">&#40;</span>默认值<span style="color: #009900;">&#41;</span>
kCATransitionFromTop；
kCATransitionFromBottom</pre></div></div>

<p>还有一种设置动画类型的方法，不用setSubtype，只用setType</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #009900;">&#91;</span>animation setType<span style="color: #339933;">:</span>@<span style="color: #ff0000;">&quot;suckEffect&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>这里的suckEffect就是效果名称，可以用的效果主要有：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">pageCurl   向上翻一页
pageUnCurl 向下翻一页
rippleEffect 滴水效果
suckEffect 收缩效果，如一块布被抽走
cube 立方体效果
oglFlip 上下翻转效果</pre></div></div>

<p>最后再给出一种常用代码供大家参考。</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Curl the image up or down</span>
CATransition <span style="color: #339933;">*</span>animation <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>CATransition animation<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span>animation setDuration<span style="color: #339933;">:</span><span style="color:#800080;">0.35</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span>animation setTimingFunction<span style="color: #339933;">:</span>UIViewAnimationCurveEaseInOut<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>curled<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//animation.type = @&quot;mapCurl&quot;;</span>
animation.<span style="color: #202020;">type</span> <span style="color: #339933;">=</span> @<span style="color: #ff0000;">&quot;pageCurl&quot;</span><span style="color: #339933;">;</span>
animation.<span style="color: #202020;">fillMode</span> <span style="color: #339933;">=</span> kCAFillModeForwards<span style="color: #339933;">;</span>
animation.<span style="color: #202020;">endProgress</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.99</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//animation.type = @&quot;mapUnCurl&quot;;</span>
animation.<span style="color: #202020;">type</span> <span style="color: #339933;">=</span> @<span style="color: #ff0000;">&quot;pageUnCurl&quot;</span><span style="color: #339933;">;</span>
animation.<span style="color: #202020;">fillMode</span> <span style="color: #339933;">=</span> kCAFillModeBackwards<span style="color: #339933;">;</span>
animation.<span style="color: #202020;">startProgress</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.01</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#91;</span>animation setRemovedOnCompletion<span style="color: #339933;">:</span>NO<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span>view exchangeSubviewAtIndex<span style="color: #339933;">:</span><span style="color: #0000dd;">0</span> withSubviewAtIndex<span style="color: #339933;">:</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span>view addAnimation<span style="color: #339933;">:</span>animation forKey<span style="color: #ff0000;">&quot;pageCurlAnimation&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Disable user interaction where necessary</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>curled<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
curled <span style="color: #339933;">=</span> <span style="color: #339933;">!</span>curled<span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.voland.com.cn/the-effect-of-the-type-and-implement-for-iphone/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>UITextField增加textDidChange回调功能</title>
		<link>http://www.voland.com.cn/uitextfield_increase_textdidchange_callback_function</link>
		<comments>http://www.voland.com.cn/uitextfield_increase_textdidchange_callback_function#comments</comments>
		<pubDate>Wed, 20 Jan 2010 07:38:38 +0000</pubDate>
		<dc:creator>Nortan</dc:creator>
				<category><![CDATA[Objective-c]]></category>
		<category><![CDATA[UITextField]]></category>

		<guid isPermaLink="false">http://127.0.0.1/~nortan/?p=134</guid>
		<description><![CDATA[在UISearchBar中,当输入信息改变时,它就会调用textDidChange函数,但是UITextField没有这个功能,唯一与这个类似的shouldChangeCharactersInRange函数,也是在文件还没有改变前就调用了,而不是在改变后调用,要想实现这个功能,我们可以增加事件监听的方式,这个与java的listener类似.先来看看objective-c提供的接口: // add target/action for particular event. you can call this multiple times and you can specify multiple target/actions for a particular event. // passing in nil as the target goes up the responder chain. The action may optionally include the sender and the event in that order // the action cannot be NULL. - [...]]]></description>
			<content:encoded><![CDATA[<p>在UISearchBar中,当输入信息改变时,它就会调用textDidChange函数,但是UITextField没有这个功能,唯一与这个类似的shouldChangeCharactersInRange函数,也是在文件还没有改变前就调用了,而不是在改变后调用,要想实现这个功能,我们可以增加事件监听的方式,这个与java的listener类似.先来看看objective-c提供的接口:<span id="more-134"></span></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// add target/action for particular event. you can call this multiple times and you can specify multiple target/actions for a particular event.</span>
<span style="color: #666666; font-style: italic;">// passing in nil as the target goes up the responder chain. The action may optionally include the sender and the event in that order</span>
<span style="color: #666666; font-style: italic;">// the action cannot be NULL.</span>
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>addTarget<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>target action<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>SEL<span style="color: #009900;">&#41;</span>action forControlEvents<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>UIControlEvents<span style="color: #009900;">&#41;</span>controlEvents<span style="color: #339933;">;</span></pre></div></div>

<p>怎么去使用这个接口呢?主要分为两步,第一步就是在UItextField组件中增加对文件编辑改变时事件的监听,然后再实现监听器监听到事件时,所调用的方法.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//第一步,对组件增加监听器</span>
<span style="color: #009900;">&#91;</span>textField addTarget<span style="color: #339933;">:</span>self action<span style="color: #339933;">:</span>@selector<span style="color: #009900;">&#40;</span>textFieldDidChange<span style="color: #339933;">:</span><span style="color: #009900;">&#41;</span> forControlEvents<span style="color: #339933;">:</span>UIControlEventEditingChanged<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
...
<span style="color: #666666; font-style: italic;">//第二步,实现回调函数</span>
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> textFieldDidChange<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> sender <span style="color: #009900;">&#123;</span>
	UITextField <span style="color: #339933;">*</span>_field <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>UITextField <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>sender<span style="color: #339933;">;</span>
	NSLog<span style="color: #009900;">&#40;</span>@<span style="color: #ff0000;">&quot;%@&quot;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span>_field text<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.voland.com.cn/uitextfield_increase_textdidchange_callback_function/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>使用Obj-C对数据等进行URLEncoding编码</title>
		<link>http://www.voland.com.cn/urlencoding_for_objective-c</link>
		<comments>http://www.voland.com.cn/urlencoding_for_objective-c#comments</comments>
		<pubDate>Mon, 18 Jan 2010 05:45:10 +0000</pubDate>
		<dc:creator>Nortan</dc:creator>
				<category><![CDATA[Objective-c]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[URLEncoding]]></category>

		<guid isPermaLink="false">http://voland.com.cn/~nortan/?p=123</guid>
		<description><![CDATA[在Objective-c进行网络编程时,经常需要把数据转换成URLEncoding编码,如对+号编码后,变成%2b.这里我们给出一种实现. //NSString+URLEncoding.h #import &#160; @interface NSString &#40;URLEncodingAdditions&#41; - &#40;NSString *&#41;URLEncodedString; - &#40;NSString *&#41;URLDecodedString; @end //NSString+URLEncoding.m #import &#34;NSString+URLEncoding.h&#34; @implementation NSString &#40;OAURLEncodingAdditions&#41; &#160; - &#40;NSString *&#41;URLEncodedString &#123; NSString *result = &#40;NSString *&#41; CFURLCreateStringByAddingPercentEscapes&#40;kCFAllocatorDefault, &#40;CFStringRef&#41;self, NULL, CFSTR&#40;&#34;!*'();:@&#38;amp;=+$,/?%#[] &#34;&#41;, kCFStringEncodingUTF8&#41;; &#91;result autorelease&#93;; return result; &#125; &#160; - &#40;NSString*&#41;URLDecodedString &#123; NSString *result = &#40;NSString *&#41; CFURLCreateStringByReplacingPercentEscapesUsingEncoding&#40;kCFAllocatorDefault, &#40;CFStringRef&#41;self, CFSTR&#40;&#34;&#34;&#41;, kCFStringEncodingUTF8&#41;; &#91;result [...]]]></description>
			<content:encoded><![CDATA[<p>在Objective-c进行网络编程时,经常需要把数据转换成URLEncoding编码,如对+号编码后,变成%2b.这里我们给出一种实现.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//NSString+URLEncoding.h</span>
<span style="color: #339933;">#import </span>
&nbsp;
@interface NSString <span style="color: #009900;">&#40;</span>URLEncodingAdditions<span style="color: #009900;">&#41;</span>
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>NSString <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>URLEncodedString<span style="color: #339933;">;</span>
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>NSString <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>URLDecodedString<span style="color: #339933;">;</span>
@end</pre></div></div>

<p><span id="more-123"></span></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//NSString+URLEncoding.m</span>
<span style="color: #339933;">#import &quot;NSString+URLEncoding.h&quot;</span>
@implementation NSString <span style="color: #009900;">&#40;</span>OAURLEncodingAdditions<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>NSString <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>URLEncodedString
<span style="color: #009900;">&#123;</span>
    NSString <span style="color: #339933;">*</span>result <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>NSString <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
       CFURLCreateStringByAddingPercentEscapes<span style="color: #009900;">&#40;</span>kCFAllocatorDefault<span style="color: #339933;">,</span>
                                                  <span style="color: #009900;">&#40;</span>CFStringRef<span style="color: #009900;">&#41;</span>self<span style="color: #339933;">,</span>
                                                  NULL<span style="color: #339933;">,</span>
                                                  CFSTR<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;!*'();:@&amp;amp;=+$,/?%#[] &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                                                  kCFStringEncodingUTF8<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#91;</span>result autorelease<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>NSString<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>URLDecodedString
<span style="color: #009900;">&#123;</span>
    NSString <span style="color: #339933;">*</span>result <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>NSString <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
       CFURLCreateStringByReplacingPercentEscapesUsingEncoding<span style="color: #009900;">&#40;</span>kCFAllocatorDefault<span style="color: #339933;">,</span>
						<span style="color: #009900;">&#40;</span>CFStringRef<span style="color: #009900;">&#41;</span>self<span style="color: #339933;">,</span>
				 		CFSTR<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	  					kCFStringEncodingUTF8<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#91;</span>result autorelease<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
@end</pre></div></div>

<p>因为是扩展了NSString,使用的时候就比较简单了,首先在使用本类的文件中引入NSString+URLEncoding.h头文件,然后直接用NSString中的URLEncodedString与URLDecodedString方法,如:<br />
NSString *urlToken = [token URLEncodedString];</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voland.com.cn/urlencoding_for_objective-c/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

