<?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>Andy Jacobs blog</title>
	<atom:link href="http://andyj.be/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://andyj.be/blog</link>
	<description>my blog</description>
	<lastBuildDate>Thu, 12 Mar 2009 13:29:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>QuickTip: Notifications in Cocoa-touch</title>
		<link>http://andyj.be/blog/?p=117</link>
		<comments>http://andyj.be/blog/?p=117#comments</comments>
		<pubDate>Thu, 12 Mar 2009 13:29:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[cocoa touch]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[notifications]]></category>

		<guid isPermaLink="false">http://andyj.be/blog/?p=117</guid>
		<description><![CDATA[A very handy thing in cocoa is the use of notifications.
Although it doesn’t work exactly the same as Events in AS3, it can be used for the same principal.
There are 3 functions you need to keep in mind.

- &#40;void&#41;addObserver:&#40;id&#41;notificationObserver selector:&#40;SEL&#41;notificationSelector name:&#40;NSString *&#41;notificationName object:&#40;id&#41;notificationSender
- &#40;void&#41;postNotificationName:&#40;NSString *&#41;notificationName object:&#40;id&#41;notificationSender userInfo:&#40;NSDictionary *&#41;userInfo
- &#40;void&#41;removeObserver:&#40;id&#41;notificationObserver name:&#40;NSString *&#41;notificationName object:&#40;id&#41;notificationSender

addObserver is the same [...]]]></description>
			<content:encoded><![CDATA[<p>A very handy thing in cocoa is the use of <strong>notifications</strong>.<br />
Although it doesn’t work exactly the same as <strong>Events</strong> in AS3, it can be used for the same principal.<br />
There are 3 functions you need to keep in mind.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>addObserver<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>notificationObserver selector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>notificationSelector name<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>notificationName object<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>notificationSender
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>postNotificationName<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>notificationName object<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>notificationSender userInfo<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>userInfo
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>removeObserver<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>notificationObserver name<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>notificationName object<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>notificationSender</pre></div></div>

<p><strong><span id="more-117"></span>addObserver</strong> is the same as <strong>addEventListener<br />
removeObserver</strong> is the same as <strong>removeEventListener</strong><strong><br />
postNotification</strong> is the same as <strong>dispatchEvent</strong></p>
<p>Small demo:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//AppDelegate.m</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application <span style="color: #002200;">&#123;</span>
       <span style="color: #11740a; font-style: italic;">//….</span>
       <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>handler<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> name<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;event&quot;</span> object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>handler<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification
<span style="color: #002200;">&#123;</span>
       NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>,notification<span style="color: #002200;">&#41;</span>;
       <span style="color: #11740a; font-style: italic;">//will output:</span>
       <span style="color: #11740a; font-style: italic;">//NSConcreteNotification 0x574ef0 {name = event; object = &lt;rootViewController: 0x580530&gt;}</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//RootViewController.m</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
       <span style="color: #11740a; font-style: italic;">//…</span>
       <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> postNotificationName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;event&quot;</span> object<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://andyj.be/blog/?feed=rss2&amp;p=117</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>AS3 to Cocoa Touch: MovieClip vs UIView</title>
		<link>http://andyj.be/blog/?p=91</link>
		<comments>http://andyj.be/blog/?p=91#comments</comments>
		<pubDate>Thu, 12 Feb 2009 13:16:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[cocoa touch]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[MovieClip]]></category>
		<category><![CDATA[UIView]]></category>

		<guid isPermaLink="false">http://andyj.be/blog/?p=91</guid>
		<description><![CDATA[UIView vs Movieclip.
It’s true that the UIView object has comparisons with the Movieclip object in actionscript.
But not everything is the same. I’m going to discuss a couple of examples that you would do with a movieclip and show you how to can achieve the same result in objective-c with the UIView object.
But first a couple [...]]]></description>
			<content:encoded><![CDATA[<h3>UIView vs Movieclip.</h3>
<p style="text-align: justify;">It’s true that the <strong>UIView</strong> object has comparisons with the <strong>Movieclip</strong> object in actionscript.<br />
But not everything is the same. I’m going to discuss a couple of examples that you would do with a <strong>movieclip</strong> and show you how to can achieve the same result in objective-c with the <strong>UIView</strong> object.<br />
But first a couple of statements about the <strong>UIView</strong> / <strong>Movieclip</strong> object:</p>
<table style="text-align: center;" border="0" width="100%">
<tbody>
<tr>
<td width="15%"><strong>Movieclip</strong></td>
<td width="70%"><strong>Question</strong></td>
<td width="15%"><strong>UIView</strong></td>
</tr>
<tr>
<td width="15%">✓</td>
<td width="70%">The object is a container for view/display objects</td>
<td width="15%">✓</td>
</tr>
<tr>
<td width="15%">✓</td>
<td width="70%">You can draw into the view/display object</td>
<td width="15%">✓</td>
</tr>
<tr>
<td width="15%">✓</td>
<td width="70%">You can animate the object directly</td>
<td width="15%">✓</td>
</tr>
<tr>
<td width="15%">✓</td>
<td width="70%">Accessing view through id</td>
<td width="15%">✓</td>
</tr>
</tbody>
</table>
<p><strong><span id="more-91"></span>Containing childs :</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//Actionscript 3</span>
<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> i <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span> ; i <span style="color: #000000; font-weight: bold;">&amp;</span>lt; <span style="color: #000000; font-weight:bold;">10</span> ; i <span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
     <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">rect</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Rectangle</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//Custom object</span>
     <span style="color: #004993;">rect</span>.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">floor</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">%</span>4<span style="color: #000000;">&#41;</span>;
     <span style="color: #004993;">rect</span>.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">90</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">floor</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#41;</span>;
     <span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">rect</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">numChildren</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//result: 10</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//Objective-C</span>
<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">10</span> ; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
     Rectangle <span style="color: #002200;">*</span>rect <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Rectangle alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #2400d9;">0</span>,<span style="color: #2400d9;">40</span>,<span style="color: #2400d9;">80</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//Custom Object</span>
     rect.transform <span style="color: #002200;">=</span> CGAffineTransformMakeTranslation<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">50</span><span style="color: #002200;">*</span><span style="color: #a61390;">floor</span><span style="color: #002200;">&#40;</span>i<span style="color: #002200;">%</span>4<span style="color: #002200;">&#41;</span>, <span style="color: #2400d9;">90</span><span style="color: #002200;">*</span><span style="color: #a61390;">floor</span><span style="color: #002200;">&#40;</span>i<span style="color: #002200;">/</span><span style="color: #2400d9;">4</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
     <span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span>rect<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//See that your window variable is accessible</span>
     <span style="color: #002200;">&#91;</span>rect release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%i&quot;</span>,<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>window subviews<span style="color: #002200;">&#93;</span> count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">//result: 10</span></pre></div></div>

<p>You just allocate your object and initiate it with the <strong>initWithFrame:</strong> function. You pass it the size and position you want in our case x,y = 0 the width is 40 and the height 80 in our case.<br />
since you can’t directly acces the frame X and Y values ( except if you pass a new <strong>CGRect</strong> to the frame property)<br />
there is a big difference with <strong>UIView</strong> and <strong>Movieclip</strong><br />
With <strong>UIView</strong> you have to specify a canvas when you init the object<br />
a <strong>Movieclip</strong> will autoresize to the content inside, unless you define the width and height<br />
with the use of a UIView you have 2 important property’s , Frame &amp; Bounds ( <a title="frame &amp; bounds tutorial" href="http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/CocoaViewsGuide/Coordinates/chapter_3_section_3.html" target="_blank">understanding frame and bounds</a> )</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//rect.frame.origin.x = 100.0f; &amp;lt;- will give an error (invalid lvalue in assignment)</span>
CGRect frame <span style="color: #002200;">=</span> rect.frame;
frame.origin <span style="color: #002200;">=</span> CGPointMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">100</span>,<span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
rect.frame <span style="color: #002200;">=</span> frame;</pre></div></div>

<p>but I’m in favor to use the transform property and pass it an Matrix Translation with the <strong>CGAffineTransformMakeTranslation</strong> function which returns an matrix translation structure.<br />
I pass it the x and y values I want (in this case I’m iterating my x and y values so it will look like the picture below)<br />
don’t forget to release your object If your not going to use it anymore<br />
and our window <strong>subviews</strong> count is 10</p>
<div class="wp-caption aligncenter" style="width: 620px"><img title="Blocks" src="http://andyj.be/blog/wp-content/Picture-42.jpg" alt="Comparison iphone as3" width="610" height="391" /><p class="wp-caption-text">Comparison iphone as3</p></div>
<p>on the left flash in the browser on the right the iphone (looks exactly the same)<br />
<strong>Drawing into a view/display object:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//Actionscript 3</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
     <span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0xFF0000<span style="color: #000000;">&#41;</span>;
     <span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawRect</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">40</span>,<span style="color: #000000; font-weight:bold;">80</span><span style="color: #000000;">&#41;</span>;
     <span style="color: #004993;">graphics</span>.<span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">Objective<span style="color: #002200;">-</span>C
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>rect <span style="color: #002200;">&#123;</span>
     <span style="color: #11740a; font-style: italic;">// Drawing code</span>
     CGContextRef context <span style="color: #002200;">=</span> UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
     CGContextSetRGBFillColor<span style="color: #002200;">&#40;</span>context, <span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#41;</span>;
     CGContextFillRect<span style="color: #002200;">&#40;</span>context, rect<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>If you make a view object in xcode there will be a function created for you called <strong>drawRect:</strong><br />
that’s the function where you mostly want to put your code for drawing.<br />
the first thing that you got to do is get the graphics context (in a movieclip that’s just a property but in objective c you can get the graphics context with a simple function)<br />
you define which rgb color you want to use for that context and you fill that rect using the <strong>CGContextFillRect</strong> passing it the context + the frame you want to draw it in.</p>
<p>the <strong>drawRect:</strong> function is automatically called on initialization and the rect that is passed is the frame property. Since our frame-size is 40&#215;80 we just pas it the rect (you could define your own rect).</p>
<p>if you want to redraw don’t ever call the <strong>drawRect:</strong> method directly! (EVER..)<br />
use the  <strong>setNeedsDisplay</strong> or <strong>setNeedsDisplayInRect:</strong> function.</p>
<p><strong>Animating your view/display object:</strong><br />
you can animate your view in 2 different ways, using the animation methods provided by the UIView or changing the properties using a timer.<br />
Which would be the same as using the Tween class or changing the property values in an onenterframe event.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//Actionscript 3</span>
<span style="color: #009900;">//Append this code to the previous function</span>
<span style="color: #6699cc; font-weight: bold;">var</span> rectAnim1 <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Rectangle</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
rectAnim1.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">90</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">4</span>;
rectAnim1.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">100</span>;
rectAnim1.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, animateHandler<span style="color: #000000;">&#41;</span>;
<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>rectAnim1<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> rectAnim2 <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Rectangle</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
rectAnim2.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">90</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">4</span>;
rectAnim2.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">200</span>;
<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>rectAnim2<span style="color: #000000;">&#41;</span>;
TweenLite.to<span style="color: #000000;">&#40;</span>rectAnim2, <span style="color: #000000; font-weight:bold;">5</span>, <span style="color: #000000;">&#123;</span><span style="color: #004993;">rotation</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">180</span>,ease<span style="color: #000000; font-weight: bold;">:</span>Expo.easeInOut<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #009900;">//end</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> animateHandler<span style="color: #000000;">&#40;</span> e <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Event</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">target</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">rotation</span> = <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">target</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">rotation</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">1</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//Objective-C</span>
<span style="color: #11740a; font-style: italic;">//Append this code to the previous function</span>
Rectangle <span style="color: #002200;">*</span>rectAnim1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Rectangle alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">100</span>,<span style="color: #2400d9;">90</span><span style="color: #002200;">*</span><span style="color: #2400d9;">4</span>,<span style="color: #2400d9;">40</span>,<span style="color: #2400d9;">80</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//Custom Object</span>
<span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span>rectAnim1<span style="color: #002200;">&#93;</span>;
rectAnim1.transform <span style="color: #002200;">=</span> CGAffineTransformMakeRotation<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#91;</span>UIView beginAnimations<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rotate&quot;</span> context<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>UIView setAnimationCurve<span style="color: #002200;">:</span>UIViewAnimationCurveEaseInOut<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>UIView setAnimationDuration<span style="color: #002200;">:</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#93;</span>;
rectAnim1.transform <span style="color: #002200;">=</span> CGAffineTransformMakeRotation<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">3.14</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#91;</span>UIView commitAnimations<span style="color: #002200;">&#93;</span>;
&nbsp;
rectAnim2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Rectangle alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">200</span>,<span style="color: #2400d9;">90</span><span style="color: #002200;">*</span><span style="color: #2400d9;">4</span>,<span style="color: #2400d9;">40</span>,<span style="color: #2400d9;">80</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//Custom Object (added to you interface so you can acces it from other functions)</span>
<span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span>rectAnim2<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #400080;">NSTimer</span> scheduledTimerWithTimeInterval<span style="color: #002200;">:</span>0.033f target<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>handleTimer<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> userInfo<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> repeats<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #11740a; font-style: italic;">//end</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> handleTimer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSTimer</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>timer
<span style="color: #002200;">&#123;</span>
     angle <span style="color: #002200;">+=</span> <span style="color: #2400d9;">0.03</span>;
     rectAnim2.transform <span style="color: #002200;">=</span> CGAffineTransformMakeRotation<span style="color: #002200;">&#40;</span>angle<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>So for the first animation were going to use the animation methods provided by the <strong>UIView</strong> we define a beginning and we give it a name. we define the curve and the duration. Than we define all our transformation we want to make happen. And we commit the animations (which means that they will instantly start playing.<br />
For the other example we use a Timer, we schedule a timer that will tick every 0.033seconds we define the method that we want to be called every tick with a selector en we define a repeat so it will repeat endlessly<br />
In the method handleTimer we just going to rotate by adding 0.03 to the currentAngle</p>
<p>Accessing view through id:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//Actionscript 3</span>
<span style="color: #009900;">//you need to name your movieclip (with a number)</span>
<span style="color: #009900;">//so add this line of code into your for loop</span>
<span style="color: #004993;">rect</span>.<span style="color: #004993;">name</span> = <span style="color: #004993;">String</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span>;
<span style="color: #009900;">//end</span>
&nbsp;
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">getChildAt</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">getChildByName</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;3&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//returns the movieclip with name “3”</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//Objective-C</span>
<span style="color: #11740a; font-style: italic;">//you need to tag our views (with a number)</span>
<span style="color: #11740a; font-style: italic;">//so add this line of code into your for loop</span>
rect.tag <span style="color: #002200;">=</span> i;
<span style="color: #11740a; font-style: italic;">//end</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>,<span style="color: #002200;">&#91;</span>window viewWithTag<span style="color: #002200;">:</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">//returns the view with tag “3”</span></pre></div></div>

<p>There are a lot more comparisons like they both have the alpha property, you can insert views at a certain index, observing changes, and a lot more. I just wanted to point out a couple of practical comparisons between the 2 languages.</p>
]]></content:encoded>
			<wfw:commentRss>http://andyj.be/blog/?feed=rss2&amp;p=91</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>FITC Amsterdam Mobile Guide Released</title>
		<link>http://andyj.be/blog/?p=80</link>
		<comments>http://andyj.be/blog/?p=80#comments</comments>
		<pubDate>Wed, 11 Feb 2009 09:53:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[conference]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[fitc iphone flash lite]]></category>

		<guid isPermaLink="false">http://andyj.be/blog/?p=80</guid>
		<description><![CDATA[It&#8217;s been awfully quite around here last couple of weeks. That has it&#8217;s reasons.
I was involved with the 2 projects, 1 being a Native iPhone applications where i will be releasing more details about very soon, and the other is the FITC Mobile Guide iphone website.
For you guys who don&#8217;t know what fitc is.
If you [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been awfully quite around here last couple of weeks. That has it&#8217;s reasons.<br />
I was involved with the 2 projects, 1 being a Native iPhone applications where i will be releasing more details about very soon, and the other is the FITC Mobile Guide iphone website.</p>
<p>For you guys who don&#8217;t know what fitc is.</p>
<blockquote><p>If you were lucky enough to attend our FITC Amsterdam 2008, you know what it&#8217;s all about. If you missed it, <span style="text-decoration: line-through;">you&#8217;re going to want to grab a ticket before they&#8217;re all gone</span> (there sold out, but you can put yourself on the wait list <a onmousedown="UntrustedLink.bootstrap($(this), &quot;7e6a4f4c1de7e2eddabfabd7a287cf71&quot;, event)" rel="nofollow" href="http://www.fitc.ca/amsterdam/wait/" target="_blank"><span>http://www.fitc.ca/amsterd</span>am/wait/</a>). There is no other event quite like it. The combination of unbelievable presenters from around the globe, incredible networking opportunities, and the infamous FITC parties, all with the amazing backdrop of the city of Amsterdam, creates a stage set for the unexpected. We&#8217;ve brought back some of your favourite speakers, added a number of new ones, and we also have a few surprises up our sleeve to keep everyone on their toes. With some of the most interesting and engaging presenters from around the world, it&#8217;s 2 days and nights that will leave you inspired, energized and awed. And probably pretty tired. So join us for an event you will never forget!<br />
With a limited number of tickets available, this event will sell out.</p>
<ul>
<li>2 full days and nights of events, plus 1 day of optional pre-festival workshops</li>
<li>Over 50 presentations and panels covering the Creative, Technical, and Business aspects of Flash and digital media</li>
<li>Over 500 attendees from around the globe</li>
<li>Over 50 internationally renowned design and technology presenters</li>
<li>Includes three FITC evening events</li>
</ul>
</blockquote>
<p>Me and my co-worker <a href="http://thomasjoos.be" target="_blank">Thomas Joos </a>Developed a complete mobile guide for the upcoming FITC Amsterdam.</p>
<p>What can you expect from the mobile guide we developed?</p>
<ul>
<li>FITC Web iPhone mobile guide ( <a title="fitc iphone website" href="http://m.fitc.ca" target="_blank"><strong>m.fitc.ca</strong></a> <a href="http://vimeo.com/3172681">FITC iPhone Web guide video</a> )</li>
<li>FITC Native iPhone mobile guide ( coming soon )</li>
<li>FITC Flash Lite mobile guide ( <a href="http://www.ﬁtc.ca/amsterdam/mobile" target="_blank">http://www.ﬁtc.ca/amsterdam/mobile</a> )</li>
<li>FITC FlashLite Screensaver/Wallpaper</li>
<li>FITC iPhone Wallpaper</li>
</ul>
<p><span id="more-80"></span>Today, FITC has send out a press release in order to inform all attendees that there is a whole range of mobile FITC Amsterdam content available. As some of you know, I kinda have my share in this one:</p>
<blockquote><p>Toronto, Canada [January x, 2009] — Adobe Max Award08 winner Boulevart is busy putting the ﬁnal touches on a mobile content package for FITC Amsterdam, a two-day event focused on the art and craft of Interactive design and technology. This is the second year for FITC Amsterdam, which takes place February 22 &#8211; 24, 2009 at the Felix Meritis European Centre for Arts and Sciences. The Boulevart team includes Mobile Consultant and Flash® Lite™ expert Thomas Joos, a graduate of the Technical University of West-Flanders.</p>
<p>“Thomas is on the cutting-edge of rich media for mobile and typiﬁes the kind of sharp, young creative minds that make our events a success,” says Shawn Pucknell, Founder and Executive Producer of FITC Amsterdam.</p>
<p>Beginning today, FITC Amsterdam attendees can download mobile screensavers, wallpapers, and a 2009 event guide at <a href="http://www.ﬁtc.ca/amsterdam/mobile" target="_blank">http://www.ﬁtc.ca/amsterdam/mobile</a>. All content was developed by Boulevart using Adobe® Flash® Lite™, a powerful runtime engine for mobile and consumer electronics devices.</p>
<p>[...]</p></blockquote>
<p>You can download the entire <a title="press release" href="http://labs.boulevart.be/mobile/FITC/FITC09_press.pdf" target="_blank">press release here.</a> Go and check out <a href="http://www.ﬁtc.ca/amsterdam/mobile" target="_blank">http://www.ﬁtc.ca/amsterdam/mobile</a> and if you are attending make sure you don’t miss FITC’s mobile boat this year!</p>
<p>If you like what you see and you want something similar for your event.. you can always <a title="mail andy" href="mailto:andy.jacobs@boulevart.be" target="_blank">contact me</a>.</p>
<p>So 2 links you definitely should check out<br />
FITC iPhone Website ( <a title="iphone website fitc" href="http://m.fitc.ca" target="_blank"><strong>m.fitc.ca</strong></a> <a href="http://vimeo.com/3172681">FITC iPhone Web guide video</a> )<br />
FITC Mobile guide Page ( <a href="http://www.ﬁtc.ca/amsterdam/mobile" target="_blank">http://www.ﬁtc.ca/amsterdam/mobile</a> )</p>
<p>We are going to be present at FITC Amsterdam so if you have any questions or feedback, just come up to us.<br />
So that being said. I learned a lot couple of last weeks, so when i find a little more free time there will definitly be a couple of follow up tutorials about iphone development ( + as3 conversion ). (which i&#8217;m currently already working on)</p>
]]></content:encoded>
			<wfw:commentRss>http://andyj.be/blog/?feed=rss2&amp;p=80</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>AS3 to Cocoa touch: JSON (Part 2: send JSON)</title>
		<link>http://andyj.be/blog/?p=65</link>
		<comments>http://andyj.be/blog/?p=65#comments</comments>
		<pubDate>Mon, 05 Jan 2009 19:58:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[cocoa touch]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[cocoa touch json as3 iphone]]></category>

		<guid isPermaLink="false">http://andyj.be/blog/?p=65</guid>
		<description><![CDATA[So for part 2 of my tutorial about json. We are going to use json encoded data to transfer over http from your application to the backend server. There are some things you have to keep in mind if you encode json in cocoa touch using the json framework (which I&#8217;ve used in the previous [...]]]></description>
			<content:encoded><![CDATA[<p>So for part 2 of my tutorial about json. We are going to use json encoded data to transfer over http from your application to the backend server. There are some things you have to keep in mind if you encode json in cocoa touch using the json framework (which I&#8217;ve used in the previous tutorial). But we will go deeper on that subject later on.</p>
<p>So for this part of the tutorial my usercase will be. Send a couple of Person objects over http using json to encode and get an xml in return which lists the Person objects in xml form. This maybe not a practical usercase but it illustrates the point of sending json over http.<span id="more-65"></span></p>
<p>So we will use <strong>POST for the HTTP Request</strong>. Which is a very simple way to send data over http from 1 point to another ( think of all the html forms filled in, in best case they will use <strong>POST</strong> to send data over from 1 page to another).</p>
<p><em><strong>Obj-c: data: encode json  -&gt; http (post) -&gt; php: decode json + change to xml -&gt; http -&gt; obj-c: XML</strong></em></p>
<p>So I&#8217;m going to provide the php I use, I didn&#8217;t wrote it myself, I used google and came with this piece of code.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">\n<span style="color: #0000ff;">&quot;;
    <span style="color: #006699; font-weight: bold;">$xml</span> .= &quot;</span>\n<span style="color: #0000ff;">&quot;;
    foreach(<span style="color: #006699; font-weight: bold;">$buffer</span> as <span style="color: #006699; font-weight: bold;">$val</span>) {
        <span style="color: #006699; font-weight: bold;">$xml</span> .= &quot;</span>    \n<span style="color: #0000ff;">&quot;;
&nbsp;
        foreach (<span style="color: #006699; font-weight: bold;">$val</span> as <span style="color: #006699; font-weight: bold;">$key</span> =&amp;gt; <span style="color: #006699; font-weight: bold;">$value</span>) {
            <span style="color: #006699; font-weight: bold;">$xml</span> .= &quot;</span>        <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #0000ff;">&quot;.utf8_encode(<span style="color: #006699; font-weight: bold;">$value</span>).&quot;</span><span style="color: #339933;">&lt;!--</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">--&gt;</span>\n<span style="color: #0000ff;">&quot;;
        }
&nbsp;
        <span style="color: #006699; font-weight: bold;">$xml</span> .= &quot;</span>    \n<span style="color: #0000ff;">&quot;;
    }
    <span style="color: #006699; font-weight: bold;">$xml</span> .= &quot;</span>\n<span style="color: #0000ff;">&quot;;
&nbsp;
    return <span style="color: #006699; font-weight: bold;">$xml</span>;
}
<span style="color: #006699; font-weight: bold;">$json</span> = <span style="color: #006699; font-weight: bold;">$_POST</span>[&quot;</span>json<span style="color: #0000ff;">&quot;];
<span style="color: #006699; font-weight: bold;">$data</span> = json_decode(stripslashes(<span style="color: #006699; font-weight: bold;">$json</span>));
echo array2xml(<span style="color: #006699; font-weight: bold;">$data</span>);
?&amp;gt;</span></pre></div></div>

<p>So I&#8217;m not going to discuss all the code. Just a couple of them.</p>
<ul>
<li>We change our content type to xml</li>
<li>We have our function that is going to convert an array into a xml.</li>
<li>We have our json data that we get from post (which simply has the name json)</li>
<li>We decode our data using json_decode and this is import, since json uses quotation marks in its string we have to use the function stripslashes. Because when we are going to send our json string with Post Http request before every quotation mark it will add a slash. So just make sure to strip those slashes. It will return an array since we send an array to our php.</li>
<li>Echo that $data variable using the array2xml which will return an xml String.</li>
</ul>
<p>So for the actionscript part we have:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #0033ff; font-weight: bold;">import</span> com.adobe.serialization.json.JSON;
&nbsp;
     <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
     <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
     <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.net</span>.<span style="color: #000000; font-weight: bold;">*</span>;
&nbsp;
     <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> MainJSON extends <span style="color: #004993;">Sprite</span>
     <span style="color: #000000;">&#123;</span>
          <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MainJSON<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
          <span style="color: #000000;">&#123;</span>
               <span style="color: #6699cc; font-weight: bold;">var</span> arr <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Array</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> Person<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Lord Vader&quot;</span>,<span style="color: #000000; font-weight:bold;">23</span><span style="color: #000000;">&#41;</span>,<span style="color: #0033ff; font-weight: bold;">new</span> Person<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Qui-Gon Jinn&quot;</span>,<span style="color: #000000; font-weight:bold;">51</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
               sendJSON<span style="color: #000000;">&#40;</span> arr <span style="color: #000000;">&#41;</span>;
          <span style="color: #000000;">&#125;</span>
          <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> sendJSON<span style="color: #000000;">&#40;</span> a <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Array</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span>
          <span style="color: #000000;">&#123;</span>
               <span style="color: #6699cc; font-weight: bold;">var</span> jsonString <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">String</span> = JSON.encode<span style="color: #000000;">&#40;</span>a<span style="color: #000000;">&#41;</span>;
&nbsp;
               <span style="color: #6699cc; font-weight: bold;">var</span> urlVariables<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">URLVariables</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLVariables</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
               urlVariables.json = jsonString;
&nbsp;
               <span style="color: #6699cc; font-weight: bold;">var</span> urlRequest<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">URLRequest</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLRequest</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;http://localhost/iphone/json/array2xml.php&quot;</span><span style="color: #000000;">&#41;</span>;
               urlRequest.<span style="color: #004993;">method</span> = <span style="color: #004993;">URLRequestMethod</span>.<span style="color: #004993;">POST</span>;
               urlRequest.<span style="color: #004993;">data</span> = urlVariables;
&nbsp;
               <span style="color: #6699cc; font-weight: bold;">var</span> urlLoader<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">URLLoader</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLLoader</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
               urlLoader.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">COMPLETE</span>, onURLLoaderCompleteEvent<span style="color: #000000;">&#41;</span>;
               urlLoader.<span style="color: #004993;">load</span><span style="color: #000000;">&#40;</span>urlRequest<span style="color: #000000;">&#41;</span>;
          <span style="color: #000000;">&#125;</span>
          <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> onURLLoaderCompleteEvent<span style="color: #000000;">&#40;</span> evt <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Event</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span>
          <span style="color: #000000;">&#123;</span>
               <span style="color: #6699cc; font-weight: bold;">var</span> xml<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">XML</span><span style="color: #000000;">&#40;</span>evt.<span style="color: #004993;">target</span>.<span style="color: #004993;">data</span><span style="color: #000000;">&#41;</span>;
               <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>xml<span style="color: #000000;">&#41;</span>;
          <span style="color: #000000;">&#125;</span>
     <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Ok, a quick run through.</p>
<ul>
<li>We declare an array with 2 person objects we want to send. And feed it to the sendJSON function.</li>
<li>Encode our JSON string using the static encode method of the JSON class from the corelib framework.</li>
<li>Declare a <strong>URLVariables</strong> object which we are going to use to store our post variables in it. Behind the scenes its going to parse a correct httpbody and header message needed to transfer over http.</li>
<li>We declare our urlRequest which we tell which url you need to load, what the method will be how we will sending our data , in our case <strong>POST</strong>, and finally in the data property we put the urlVariables object.</li>
<li>We declare our <strong>URLLoader</strong> which is going to do the request to the page and in our case return an xml</li>
<li>When we have an <strong>eventListener</strong> listening to the <strong>URLLoader</strong> object on complete you will get some data in return for it when that function is called. We cast it to XML an trace that.</li>
</ul>
<p>And that&#8217;s how we send JSON over http with Actionscript 3.</p>
<p>Now let&#8217;s try the same in Cocoa Touch</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>arrayDict <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
Person <span style="color: #002200;">*</span>person1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Person alloc<span style="color: #002200;">&#93;</span> initWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Lord Vader&quot;</span> andAge<span style="color: #002200;">:</span><span style="color: #2400d9;">23</span><span style="color: #002200;">&#93;</span>;
Person <span style="color: #002200;">*</span>person2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Person alloc<span style="color: #002200;">&#93;</span> initWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Qui-Gon Jinn&quot;</span> andAge<span style="color: #002200;">:</span><span style="color: #2400d9;">51</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>arrayDict addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>person1 getDictionary<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>arrayDict addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>person2 getDictionary<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>person1 release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>person2 release<span style="color: #002200;">&#93;</span>;
&nbsp;
SBJSON <span style="color: #002200;">*</span>json <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>SBJSON alloc<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>jsonString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>json stringWithObject<span style="color: #002200;">:</span>arrayDict error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>arrayDict release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>json release<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSMutableURLRequest</span> <span style="color: #002200;">*</span>request <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableURLRequest</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>post <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&amp;amp;json=%@&quot;</span>,jsonString<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>postData <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>post dataUsingEncoding<span style="color: #002200;">:</span>NSASCIIStringEncoding allowLossyConversion<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>request setURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://localhost/iphone/json/array2xml.php&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>request setHTTPMethod<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;POST&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>request setValue<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;application/x-www-form-urlencoded&quot;</span> forHTTPHeaderField<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;content-type&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>request setHTTPBody<span style="color: #002200;">:</span>postData<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSURLResponse</span> <span style="color: #002200;">*</span>urlResponse;
&nbsp;
<span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>data <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLConnection</span> sendSynchronousRequest<span style="color: #002200;">:</span>request returningResponse<span style="color: #002200;">:&amp;</span>amp;urlResponse error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>request release<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>jsonData <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> alloc<span style="color: #002200;">&#93;</span> initWithData<span style="color: #002200;">:</span>data encoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>,jsonData<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#91;</span>jsonData release<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>So first of all, we do need a project setup with the JSON framework (used in the previous tutorial).</p>
<p>So the first 7 lines of code doesn&#8217;t have anything to do with json but its just our data we are going to encode to json and send over to our backend server. But there are a couple of rules we have to keep in mind. And the most important rule is you cannot encode <strong>NSObjects</strong> to JSON. You have to use <strong>NSDictionary</strong> instead! When we open the JSON framework documentation ( included in the .dmg file) we can see the conversian table.</p>
<p>Objective-C types are mapped to JSON types and back in the following way:</p>
<p>* NSNull -&gt; Null -&gt; NSNull<br />
* NSString -&gt; String -&gt; NSMutableString<br />
* NSArray -&gt; Array -&gt; NSMutableArray<br />
* NSDictionary -&gt; Object -&gt; NSMutableDictionary<br />
* NSNumber (-initWithBool:) -&gt; Boolean -&gt; NSNumber -initWithBool:<br />
* NSNumber -&gt; Number -&gt; NSDecimalNumber</p>
<p>So In our case we have a value object of the type Person but we cant encode that to JSON. What I did instead was add a method to the Person class that will return an NSDictionary containing the values from the Person objects assigned to the key&#8217;s with the exact names as the object property&#8217;s.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//Person.h</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>getDictionary;
<span style="color: #11740a; font-style: italic;">//Person.m</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>getDictionary<span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>self.name, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;name&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span>self.age<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;age&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Add these lines of code to the Person.h &amp; Person.m class.<br />
What it does is very simple, return an <strong>Object of</strong> type <strong>NSDictionary</strong> with the values and properties of the <strong>Person</strong> class.<br />
(note: that you have to convert the integer age to an <strong>Object</strong> of type <strong>NSNumber</strong>).</p>
<p>Now back to our code. We instantiate an object of type <strong>NSMutableArray</strong> where we are going to keep all our dictionary&#8217;s. We make our person Objects and  call the <strong>getDictionary</strong> method on it and push it into our previously created array.<br />
You can release those objects because we don&#8217;t will be needing them anymore now.</p>
<p>Next step in the process will be to encode our array to JSON.<br />
We allocate an object of type <strong>SBJSON</strong>.<br />
we use the method <strong>stringWithObject:error:</strong> on it to convert our array to a JSON String, and store it in a <strong>NSString</strong> object.<br />
We don&#8217;t need the array or the json object anymore so we can release that as well.</p>
<p>So now we have our string we want to send to our backend service. We are going to send it the same way like we did in actionscript using HTTP request (POST). It&#8217;s a little bit more difficult than in actionscript, and that&#8217;s simply because actionscript does a little bit more work under the hood that we have to do for ourselves in objective-C.</p>
<p>So what we need 4 things for our request object</p>
<p><em>- the url of the backend service (obviously)<br />
- the http method ( POST in our case )<br />
- the http body containing our data<br />
- We need to set the value of 1 http header field ( content-type )</em></p>
<p>So first we are going to make our http body which is going to hold up our data we want to send.</p>
<p>We format a string with the property&#8217;s like this:</p>
<p><em><strong>&amp;key=value,key=value,..</strong></em></p>
<p>if we use the function <strong>stringWithFormat: </strong>we can add directives which we can add as a parameter that are copied directly to the result. But our http body expects an object of type <strong>NSData</strong> so we need to convert our string to an <strong>NSData</strong> object which we do by using the method <strong>dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO</strong> on our string. Using the ascii encoding and not allowing it to lossy conversion.</p>
<p>So fill in the right values to the right properties of our request object.</p>
<p>Make a pointer for urlResponse since we are getting an xml back from the backend service. And the next lines of code I already discussed in my previous tutorial about JSON. So I&#8217;m not going to run through it again.</p>
<p>So the key principle is to setup your Http Request for <strong>POST</strong> right, which in objective-C you have to do for yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://andyj.be/blog/?feed=rss2&amp;p=65</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>AS3 to Cocoa touch: JSON (Part 1: load json)</title>
		<link>http://andyj.be/blog/?p=52</link>
		<comments>http://andyj.be/blog/?p=52#comments</comments>
		<pubDate>Wed, 24 Dec 2008 12:40:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[cocoa touch]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://andyj.be/blog/?p=52</guid>
		<description><![CDATA[So in this tutorial I&#8217;m going to discuss what would be a possible way to  transfer your data from a backend to your iphone. I have 2 primary things I have in mind doing this what are very important to me. It must be easy to implement (in the backend as well as in objective-c), [...]]]></description>
			<content:encoded><![CDATA[<p>So in this tutorial I&#8217;m going to discuss what would be a possible way to  transfer your data from a backend to your iphone. I have 2 primary things I have in mind doing this what are very important to me. It must be easy to implement (in the backend as well as in objective-c), And I need a format that is accessible from many program languages / approaches.<br />
So from that point on I stumbled upon the use of JSON. It&#8217;s very easy in use, support for many languages (http://json.org). My Actionscript mind would always put amf in favor since its very well supported in actionscript and the flash player. As well as the backend side of the story ( fluorine, amfphp, weborb, blazeds, livecycle, ..).</p>
<p>The usercase that I came in contact with the most was, get something out of a database with php convert it to amf with amfphp or weborb and send it over to flash. So that&#8217;s the usercase I want to take as an example to do the comparison. So what is my setup ?</p>
<p><strong><span id="more-52"></span>Mysql:</strong> Data -&gt; <strong>php: </strong>encode json -&gt; <strong>http</strong> -&gt; <strong>obj-c:</strong> decode json</p>
<p>I&#8217;m going to keep it all as simple as possible. Just to give you a head start on what could be done.</p>
<p>So for the backend we have our mysql db (&#8221;iphone&#8221; in my case)</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #008000;">'persons'</span> <span style="color: #FF00FF;">&#40;</span>
    <span style="color: #008000;">'id'</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">11</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">auto_increment</span><span style="color: #000033;">,</span>
    <span style="color: #008000;">'name'</span> <span style="color: #999900; font-weight: bold;">varchar</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">255</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
    <span style="color: #008000;">'age'</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">5</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
    <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span>  <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'id'</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">ENGINE</span><span style="color: #CC0099;">=</span>MyISAM  <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #FF9900; font-weight: bold;">CHARSET</span><span style="color: #CC0099;">=</span>latin1 <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #CC0099;">=</span><span style="color: #008080;">5</span> <span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #008000;">'persons'</span> <span style="color: #990099; font-weight: bold;">VALUES</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">1</span><span style="color: #000033;">,</span> <span style="color: #008000;">'Hans'</span><span style="color: #000033;">,</span> <span style="color: #008080;">32</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #008000;">'persons'</span> <span style="color: #990099; font-weight: bold;">VALUES</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">2</span><span style="color: #000033;">,</span> <span style="color: #008000;">'John'</span><span style="color: #000033;">,</span> <span style="color: #008080;">12</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #008000;">'persons'</span> <span style="color: #990099; font-weight: bold;">VALUES</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">3</span><span style="color: #000033;">,</span> <span style="color: #008000;">'Zaki'</span><span style="color: #000033;">,</span> <span style="color: #008080;">34</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #008000;">'persons'</span> <span style="color: #990099; font-weight: bold;">VALUES</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">4</span><span style="color: #000033;">,</span> <span style="color: #008000;">'Dr. Cox'</span><span style="color: #000033;">,</span> <span style="color: #008080;">88</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></div></div>

<p>And for our php we have:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_pconnect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not connect&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;iphone&quot;</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not select database&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$rs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT name, age FROM persons&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rs</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Whatever backend you use it doesn&#8217;t matter as long as it useses the http protocol and the json format to transfer your data from the database. You can get the example to work.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> com.adobe.serialization.json.JSON;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.net</span>.<span style="color: #004993;">URLLoader</span>;
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.net</span>.<span style="color: #004993;">URLRequest</span>;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> MainJSON extends <span style="color: #004993;">Sprite</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MainJSON<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">loader</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">URLLoader</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLLoader</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #004993;">loader</span>.<span style="color: #004993;">load</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLRequest</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;http://andyj.be/iphone/json/persons.php&quot;</span> <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #004993;">loader</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">COMPLETE</span>, decodeJSON<span style="color: #000000;">&#41;</span> ;
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> decodeJSON<span style="color: #000000;">&#40;</span>evt<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> persons<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = JSON.<span style="color: #004993;">decode</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">URLLoader</span><span style="color: #000000;">&#40;</span> evt.<span style="color: #004993;">target</span> <span style="color: #000000;">&#41;</span>.<span style="color: #004993;">data</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> key<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> <span style="color: #0033ff; font-weight: bold;">in</span> persons<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #6699cc; font-weight: bold;">var</span> tmp <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Object</span> = persons<span style="color: #000000;">&#91;</span>key<span style="color: #000000;">&#93;</span>;
                <span style="color: #6699cc; font-weight: bold;">var</span> person <span style="color: #000000; font-weight: bold;">:</span> Person = <span style="color: #0033ff; font-weight: bold;">new</span> Person<span style="color: #000000;">&#40;</span>tmp.<span style="color: #004993;">name</span>,<span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span> tmp.age <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
                <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> person <span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p style="text-align: left;">*note: you must include the corelib api ( http://code.google.com/p/as3corelib/ ) because decoding and encoding JSON isn&#8217;t part of the flash player runtime.</p>
<p>So a quick overview of our AS3 code, we intantiate our <strong>URLLoader</strong> (that we provide with our link to our webservice) which is going to handle the loading of our data from the webservice. Just define an <strong>eventListener</strong> on &#8220;<strong>complete</strong>&#8220;. In that function were just going to decode our data with the static function &#8220;<strong>decode</strong>&#8221; provided by the JSON class ( from the corelib api ). Because we are parsing an array from our webservice we should get an <strong>Array</strong> in return from the static function.</p>
<p>Each object in the array has a key ( which is just a simple integer in reality ( so you could use a Three-expression for loop if you want) ) if we take the value for the key we provide we get our object in return which has a name and age property. Just put those values in our person Object et voila, were finished.</p>
<p>Ok now for our objective-c side of the story, just like in Actionscript, the cocoa framework doesn&#8217;t provide a JSON decoding and encoding API. But Stig Brautaset provided us with a JSON framework that is fairly easy to implement.</p>
<p>You have to do a couple of things before we can continue.</p>
<p><span style="color: #ff9900;"><strong>1. Download the DMG <a href="http://json-framework.googlecode.com/files/JSON_2.1.1.dmg" target="_blank">http://json-framework.googlecode.com/files/JSON_2.1.1.dmg</a><br />
2. Copy the directory /Volumes/JSON_2.1.1/SDKs/JSON to ~/Library/SDKs/JSON</strong></span></p>
<p>that wasn&#8217;t so hard was it ?.. Ok, create a window based application like we did in our previous examples and before we continue providing our appdelegate class with code we have to set a couple paramaters (just like we would add the corelib swc to the build path of our actionscript project in flex). We have to set 2 paramaters in our info panel ( apple-key+I ) in the build tab( note: make sure you select the &#8220;all configurations&#8221; under configuration so you don&#8217;t have to worry about the difference between debugging and releasing.</p>
<p><span style="color: #ff9900;"><strong>a. &#8220;Additional SDKs&#8221; must be set to &#8220;$HOME/Library/SDKs/JSON/$(PLATFORM_NAME).sdk&#8221;<br />
b. &#8220;Other Linker Flags&#8221; must be set to &#8220;-ObjC -ljson&#8221;</strong></span></p>
<p>So the JSON-framework is now a part of our current project. Just to be sure its working open up your appdelegate class and add the following line code at the top of your class.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import</span>
<span style="color: #6e371a;">#import &quot;Person.h&quot;</span></pre></div></div>

<p>*note: we add <strong>Person.h</strong> because we are going to need it further on in the project but it has no relation to the JSON framework.</p>
<p>Now try to build your project( apple-key + B ) if you don&#8217;t get any errors you than your setup to go. If you do get any errors, you can always post them as a comment or mail me and I will see what I can do. In the DMG you downloaded is an INSTALL and README file, you can always check those files if you still get errors.</p>
<p>So assuming were setup and we don&#8217;t get any errors. We continue with our example. Just add the following code to the <span style="color: #666699;"><strong>applicationDidFinishLaunching</strong></span> Method.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSURLResponse</span> <span style="color: #002200;">*</span>urlResponse;
&nbsp;
<span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>data <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLConnection</span> sendSynchronousRequest<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLRequest</span> requestWithURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://andyj.be/iphone/json/persons.php&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> returningResponse<span style="color: #002200;">:&amp;</span>amp;urlResponse error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>jsonData <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> alloc<span style="color: #002200;">&#93;</span> initWithData<span style="color: #002200;">:</span>data encoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span>;
&nbsp;
SBJSON <span style="color: #002200;">*</span>json <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>SBJSON alloc<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>jsonArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>json objectWithString<span style="color: #002200;">:</span>jsonData error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i<span style="color: #002200;">=</span><span style="color: #2400d9;">0</span>; i<span style="color: #002200;">&amp;</span>lt;<span style="color: #002200;">&#91;</span>jsonArray count<span style="color: #002200;">&#93;</span>; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">id</span> tmp <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>jsonArray objectAtIndex<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span>;
    Person <span style="color: #002200;">*</span>p <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Person alloc<span style="color: #002200;">&#93;</span> initWithName<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>tmp valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;name&quot;</span><span style="color: #002200;">&#93;</span> andAge<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>tmp valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;age&quot;</span><span style="color: #002200;">&#93;</span> intValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@ is %i years old.&quot;</span>, <span style="color: #002200;">&#91;</span>p name<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>p age<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>p release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#91;</span>json release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>jsonData release<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>If you take a glance at the code, it might be a little strange at first sight. But if you give it a better look you will see that it&#8217;s actually pretty much the same path we are following as we would do in AS3.</p>
<p>We start by instantiating a pointer to <strong>urlResponse</strong> of type <strong>NSURLResponse</strong> because we are going to need that pointer in the next line of code. What it actually does is just addresses a pointer to a variable, it will be necessary for when we load some data, so that we can provide this pointer to the function we want to load data with.</p>
<p>Next we instantiate a variable called data of the type <strong>NSData</strong> that&#8217;s because were calling the class method <strong><span style="color: #666699;">sendSynchronousRequest:returningResponse:error:</span></strong> of the <strong>NSURLConnection</strong> object which will return an object of the type <strong>NSData</strong>. So since we use a class method we don&#8217;t need to allocate an object of the <strong>NSURLConnection</strong> class, so we need to feed the function 3 paramaters, the first is a <strong>NSURLRequest</strong> which we need to provide with a <strong>NSURL</strong> through the <span style="color: #666699;"><strong>requestWithURL</strong></span> method, and in the <strong>NSURL</strong> we need to provide the actual url to the webservice through the function <span style="color: #666699;"><strong>URLWithString</strong></span>. We need to provide the <strong>returningResponse</strong> with the earlier created <strong>urlResponse</strong> pointer, note that we use and <strong>ampersand &#8220;&amp;&#8221;</strong> in front of the variable which denotes <em>pass-by-reference</em>. (I&#8217;m not going to go deeper on that topic but if you want to know more about it just use this article as a start point <a href="http://en.wikipedia.org/wiki/Reference_(C%2B%2B)" target="_blank">http://en.wikipedia.org/wiki/Reference_(C%2B%2B)</a> it&#8217;s for c++ but has the sample principle.)</p>
<p>Since our JSON class needs a string for decoding our json we need to convert our data into a string we do that by using the <span style="color: #888888;"><strong><span style="color: #666699;">initWithData:encoding:</span> </strong></span>method from the <strong>NSString</strong> class we provide that the data and for encoding use the <strong>NSUTF8StringEncoding</strong> constant.</p>
<p>Now we have our data ready to be decoded we just have to allocate a variable of the type <strong>SBJSON</strong> which is a class from the JSON framework. And just like in actionscript we know that it will be an array so we declare a variable <strong>jsonArray</strong> of the type <strong>NSArray</strong> ( note: if you don&#8217;t know for sure which type it will be you can use &#8220;<strong>id</strong>&#8221; instead of <strong>NSArray</strong> than you will be sure your program doesn&#8217;t crash at runtime, but in our case I know I&#8217;m sure!) for decoding we just use the <span style="color: #666699;"><strong>objectWithString:error:</strong></span> method and provide nil for error since we don&#8217;t care for any errors now.</p>
<p>Make a for loop that iterates through every object in the array. And in every object there will be a key and a value for each property in the json string ( just like in actionscript).</p>
<p>Allocate youre Person object and juse the function <span style="color: #666699;"><strong>initWithName:andAge:</strong></span> to put the values of the tempory object into you person object. ( note, that we need to convert the age object into an <strong>intValue</strong> ( just like we did in the XML example ) because in our backend they all are strings ).</p>
<p>Release the allocated objects from the memory, eh voila. Open your debug log window and see the magic happen. In the next tutorial I will be discussion how to use json to send data from the iphone to the backend.</p>
<h3><strong><em>In the mean time a happy Christmas to all of you.</em></strong></h3>
]]></content:encoded>
			<wfw:commentRss>http://andyj.be/blog/?feed=rss2&amp;p=52</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>AS3 to Cocoa touch: XML</title>
		<link>http://andyj.be/blog/?p=37</link>
		<comments>http://andyj.be/blog/?p=37#comments</comments>
		<pubDate>Thu, 18 Dec 2008 16:40:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[cocoa touch]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[objc]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://andyj.be/blog/?p=37</guid>
		<description><![CDATA[For this tutorial were going to discus XML Parsing. I&#8217;m not totally fond of the xml functionality&#8217;s in objective c. I use XML all the time in actionscript because its so easy and powerfull with E4X.  Although there are some open source initiatives for xml parsing. I find it hard to implement them into my [...]]]></description>
			<content:encoded><![CDATA[<p>For this tutorial were going to discus XML Parsing. I&#8217;m not totally fond of the xml functionality&#8217;s in objective c. I use XML all the time in actionscript because its so easy and powerfull with E4X.  Although there are some open source initiatives for xml parsing. I find it hard to implement them into my projects so I keep it with the standard XML class provided by the cocoa touch framework. So we <strong>NSXMLParser</strong>, and.. oh wait that&#8217;s the only one. <strong>NSXMLDocument</strong>, <strong>NSXMLElement</strong>, .. won&#8217;t work on the iphone, I didn&#8217;t test it but what I have read on the forums about it will work in the iphone simulator but not on the iphone itself. ( by the way if someone knows a great/simple api for xml parsing on the iphone let me know <img src='http://andyj.be/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> )</p>
<p>So for this tutorial we are going to stick with the <strong>NSXMLParser</strong> class.<br />
Actually the <strong>NSXMLParser</strong> isn&#8217;t so bad at all if you start working with it.<br />
The basic idea behind the <strong>NSXMLParser</strong> class is that it iterates through every element in your XML.<br />
And it uses a Delegate to invoke the methods needed but that will become more clear when we dive into it.</p>
<p>So our user story will be: we have an XML of a couple of person with there name and an attribute with there age, so you can simple use the classes from my previous examples.<span id="more-37"></span></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">#Actionscript
<span style="color: #6699cc; font-weight: bold;">var</span> xmlLoader <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">URLLoader</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLLoader</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
xmlLoader.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">COMPLETE</span>,xmlLoadComplete<span style="color: #000000;">&#41;</span>;
xmlLoader.<span style="color: #004993;">load</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLRequest</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;http://andyj.be/iphone/examples/xml/persons.xml&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> xmlLoadComplete<span style="color: #000000;">&#40;</span> e <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Event</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> persons <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Array</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// Our Array that we want to fill up with Person Objects</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> xml <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">XML</span> = <span style="color: #004993;">XML</span><span style="color: #000000;">&#40;</span> e.<span style="color: #004993;">target</span>.<span style="color: #004993;">data</span> <span style="color: #000000;">&#41;</span>;
      <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span><span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> xmlPerson <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">XML</span> <span style="color: #0033ff; font-weight: bold;">in</span> xml.person <span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
          <span style="color: #6699cc; font-weight: bold;">var</span> person <span style="color: #000000; font-weight: bold;">:</span> Person = <span style="color: #0033ff; font-weight: bold;">new</span> Person<span style="color: #000000;">&#40;</span> xmlPerson.<span style="color: #004993;">name</span>, xmlPerson.@age <span style="color: #000000;">&#41;</span>;
          persons.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> person <span style="color: #000000;">&#41;</span>;
      <span style="color: #000000;">&#125;</span>
      <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> persons <span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>So very straight forward, make an URLLoader object, put an addEventListener for Complete on it load the url. And in the function we make a new array where we are going to store our objects in it. Retrieve the xml data from the target of the event (don&#8217;t forgot to cast it to XML) and iterate it with a for loop on xml. person which will loop over every element &#8220;person&#8221;. Make an object of type Person and put the values in it. Push it into the array. Et voila in about 10 lines of code we have our xml stored into objects.</p>
<p>So lets bring that to Objective-c</p>
<p>We want to make use of our Person class early created it has 2 property&#8217;s that we need to fill up with the values from the xml so we don&#8217;t need to make any changes to that class so if your following. I will be providing code for this example as it may be more readable in XCode than it is on my blog. So if your just interested in my current post you don&#8217;t have to worry about the Person class and just download the project at the bottom of my article.</p>
<p>So lets continue shall we.. I&#8217;ve mentioned that NSXMLParser uses Delegate to invoke his methods. So we there are 3 methods we should implement to our class.</p>
<blockquote><p><em>- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict</em></p>
<p><em>- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName</em></p>
<p><em>- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string</em></p></blockquote>
<p>I will explain them based on the code we shall write for the example.</p>
<p>So for setup we make a new <strong><em>window-based Application project</em></strong> (for the iphone since were learning cocoa touch). So were going to add 2 classes the one is <em><strong>Person</strong></em> <strong>(Person.h / Person.m)</strong> that we made in a previous example, and we are going to make a new Class called &#8220;<strong>XMLController</strong>&#8221; which is going to handle the XML parsing itself so we can separate our business logic. So make a new class that with the <em><strong>NSObject subclass</strong> template</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//XMLController.h</span>
<span style="color: #6e371a;">#import</span>
<span style="color: #a61390;">@class</span> Person;
&nbsp;
<span style="color: #a61390;">@interface</span> XMLController <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
      <span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>currentNodeContent;
      <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>persons;
      <span style="color: #400080;">NSXMLParser</span> <span style="color: #002200;">*</span>parser;
      Person <span style="color: #002200;">*</span>currentPerson;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>readonly,retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>persons;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>loadXMLByURL<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>urlString;
<span style="color: #a61390;">@end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//XMLController.m</span>
<span style="color: #6e371a;">#import &quot;XMLController.h&quot;</span>
<span style="color: #6e371a;">#import &quot;Person.h&quot;</span>
<span style="color: #a61390;">@implementation</span> XMLController
<span style="color: #a61390;">@synthesize</span> persons;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>loadXMLByURL<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>urlString<span style="color: #002200;">&#123;</span>
      persons <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
      <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>url <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span> urlString<span style="color: #002200;">&#93;</span>;
      parser <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSXMLParser</span> alloc<span style="color: #002200;">&#93;</span> initWithContentsOfURL<span style="color: #002200;">:</span>url<span style="color: #002200;">&#93;</span>;
      parser.delegate <span style="color: #002200;">=</span> self;
      <span style="color: #002200;">&#91;</span>parser parse<span style="color: #002200;">&#93;</span>;
      <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>parser<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSXMLParser</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parser didStartElement<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>elementName namespaceURI<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>namespaceURI qualifiedName<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>qName attributes<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>attributeDict
<span style="color: #002200;">&#123;</span>
      <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>elementName isEqualToString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;person&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
            currentPerson <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Person alloc<span style="color: #002200;">&#93;</span>;
            currentPerson.age <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>attributeDict valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;age&quot;</span><span style="color: #002200;">&#93;</span> intValue<span style="color: #002200;">&#93;</span>;
            currentNodeContent <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
      <span style="color: #002200;">&#125;</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>parser<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSXMLParser</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parser didEndElement<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>elementName namespaceURI<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>namespaceURI qualifiedName<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>qName
<span style="color: #002200;">&#123;</span>
      <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>elementName isEqualToString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;name&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
            <span style="color: #002200;">&#91;</span>currentPerson setValue<span style="color: #002200;">:</span>currentNodeContent forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;name&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
            <span style="color: #002200;">&#91;</span>persons addObject<span style="color: #002200;">:</span>currentPerson<span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#91;</span>currentPerson release<span style="color: #002200;">&#93;</span>;
            currentPerson <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
            <span style="color: #002200;">&#91;</span>currentNodeContent release<span style="color: #002200;">&#93;</span>;
            currentNodeContent <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
      <span style="color: #002200;">&#125;</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>parser<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSXMLParser</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parser foundCharacters<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><span style="color: #a61390;">string</span>
<span style="color: #002200;">&#123;</span>
      <span style="color: #002200;">&#91;</span>currentNodeContent appendString<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #a61390;">string</span> stringByTrimmingCharactersInSet<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSCharacterSet</span> whitespaceAndNewlineCharacterSet<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>So in the XMLController.h file we specify our variables and methods.</p>
<ul>
<li><span style="color: #008080;">currentNodeContent</span> is the variable we are going to store our node values into it, it is of type NSMutableString because we want the string to by dynamic so we can append strings to it rather than a simple static string.</li>
<li><span style="color: #008080;">persons</span> is an mutableArray where we are going to add our different person objects into it. It&#8217;s the only variable we are going to define as a property (readonly,retain) because we only want to read the array outside our class, not modify it.</li>
<li><span style="color: #008080;">parser</span> is our XMLParser object that is going to handle the delegates for parsing the xml</li>
<li><span style="color: #008080;">currentPerson</span> of type Person is going to hold the current Person values for that xml node. It will be released when its complete and added to the persons array.</li>
</ul>
<p>- (<span style="color: #ff00ff;">id</span>)loadXMLByURL:(<span style="color: #cc99ff;">NSString</span> *)urlString;<br />
is the method were adding to let our XMLController class initialze itself (like a constructor in actionscript).</p>
<p>On to the implementation file. We synthesize our person variable so our getter is automatically generated. Then on to the implementation for our <strong>loadXMLByURL</strong> method whe are going to allocate the persons array just like we would say in actionscript new Array();</p>
<p>We make a NSURL object with the string provided by the methods argument( wich contains the url ) it is the same as making an URLRequest object in actionscript.</p>
<p>We allocate the <strong>NSXMLParser</strong> and we instantly use the <strong>initWithContentsOfURL</strong> method which is going to load the xml from a URL. We assign our delegation class by telling the parser our <strong><em>delgate = self ( self = this in actionscript) </em></strong>which means our delegation methods will be invoked in the this class. And we return the class itself.</p>
<p>Now we are going to setup our 3 delegation methods mentioned earlier.<br />
So what the <strong>NSXMLParser</strong> is going to do is iterate through every element of your xml so in our case first persons, then person, then name, then person.</p>
<p>The <strong>didStartElement</strong> is going to be invoked whenever a element is opened, and the <strong>didEndElement</strong> will be invoked whenever it finds a closing element in the xml.</p>
<p>So for our case it will be:</p>
<blockquote>
<ol>
<li><strong><span style="color: #000000;">persons</span></strong> -&gt; <span style="color: #808000;">didStartElement</span></li>
<li><strong>person</strong> -&gt; <span style="color: #808000;">didStartElement</span></li>
<li><strong>name</strong> -&gt; <span style="color: #808000;">didStartElement</span></li>
<li><strong>name</strong> -&gt; <span style="color: #458bba;">didEndElement</span></li>
<li><strong>person</strong> -&gt; <span style="color: #458bba;">didEndElement</span></li>
<li><strong>person</strong> -&gt; <span style="color: #808000;">didStartElement</span><br />
and so on..</li>
</ol>
</blockquote>
<p>So in our function we are just going to search for the elementName we want.<br />
Notice that I don&#8217;t use <em>Comparison operator </em>to check if the <strong>elementName</strong> is the same as &#8220;person&#8221; like I would do in actionscript. People who have a background in java know that you cannot compare strings using the comparison operator, instead we are using the <strong>isEqualToString</strong> method.</p>
<p>Once we found our person element we are going to allocate our <strong>currentPerson</strong> variable because we now know that every element that follows will be a needed to store in our Person object.<br />
Every element that has attributes in our xml will be stored in the <strong>attributeDict</strong>. The only thing we have to do now is use the method <strong>valueForKey</strong> because <strong>NSXMLParser</strong> will store the values of the attributes with the keys that are defined in the xml, but because our age property is of type int we use the <strong>NSString</strong> method <strong>intValue</strong> to convert our string to an int.</p>
<p>We could search for the next element we want data from that would be the element name, but instead were just going to allocate currentNodeContent (wich will collect the data between the nodes) in the same if structure. Note that it our currentNodeContent will collect all the whitespaces and newlines but we will erase that later on.</p>
<p>So in our didEndElement method we are going to search that elementName is equal to name .. so what happens behind the scene.</p>
<blockquote>
<ol>
<li><strong>person</strong> -&gt; <span style="color: #808000;">didStartElement</span></li>
<li><span style="color: #ff9900;">we instantiate our currentPerson object</span></li>
<li><strong>name</strong> -&gt; <span style="color: #808000;">didStartElement</span></li>
<li><span style="color: #ff9900;">foundCharacters method is invoked and will append all values it finds between the nodes to our currentNodeContent string*</span></li>
<li><strong>name</strong> -&gt; <span style="color: #458bba;">didEndElement</span></li>
</ol>
</blockquote>
<p><em>*note that we use <strong>stringByTrimmingCharactersInSet</strong> using the <strong>whitespaceAndNewlineCharacterSet</strong> <strong>NSCharacterSet</strong> object so that will automaticly erase whitespace and newlines.</em></p>
<p>So when our <strong>didEndElement</strong> method for element name is invoked we know that our <strong>currentNodeValue</strong> contains the data between the name nodes. So we are going to assign it to our name property of our <strong>currentPerson</strong> object. ( I could use <span style="color: #993300;"><em>currentPerson.name = currentNodeContent;</em></span> for assigning it I use a different approach so you can see that you can assign your values in very different ways )</p>
<p>We add our object to our persons array and we release both <strong>currentPerson</strong> and <strong>currentNodeContent</strong> and assign it to nil. So its cleared completely and released from the memory.</p>
<p>Ok that&#8217;s for the parsing part of our array. Now we still have to do something with our data.</p>
<p>Since we made a window based Application XCode will generate an <em>AppDelegate class</em> in my case <strong>PersonXMLAppDelegate.h</strong> and <strong>PersonXMLAppDelegate.m</strong></p>
<p>So the only thing we must do now is to edit the applicationDidFinishLaunching method so we can do something with our data ( the only thing we are going to do with it, is tracing it to the output (or Debugger console called in XCode ( shift + apple + R ))</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//PersonXMLAppDelegat.m</span>
XMLController <span style="color: #002200;">*</span>xmlcontr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>XMLController alloc<span style="color: #002200;">&#93;</span> loadXMLByURL<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://andyj.be/iphone/examples/xml/persons.xml&quot;</span><span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>,<span style="color: #002200;">&#91;</span>xmlcontr persons<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>Person <span style="color: #002200;">*</span>p <span style="color: #a61390;">in</span> <span style="color: #002200;">&#91;</span>xmlcontr persons<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
      NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@ is %i years old.&quot;</span>, <span style="color: #002200;">&#91;</span>p name<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>p age<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p><em>*note: don&#8217;t forget to import XMLController.h and Person.h</em></p>
<p>We allocate our <strong>XMLController</strong> class. And use the <strong>loadXMLByURL</strong> earlier created and pass it the url to our xml. In our <strong>XMLController</strong> class we defined 1 property called person that is <em>readonly</em>, so we can now just acces that property and trace it with <strong>NSLog</strong>. For the sake of it, I&#8217;m going to trace a string with the values of each person object separately in a for loop.</p>
<p>So that&#8217;s the very basics of parsing XML. If you want a more advanced tutorial you should definitely check out <a title="nsxmlparser" href="http://codesofa.com/blog/archive/2008/07/23/make-nsxmlparser-your-friend.html" target="_blank">this article</a></p>
<p>If you want a more automated way of parsing your xml to objects you should definitely check <a title="nsxmlparser to object" href="http://blog.atrexis.com/index.cfm/2008/7/28/iPhone--Parse-XML-to-custom-objects" target="_blank">this blog post</a> which can save you allot of time!</p>
<h3 style="text-align: center;"><a title="person xml zip" href="http://andyj.be/iphone/examples/PersonXML.zip" target="_blank">download the source files for this example here</a></h3>
]]></content:encoded>
			<wfw:commentRss>http://andyj.be/blog/?feed=rss2&amp;p=37</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>AS3 to Cocoa Touch: Array&#039;s</title>
		<link>http://andyj.be/blog/?p=25</link>
		<comments>http://andyj.be/blog/?p=25#comments</comments>
		<pubDate>Tue, 16 Dec 2008 13:01:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[cocoa touch]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[objective c]]></category>

		<guid isPermaLink="false">http://andyj.be/blog/?p=25</guid>
		<description><![CDATA[So for this topic I will discuss Array&#8217;s. So defined by wikipedia
&#8220;In computer science an array is a data structure consisting of a group of elements that are accessed by indexing. In most programming languages each element has the same data type and the array occupies a contiguous area of storage.&#8221;
So we all know that [...]]]></description>
			<content:encoded><![CDATA[<p>So for this topic I will discuss Array&#8217;s. So defined by wikipedia</p>
<blockquote><p>&#8220;In computer science an array is a data structure consisting of a group of elements that are accessed by indexing. In most programming languages each element has the same data type and the array occupies a contiguous area of storage.&#8221;</p></blockquote>
<p>So we all know that when we have an array in Actionscript we don&#8217;t have to fill it up with objects of the same data type&#8217;s, we can use any data type. We&#8217;re not restricted. That&#8217;s the same in Objective-C. Except 1 little thing. In objective-C if we want to fill up an array it can only be with Objects, not primitives (like int or float, ..). So what we will do is take a couple of examples using an Array in actionscript and converting it to Objective-c. Along the way we will se what is possible and what is not, or what will be different.</p>
<p>So first things first.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">#Actionscript
<span style="color: #6699cc; font-weight: bold;">var</span> p1 <span style="color: #000000; font-weight: bold;">:</span> Person = <span style="color: #0033ff; font-weight: bold;">new</span> Person<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;john&quot;</span>,<span style="color: #000000; font-weight:bold;">15</span><span style="color: #000000;">&#41;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> string <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">String</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">String</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;lol&quot;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> number <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">15</span>;
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> array <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Array</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span>p1,string,number<span style="color: #000000;">&#41;</span>;
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>array<span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//output: [object Person],lol,15</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>array<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//output: 15</span></pre></div></div>

<p>So what we have done is created 3 variables 1 object of the type Person ( see the previous example ). A simple string and a number. Create an Array and put those 3 variables in the array and trace it.</p>
<p>So how would we do that in Objective-c</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#Objective-C</span>
Person <span style="color: #002200;">*</span>p1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Person alloc<span style="color: #002200;">&#93;</span> initWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;joe&quot;</span> andAge<span style="color: #002200;">:</span><span style="color: #2400d9;">34</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">string</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;This is a string&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>number <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">4</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>arr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> alloc<span style="color: #002200;">&#93;</span> initWithObjects<span style="color: #002200;">:</span>p1,<span style="color: #a61390;">string</span>,number,<span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;array : %@&quot;</span>,arr<span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;array object 2: %@&quot;</span>,<span style="color: #002200;">&#91;</span>mArr objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p><span style="color: #000000;"><strong><span>And our output should be:<br />
2008-12-15 21:59:58.615 cocoatests[26361:20b] array : (<br />
<span style="padding-left: 30px;">&lt;Person: 0&#215;524810&gt;,</span><br />
<span style="padding-left: 30px;">&#8220;This is a string&#8221;,</span><br />
<span style="padding-left: 30px;">4</span><br />
)<br />
2008-12-15 21:59:58.615 cocoatests[26361:20b] array object 2: (<br />
<span style="padding-left: 30px;">4</span><br />
)</span></strong></span><br />
So what we do is exactly the same as in actionscript. Create 3 variables 1 of the type Person instantiate it with 2 parameters &#8220;joe&#8221; &amp; 34 (as being a method defined in the interface of the Class Person ( if you want to know more about that or it is a little unclear just go back to my previous post about Objects and methods )) We make a String and A Number.</p>
<p>So for our &#8220;array part&#8221;. We make a new variables of the type NSArray  we allocate it to reserve space in the memory, and we immediately initialize it with some objects (being the objects created before). Doing this by using the method initWithObjects: followed by the objects we want to put in our array as a set of parameters. If you look closely enough you would see that we end our set of parameters with &#8220;nil&#8221;. This is because when we use initWithObjects, under the hood it iterates over every object until it finds nil so it knows it is at the end of the array. Everything that you put behind nil will not be pushed into your array.</p>
<p>So next on.. we want to add some more data to our array.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">#Actionscript
array.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;foo&quot;</span><span style="color: #000000;">&#41;</span>;
array.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;bar&quot;</span><span style="color: #000000;">&#41;</span>;
array.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">12</span><span style="color: #000000;">&#41;</span>;
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>array<span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//output: [object Person],lol,15,foo,bar,12</span></pre></div></div>

<p>We us the push method to push new data in to our array (wich still can be of any datatype)</p>
<p>#Objective-C<br />
So at our objective C side we cannot just add new data to an object of type NSArray, instead we have to use NSMutableArray. That&#8217;s because NSArray creates static arrays, and NSMutableArray creates dynamic arrays. Actually NSMutableArray is a subclass of NSArray wich adds extra functionality to NSArray such as push or remove new objects to our array.</p>
<p>So what we can do is create a new NSMutableArray or just push our already created array into our new NSMutableArray</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#Objective-C</span>
<span style="color: #11740a; font-style: italic;">//Method 1</span>
<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>mArr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithObjects<span style="color: #002200;">:</span>p1,<span style="color: #a61390;">string</span>,number,<span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//Method 2</span>
<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>mArr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithArray<span style="color: #002200;">:</span>arr<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>mArr addObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;foo&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>mArr addObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;bar&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>mArr addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">15</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;array : %@&quot;</span>,mArr<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Which would result:<br />
<strong>2008-12-15 22:24:26.883 cocoatests[26560:20b] array : (<br />
<span style="padding-left: 30px;">&lt;Person: 0&#215;5229e0&gt;,</span><br />
<span style="padding-left: 30px;">&#8220;This is a string&#8221;,</span><br />
<span style="padding-left: 30px;">4,</span><br />
<span style="padding-left: 30px;">foo,</span><br />
<span style="padding-left: 30px;">bar,</span><br />
<span style="padding-left: 30px;">15</span><br />
)</strong></p>
<p>So what we have done is used the addObject method to add objects to our NSMutableArray. Simple as that. So for removing. We use the same principle only that in objective-c removing or replacing is better supported.</p>
<p>If we would like to remove the first object and the object called &#8220;foo&#8221; in actionscript we would do it like:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">#Actionscript
array.<span style="color: #004993;">splice</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span>;
array.<span style="color: #004993;">splice</span><span style="color: #000000;">&#40;</span>array.<span style="color: #004993;">indexOf</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;foo&quot;</span><span style="color: #000000;">&#41;</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>array<span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//output: lol,15,bar,12</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#Objective-C</span>
<span style="color: #002200;">&#91;</span>mArr removeObjectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>mArr removeObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;foo&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;array : %@&quot;</span>,mArr<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p><strong>2008-12-15 22:39:14.451 cocoatests[26689:20b] array : (<br />
<span style="padding-left: 30px;">&#8220;This is a string&#8221;,</span><br />
<span style="padding-left: 30px;">4,</span><br />
<span style="padding-left: 30px;">1,</span><br />
<span style="padding-left: 30px;">15</span><br />
)</strong><br />
if you want to know all the add/remove/replace function just go to <a title="NSMutableArray reference" href="https://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/occ/cl/NSMutableArray there you will find all the implementations." target="_blank">NSMutableArray Reference</a><br />
there you will find all the implementations.</p>
<p>So what about sorting ?</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">#Actionscript
<span style="color: #6699cc; font-weight: bold;">var</span> poets<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;Blake&quot;</span>, <span style="color: #990000;">&quot;cummings&quot;</span>, <span style="color: #990000;">&quot;Angelou&quot;</span>, <span style="color: #990000;">&quot;Dante&quot;</span><span style="color: #000000;">&#93;</span>;
poets.<span style="color: #004993;">sort</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// default sort</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>poets<span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// output: Angelou,Blake,Dante,cummings</span>
&nbsp;
poets.<span style="color: #004993;">sort</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Array</span>.<span style="color: #004993;">CASEINSENSITIVE</span><span style="color: #000000;">&#41;</span>;
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>poets<span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// output: Angelou,Blake,cummings,Dante</span>
&nbsp;
poets.<span style="color: #004993;">sort</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Array</span>.<span style="color: #004993;">DESCENDING</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #004993;">Array</span>.<span style="color: #004993;">CASEINSENSITIVE</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// use two options</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>poets<span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// output: Dante,cummings,Blake,Angelou</span></pre></div></div>

<p>The Array class in Actionscript provides you with a couple of static constants that can be given as an argument to the sort method. It simply takes the array and sort it depending on the argument you pass it.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#Objective-C</span>
<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>poets <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> alloc<span style="color: #002200;">&#93;</span> initWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Blake&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;cummings&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Angelou&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Dante&quot;</span>,<span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>,<span style="color: #002200;">&#91;</span>poets sortedArrayUsingSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>compare<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>,<span style="color: #002200;">&#91;</span>poets sortedArrayUsingSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>caseInsensitiveCompare<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #400080;">NSSortDescriptor</span> <span style="color: #002200;">*</span>sortDescriptor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSSortDescriptor</span> alloc<span style="color: #002200;">&#93;</span> initWithKey<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> ascending<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span> selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>caseInsensitiveCompare<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>,<span style="color: #002200;">&#91;</span>poets sortedArrayUsingDescriptors<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObject<span style="color: #002200;">:</span>sortDescriptor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>So this code creates exactly the same output as we would have in actionscript.</p>
<p>So for starters the sorting methods we use on the array doesn&#8217;t change the array itself as it does in actionscript but it returns a new array. What we can do is instantiate a new array. But for this example as I&#8217;m only showing the output of the array i skip that.</p>
<p>So if we look to the reference we see that the NSArray has a couple of methods to do sorting on. I&#8217;m going to discuss the 2 most used as from my experience.</p>
<p>The first one is the use of selectors (Selectors are used for method invocation) we use the method <strong>sortedArrayUsingSelector</strong> and we pass the selector we want to use by telling it is a selector <span style="color: #000000;"><strong>@selector( &lt;your selector &gt; ).</strong></span></p>
<p>The compare: selector is just going to sort it A-Z / 1-10 (and is defined in NSString, NSNumber..)<br />
what gives the same result as the default sort method in actionscript.</p>
<p>If we wanted to use the <strong>caseInsesitiveCompare</strong> we just use that selector ( which is defined in NSString)</p>
<p>No for our next example being descending / caseInsensitive we are going to make use of <strong>Descriptors</strong> which are just separated objects that describe what sorting we want to take place on our array.</p>
<p>For that we use the <strong>NSSortDescriptor</strong> object. We allocate it you must initialize it with a key (I use nil because we don&#8217;t need a key) we pass the NO argument to ascending and we make us of a selector <strong><span style="color: #000000;">caseInsensitiveCompare</span></strong> like we did in our previous example. Now that our object is created we want to tell the array to sort on that descriptor we use the method <strong>sortedArrayUsingDescriptors</strong> (it expects an array so we put feed it an array created with our sortDescriptor object init). If we would log that you would see that it would result in a descending / caseInsensitive output.</p>
<p>So that was it for array&#8217;s for now. There is a lot more to cover, if you want to see something more clearly just leave a comment, or if something isn&#8217;t clear enough, let me know!</p>
<p>Next topic will be XML Parsing.</p>
]]></content:encoded>
			<wfw:commentRss>http://andyj.be/blog/?feed=rss2&amp;p=25</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Cocoa touch for actionscript developers (basics)</title>
		<link>http://andyj.be/blog/?p=19</link>
		<comments>http://andyj.be/blog/?p=19#comments</comments>
		<pubDate>Thu, 04 Dec 2008 19:31:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[cocoa touch]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[cocoa]]></category>

		<guid isPermaLink="false">http://andyj.be/blog/?p=3</guid>
		<description><![CDATA[So in the (near) future i will discuss some iphone SDK topics seen through the eye of an Actionscript developer. so what I&#8217;m planning to do is take a real live example (e.g.: Array Sorting, loading XML, drawing,..) and provide you with the actionscript version and convert it to objective C / iphone SDK, if [...]]]></description>
			<content:encoded><![CDATA[<p>So in the (near) future i will discuss some iphone SDK topics seen through the eye of an Actionscript developer. so what I&#8217;m planning to do is take a real live example (e.g.: Array Sorting, loading XML, drawing,..) and provide you with the actionscript version and convert it to objective C / iphone SDK, if there are some specific topics .. just leave a comment and i will definitely  consider blogging about it.</p>
<p>It will be a series of specific topics, for the general work-flow/development I recommend you to use google or youtube. My topics will be mainly focused on developing with the iphone sdk through the eyes of an Actionscript mind.</p>
<p>But before we start our first topic i would like to give some basic knowledge about obj-c.<br />
Objects<br />
So for example we have an Object which describes some person information.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">#Actionscript
<span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Person extends <span style="color: #004993;">Object</span>
     <span style="color: #000000;">&#123;</span>
          <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _name     <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">String</span>;
          <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _age    <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">int</span>;
          <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Person<span style="color: #000000;">&#40;</span><span style="color: #004993;">name</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>, age<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000000;">&#41;</span>
          <span style="color: #000000;">&#123;</span>
             _name = <span style="color: #004993;">name</span>;
             _age = age;
          <span style="color: #000000;">&#125;</span>
          <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> <span style="color: #004993;">name</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">String</span>
          <span style="color: #000000;">&#123;</span>
             <span style="color: #0033ff; font-weight: bold;">return</span> _name;
          <span style="color: #000000;">&#125;</span>
          <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> age<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">int</span>
          <span style="color: #000000;">&#123;</span>
             <span style="color: #0033ff; font-weight: bold;">return</span> _age;
          <span style="color: #000000;">&#125;</span>
          <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">set</span> <span style="color: #004993;">name</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">value</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">String</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span>
          <span style="color: #000000;">&#123;</span>
             _name = <span style="color: #004993;">value</span>;
          <span style="color: #000000;">&#125;</span>
          <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">set</span> age<span style="color: #000000;">&#40;</span> <span style="color: #004993;">value</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">int</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span>
          <span style="color: #000000;">&#123;</span>
            _age = <span style="color: #004993;">value</span>;
          <span style="color: #000000;">&#125;</span>
     <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
     <span style="color: #0033ff; font-weight: bold;">import</span> Person;
     <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
&nbsp;
     <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Main extends <span style="color: #004993;">Sprite</span>
     <span style="color: #000000;">&#123;</span>
         <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
         <span style="color: #000000;">&#123;</span>
         <span style="color: #6699cc; font-weight: bold;">var</span> person <span style="color: #000000; font-weight: bold;">:</span> Person = <span style="color: #0033ff; font-weight: bold;">new</span> Person<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;John&quot;</span>, <span style="color: #000000; font-weight:bold;">32</span> <span style="color: #000000;">&#41;</span>;
         <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> person.<span style="color: #004993;">name</span>, person.age <span style="color: #000000;">&#41;</span>;
         <span style="color: #000000;">&#125;</span>
     <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>So what we have done is create a very simple object where we can store 2 variables.<br />
Which we trace in Main class, so the output would be &#8220;John 32&#8243;</p>
<p>Now lets convert that to Objective C</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">Person.h
<span style="color: #6e371a;">#import</span>
&nbsp;
<span style="color: #a61390;">@interface</span> Person <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> age;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>name;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>readwrite<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">int</span> age;
<span style="color: #a61390;">@end</span>
&nbsp;
Person.m
<span style="color: #6e371a;">#import &quot;Person.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> Person
<span style="color: #a61390;">@synthesize</span> name,age;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>So for the creation of objects / classes you have 2 files, the interface and the implementation. In the interface part we describe the different module variables and functions. But also the type of the object and which delegates/controls the object should implement.</p>
<p>Apple discourages the use of an underscore as a prefix for a private instance variable. The idea of a private prefix is largely unnecessary, as virtually all instance variables in Cocoa are protected anyway.</p>
<p>The asterix in front of &#8220;name&#8221; tells me the variable &#8220;name&#8221; is a pointer  (&#8221;Pointers are designed to hold memory addresses. With a pointer you can indirectly manipulate data stored in other variable.&#8221;) I&#8217;m not going to explain the in&#8217;s and out&#8217;s of pointers but if you like to read more about it i suggest you to read this page (its written in C but has the same principles) http://home.netcom.com/~tjensen/ptr/pointers.htm</p>
<p>We declare the property&#8217;s and define their parameters. If you like to know more about the different parameters i used (http://www.cocoacast.com/?q=node/103)</p>
<p>In the implementation file we synthesize our variables: The @synthesize directive generates accessor methods for us. (  at the given parameters which are declared by the property )</p>
<blockquote><p>Main Application (Just choose the template for a window based application, i&#8217;m only going to discuss the creation of our Person object) (just replace the applicationDidFinishLaunching method)</p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application <span style="color: #002200;">&#123;</span>
&nbsp;
     <span style="color: #11740a; font-style: italic;">// Override point for customization after application launch</span>
     <span style="color: #002200;">&#91;</span>window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
&nbsp;
     <span style="color: #11740a; font-style: italic;">//init Person</span>
     Person <span style="color: #002200;">*</span>person <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Person alloc<span style="color: #002200;">&#93;</span>;
     person.age <span style="color: #002200;">=</span> <span style="color: #2400d9;">32</span>;
     <span style="color: #11740a; font-style: italic;">//person.name = @&quot;John&quot;;</span>
     <span style="color: #002200;">&#91;</span>person setName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;John&quot;</span><span style="color: #002200;">&#93;</span>;
     NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@ %i&quot;</span>,person.name,<span style="color: #002200;">&#91;</span>person age<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
     <span style="color: #002200;">&#91;</span>person release<span style="color: #002200;">&#93;</span>;
     NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@ %i&quot;</span>,person.name,<span style="color: #002200;">&#91;</span>person age<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>So as we can see we need to allocate the variable in the memory,<br />
as we are familiar with in actionscript</p>
<p>New Person(); -&gt; is the same als [Person alloc] (if we want to call the constructor we normally would use [[Person alloc] init])</p>
<p>Since objective C 2 we can access our variables with -dot notation or you can either use the two accessor forms</p>
<p>NSLog is the equivalent of trace in Actionscript .. we define what types we want to output and just pass the paramaters.<br />
<strong></strong></p>
<p><strong>@&#8221;"</strong> defines a string<br />
<strong>%@</strong> lets the formatter know he would expect a string<br />
<strong>%i</strong> lets the formatter know he would expect an integer</p>
<p>if we release the object from the memory it can not be accessed any more</p>
<p>output should be:</p>
<p><strong>2008-12-02 12:21:42.323 basics[1781:20b] John 32</strong></p>
<p><strong>objc[1781]: FREED(id): message age sent to freed object=0&#215;522220</strong></p>
<p>Methods</p>
<p>So we are going to continue our previous example I&#8217;m going to add 3 functions</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">#Actionscript
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> myNameLength<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">int</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #0033ff; font-weight: bold;">return</span> _name.<span style="color: #004993;">length</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> myBodyMassIndex<span style="color: #000000;">&#40;</span> <span style="color: #004993;">length</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span>, weight <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">String</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #990000;">&quot;My BMI is: &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> roundTo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>weight <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">length</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">length</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>,<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> static <span style="color: #339966; font-weight: bold;">function</span> roundTo<span style="color: #000000;">&#40;</span> <span style="color: #004993;">value</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span>, n <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #6699cc; font-weight: bold;">var</span> aft <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">int</span> = <span style="color: #004993;">Math</span>.<span style="color: #004993;">pow</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span>, n<span style="color: #000000;">&#41;</span>;
     <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000; font-weight: bold;">*</span>aft<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>aft;
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#Objective C</span>
Person.h
<span style="color: #6e371a;">#import</span>
&nbsp;
<span style="color: #a61390;">@interface</span> Person <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
<span style="color: #11740a; font-style: italic;">// variables</span>
<span style="color: #002200;">&#125;</span>
<span style="color: #11740a; font-style: italic;">//properties</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>myNameLength;
<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>myBodyMassIndexWithLength<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span>length weight<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span>weight;
<span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span>roundToWithValue<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span>bmi DigitsAfterPoint<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>dap;
<span style="color: #a61390;">@end</span>
Person.m
<span style="color: #6e371a;">#import &quot;Person.h&quot;</span>
<span style="color: #6e371a;">#import &quot;Math.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> Person
<span style="color: #a61390;">@synthesize</span> name,age;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>myNameLength<span style="color: #002200;">&#123;</span>
     <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>name length<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>myBodyMassIndexWithLength<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span>length weight<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span>weight<span style="color: #002200;">&#123;</span>
     <span style="color: #a61390;">float</span> bmi <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>weight<span style="color: #002200;">/</span><span style="color: #002200;">&#40;</span>length<span style="color: #002200;">*</span>length<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
     <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;My BMI is: %f - rounded BMI: %f&quot;</span>,bmi,<span style="color: #002200;">&#91;</span>Person roundToWithValue<span style="color: #002200;">:</span>bmi DigitsAfterPoint<span style="color: #002200;">:</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span>roundToWithValue<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span>bmi DigitsAfterPoint<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>dap<span style="color: #002200;">&#123;</span>
     <span style="color: #a61390;">float</span> aft <span style="color: #002200;">=</span> <span style="color: #a61390;">pow</span><span style="color: #002200;">&#40;</span><span style="color: #2400d9;">10</span>,dap<span style="color: #002200;">&#41;</span>;
     <span style="color: #a61390;">return</span> round<span style="color: #002200;">&#40;</span>bmi<span style="color: #002200;">*</span>aft<span style="color: #002200;">&#41;</span><span style="color: #002200;">/</span>aft;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>So we have 3 functions 2 public functions 1 static in Actionscript.</p>
<p>At the Objective-C side of the story</p>
<p>We have 3 methods 2 &#8220;instance&#8221; Methods and 1 &#8220;class&#8221; Method(The minus sign before (int) indicates that this method is an &#8220;instance&#8221; method. If it were a plus, it would indicate that it is a class method. You could see it more or less like public/static functions in Actionscript)<br />
We usually define them under the @property&#8217;s in the interface</p>
<p>We can&#8217;t actually declare if our methods are private or public and we don&#8217;t have to specify a &#8220;separate method name&#8221; we can just add our parameters. As seen clearly in the second and third method.</p>
<p>for the return value we use a class Method(stringWithFormat) of the class NSString to format our string,</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;My BMI is: %f - rounded BMI: %f&quot;</span>,bmi,<span style="color: #002200;">&#91;</span>Person roundToWithValue<span style="color: #002200;">:</span>bmi DigitsAfterPoint<span style="color: #002200;">:</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span></pre></div></div>

<p><strong>@&#8221;</strong> &lt;- start of the string<br />
<strong>%f </strong>&lt;- expects a float (format specifiers)</p>
<p>most common<br />
<strong>%@ </strong> Object<br />
<strong>%d, %i</strong> signed int<br />
<strong>%u</strong> unsigned int<br />
<strong>%f</strong> float/double</p>
<p>so for all the format specifiers: we add a parameter.</p>
<p>For the last function we specified a class method.<br />
that uses some functions of the Math.h ( so don&#8217;t forget the #import &#8220;Math.h&#8221; at the top)</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%i&quot;</span>,<span style="color: #002200;">&#91;</span>person myNameLength<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>,<span style="color: #002200;">&#91;</span>person myBodyMassIndexWithLength<span style="color: #002200;">:</span><span style="color: #2400d9;">1.7</span> weight<span style="color: #002200;">:</span><span style="color: #2400d9;">51</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%f&quot;</span>,<span style="color: #002200;">&#91;</span>Person roundToWithValue<span style="color: #002200;">:</span><span style="color: #2400d9;">20.796434234</span> DigitsAfterPoint<span style="color: #002200;">:</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>So now if we add those NSLog statements to our applicationDidFinishLaunching method you get the exact logs</p>
<p>We use [&lt;our object&gt; &lt;ourmethod&gt;:&lt;paramaters] to access our &#8220;instance&#8221; methods<br />
- like shown in the first 2 NSLog statements</p>
<p>We use [&lt;our Class&gt; ourmethod:&lt;paramaters] to access our &#8220;class&#8221; methods<br />
- like shown in the last 2 NSLog statements</p>
<p>So that&#8217;s it for now,See you in a next tutorial</p>
]]></content:encoded>
			<wfw:commentRss>http://andyj.be/blog/?feed=rss2&amp;p=19</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>IMDB AS3 Api (beta)</title>
		<link>http://andyj.be/blog/?p=18</link>
		<comments>http://andyj.be/blog/?p=18#comments</comments>
		<pubDate>Mon, 17 Mar 2008 16:15:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[api]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[imdb]]></category>

		<guid isPermaLink="false">http://avalanched.wordpress.com/?p=8</guid>
		<description><![CDATA[As part of a school project about AS3 api&#8217;s i developed a small actionscript api that uses Itzchak Rehberg php classes to parse cache the imdb pages and extract the information from it(with a very cool caching mechanism). i just tweaked it a bit so it parses xml instead of html. there is a working [...]]]></description>
			<content:encoded><![CDATA[<p>As part of a school project about AS3 api&#8217;s i developed a small actionscript api that uses <a href="http://projects.izzysoft.de/">Itzchak Rehberg</a> php classes to parse cache the imdb pages and extract the information from it(with a very cool caching mechanism). i just tweaked it a bit so it parses xml instead of html. there is a working draft copy <a href="http://imdb.full-ops.com/">here</a> if you want to test it just type in a movie title and it will parse an xml wich gives you the all the associated imdb-id&#8217;s for it.</p>
<p>the as3 api now only concludes searching and  fetching movie information.</p>
<p>its as easy as just to</p>
<p><font color="#c0c0c0">var imdbSearch:SearchIMDB = new SearchIMDB(&#8221;scrubs&#8221;,20);<br />
imdbSearch.addEventListener(Event.COMPLETE,serviceLoaded);<br />
imdbSearch.load();</font></p>
<p>and for fetching a movie you can just do</p>
<p><font color="#c0c0c0">movie = new Movie(&lt;enter movie id here&gt;);<br />
movie.addEventListener(MovieEvent.LOADED,movieComplete);<br />
movie.load(); </font></p>
<p>Try not to put in a loop of fetching 500 movies at the same time because it will overload your internet connection. I did a couple of tests <a href="http://imdb.full-ops.com/test/imdb_test_pictures.html" target="_blank">(try it out here)</a> <a href="http://imdb.full-ops.com/test/srcview/">(srcview)</a> i also did test to fetch movie information 1 at the time but it goes like 50% (avg) faster if you just request all at the same time ( just respect a golden rule .. &#8220;don&#8217;t overdo it&#8221; <img src='http://andyj.be/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  )</p>
<p>you can download the <a href="http://users.pandora.be/blf/imdb_api.zip">source files here</a></p>
<p>you can download the <a href="http://users.pandora.be/blf/imdb_php.zip">php source files here </a></p>
<p>note that if you want to use the code that my server is only there for testing purpose and if you do want to setup you own project just copy the files to you php server and change the link. I can only keep my server online as long as it can handle traffic <img src='http://andyj.be/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://andyj.be/blog/?feed=rss2&amp;p=18</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>AS3 Secure zip extension</title>
		<link>http://andyj.be/blog/?p=17</link>
		<comments>http://andyj.be/blog/?p=17#comments</comments>
		<pubDate>Mon, 17 Mar 2008 13:26:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[api]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[bytearray]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[rc4]]></category>
		<category><![CDATA[swfparser]]></category>
		<category><![CDATA[zip]]></category>

		<guid isPermaLink="false">http://avalanched.wordpress.com/?p=6</guid>
		<description><![CDATA[A couple of days ago i was searching for a AS3 zip API that would support encryption.
When i couldn&#8217;t find one i started to extend David Chang&#8217;s  Zip API with the RC4 encryption method made by  Metal Hurlant
What it does is that it encrypts the bytearray provided by the zip API with RC4, [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago i was searching for a AS3 zip API that would support encryption.<br />
When i couldn&#8217;t find one i started to extend <a href="http://nochump.com" title="David Chang" target="_blank">David Chang&#8217;s </a> Zip API with the RC4 encryption method made by  <a href="http://metal.hurlant.com/blog/" title="Metal Hurlant" target="_blank">Metal Hurlant</a></p>
<p>What it does is that it encrypts the bytearray provided by the zip API with RC4, and in the test application you can see that it writes the file to the hard disk. It is not compatible to open in windows/mac environment. But that just my intention, only to open it in flash ( if provided the right key off-course ). Maybe a nice way to secure/compress your assets in 1 file.</p>
<p>my latest test on my mac ( 2.2ghz, 2gb ram) concluded that it could parse/decrypt +500kb/s<br />
so be careful with very big files <img src='http://andyj.be/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /><br />
I&#8217;m open for suggestions and if you find any bugs just let me know <img src='http://andyj.be/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>(in the zip there is also an SWFParser class from <a href="http://flashpanoramas.com/blog/">Denis V. Chumakov</a> it comes in handy if you want to parse an swf-bytearray)</p>
<p><a href="http://users.pandora.be/blf/ZipSecure.zip">download source code </a></p>
<p><i>for the test code just look in the <b>ZipSecureXML_test.as</b> file and you will find out how to load/save an encrypted zip file </i></p>
]]></content:encoded>
			<wfw:commentRss>http://andyj.be/blog/?feed=rss2&amp;p=17</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
