<?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>netinfluence&#039;s blog &#187; optimization</title>
	<atom:link href="http://blog.netinfluence.com/tag/optimization/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.netinfluence.com</link>
	<description>le blog de l&#039;agence netinfluence</description>
	<lastBuildDate>Fri, 13 Jan 2012 10:39:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Closure et fonctions lambda en Objective-C</title>
		<link>http://blog.netinfluence.com/2009/11/12/closure-et-fonctions-lambda-en-objective-c/</link>
		<comments>http://blog.netinfluence.com/2009/11/12/closure-et-fonctions-lambda-en-objective-c/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 22:02:00 +0000</pubDate>
		<dc:creator>macmade</dc:creator>
				<category><![CDATA[create]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[avant-garde]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[communauté]]></category>
		<category><![CDATA[Développement]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[méthodes]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[XCode]]></category>

		<guid isPermaLink="false">http://blog.netinfluence.ch/?p=55</guid>
		<description><![CDATA[De nombreux langages de scripts permettent l'utilisation de «fonctions lambdas», ou encore «fonctions anonymes», concept généralement lié au phénomène appelé «closure».<br />
 Il s'agit de concepts bien connus notamment en JavaScript, ActionScript, ou encore en PHP depuis sa version 5.3.<br />
 Le langage Objective-C offre depuis peu une implémentation de ces deux concepts, appelée «blocks».<br />
 Les blocks sont disponibles depuis Mac OS X 10.6 et l'adoption de Clang.]]></description>
			<content:encoded><![CDATA[<div class="macmade-grey">
<div class="macmade-small"> Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license can be found at: <a href="http://www.gnu.org/copyleft/fdl.html" title="Free Documentation License">http://www.gnu.org/copyleft/fdl.html</a></div>
</div>
<h3>Définitions</h3>
<div>
De nombreux langages de scripts permettent l&#8217;utilisation de «fonctions lambdas», ou encore «fonctions anonymes», concept généralement lié au phénomène appelé «closure».<br />
 Il s&#8217;agit de concepts bien connus notamment en JavaScript, ActionScript, ou encore en PHP depuis sa version 5.3.<br />
 Le langage Objective-C offre depuis peu une implémentation de ces deux concepts, appelée «blocks».<br />
 Les blocks sont disponibles depuis Mac OS X 10.6 et l&#8217;adoption de Clang.
</div>
<p><span id="more-55"></span></p>
<h4>Fonctions anonymes</h4>
<div> Comme son nom l&#8217;indique, une fonction anonyme est une fonction ne possédant pas de nom, ou d&#8217;identifiant. Elle ne contient que son contenu (body), et peut être associée à une variable, pour être ré-utilisée, ou passée en argument d&#8217;une autre fonction. </div>
<div> Ce phénomène est très souvent utilisé dans des langages de scripts, notamment pour des callbacks. </div>
<div> En JavaScript par exemple, imaginons une fonction standard nommée «foo», prenant en paramètre un callback, et l&#8217;exécutant dans son body: </div>
<div class="macmade-code"> <code class="macmade-source"><span class="macmade-code-keyword">function</span> foo( callback )</code><br />
 <code class="macmade-source">{</code><br />
 <code class="macmade-source">    callback();</code><br />
 <code class="macmade-source">}</code> </div>
<div> Il est parfaitement possible de définir une autre fonction standard, et de la passer en paramètre de notre première fonction: </div>
<div class="macmade-code"> <code class="macmade-source"><span class="macmade-code-keyword">function</span> bar()</code><br />
 <code class="macmade-source">{</code><br />
 <code class="macmade-source">    alert( <span class="macmade-code-string">'Hello World!'</span> );</code><br />
 <code class="macmade-source">}</code></p>
<p> <code class="macmade-source">foo( bar );</code> </div>
<div> Le problème dans un tel cas est que nous déclarons une fonction nommée «bar» dans le scope global. Il en découle un risque de collision avec une éventuelle autre fonction qui porterait le même nom. </div>
<div> Le langage JavaScript nous permet donc de déclarer la fonction utilisée comme callback lors de l&#8217;appel: </div>
<div class="macmade-code"> <code class="macmade-source">foo(</code><br />
 <code class="macmade-source">    <span class="macmade-code-keyword">function</span>()</code><br />
 <code class="macmade-source">    {</code><br />
 <code class="macmade-source">        alert( <span class="macmade-code-string">'Hello World!'</span> );</code><br />
 <code class="macmade-source">    }</code><br />
 <code class="macmade-source">);</code> </div>
<div> Notre callback n&#8217;a ici pas d&#8217;identifiant. Il n&#8217;existera pas dans le scope global, et donc ne risquera pas d&#8217;entrer en conflit avec une fonction existante. </div>
<div> Il est également possible de définir le callback comme variable. Il n&#8217;existera toujours pas dans le scope global, mais pourra ainsi être ré-utilisé à volonté via cette variable: </div>
<div class="macmade-code"> <code class="macmade-source">myCallback = <span class="macmade-code-keyword">function</span>()</code><br />
 <code class="macmade-source">{</code><br />
 <code class="macmade-source">    alert( <span class="macmade-code-string">'Hello World!'</span> );</code><br />
 <code class="macmade-source">};</code></p>
<p> <code class="macmade-source">foo( myCallback );</code> </div>
<h4>Closure</h4>
<div> Le phénomène appelé «closure» consiste en la possibilité, pour une fonction, d&#8217;accéder aux variables disponibles dans son contexte de déclaration, ceci même dans le cas où son contexte d&#8217;exécution est différent. </div>
<div> Toujours en JavaScript, imaginons le code suivant: </div>
<div class="macmade-code"> <code class="macmade-source"><span class="macmade-code-keyword">function</span> foo( callback )</code><br />
 <code class="macmade-source">{</code><br />
 <code class="macmade-source">    alert( callback() );</code><br />
 <code class="macmade-source">}</code></p>
<p> <code class="macmade-source"><span class="macmade-code-keyword">function</span> bar()</code><br />
 <code class="macmade-source">{</code><br />
 <code class="macmade-source">    <span class="macmade-code-keyword">var</span> str = <span class="macmade-code-string">'Hello World'</span>;</code></p>
<p> <code class="macmade-source">    foo(</code><br />
 <code class="macmade-source">        <span class="macmade-code-keyword">function</span>()</code><br />
 <code class="macmade-source">        {</code><br />
 <code class="macmade-source">            <span class="macmade-code-keyword">return</span> str;</code><br />
 <code class="macmade-source">        }</code><br />
 <code class="macmade-source">    );</code><br />
 <code class="macmade-source">}</code></p>
<p> <code class="macmade-source">bar();</code> </div>
<div> Notre callback, passé à la fonction «foo» depuis le contexte d&#8217;exécution de le fonction «bar» retourne une variable nommée «str».<br />
 Or, cette variable, déclarée dans le contexte de la fonction «bar», est une variable locale à cette dernière. Autrement dit, elle n&#8217;existe qu&#8217;à l&#8217;intérieur de ce contexte.<br />
 Comme notre callback est exécuté depuis un contexte différent du contexte de la déclaration de cette variable, on pourrait penser que le code-ci dessus n&#8217;affiche rien.<br />
 Mais c&#8217;est ici que le phénomène de closure intervient.<br />
 Quel que soit son contexte d&#8217;exécution, une fonction conserve un accès aux variables disponibles lors de sa déclaration. </div>
<div> Notre callback aura donc bien accès à la variable «str», même s&#8217;il est effectivement appelé depuis la fonction «foo», et que cette dernière n&#8217;y a pas accès. </div>
<h3>Implémentation en Objective-C</h3>
<div> Objective-C implémente ce genre de concepts, même s&#8217;il existe un certain nombre de subtilités, venant du fait qu&#8217;il s&#8217;agit d&#8217;un langage compilé dérivé du C, en restant très proche, donc très différent d&#8217;un langage de script interprété. </div>
<div> Il est à noter que le support des blocks est aussi disponible en C pur, ou en C++ (et donc en Objective-C++). </div>
<div> De même qu&#8217;une fonction standard en C, la déclaration d&#8217;un block (fonction anonyme) se doit d&#8217;être précédée par la déclaration de son prototype. </div>
<div> La syntaxe de déclaration d&#8217;un block n&#8217;est pas la plus évidente, mais on s&#8217;y fait relativement vite.<br />
 Voici par exemple le prototype d&#8217;un block: </div>
<div class="macmade-code"> <code class="macmade-source"><span class="macmade-code-predefined">NSString</span> * ( ^myBlock )( <span class="macmade-code-keyword">int</span> );</code> </div>
<div> Nous déclarons ici le prototype d&#8217;un block («^»), destiné à être nommé «myBlock», prenant un argument unique de type «int», et retournant un pointeur sur un objet de type «NSString». </div>
<div> Nous pouvons maintenant déclarer le block: </div>
<div class="macmade-code"> <code class="macmade-source">myBlock = ^( <span class="macmade-code-keyword">int</span> number )</code><br />
 <code class="macmade-source">{</code><br />
 <code class="macmade-source">    <span class="macmade-code-keyword">return</span> [ <span class="macmade-code-predefined">NSString stringWithFormat</span>: <span class="macmade-code-string">@"Passed number: %i"</span>, number ];</code><br />
 <code class="macmade-source">};</code> </div>
<div> Nous assignons donc à la variable «myBlock» le body d&#8217;une fonction, prenant comme argument «number» un entier. Cette fonction retourne un objet «NSString», dans lequel sera affiché cet entier. </div>
<div> <strong>Attention, ne pas oublier le point virgule à la fin de la déclaration du block!</strong> </div>
<div> Si cela est facultatif dans des langages de scripts, c&#8217;est absolument nécessaire pour un langage compilé comme Objective-C.<br />
 L&#8217;oublier résulterait en une erreur du compilateur, qui refuserait de générer l&#8217;exécutable final. </div>
<div> Le block peut désormais être utilisé, comme une fonction standard: </div>
<div class="macmade-code"> <code class="macmade-source">myBlock();</code> </div>
<div> Voici le code complet d&#8217;un programme Objective-C, reprenant l&#8217;exemple précédent: </div>
<div class="macmade-code"> <code class="macmade-source"><span class="macmade-code-keyword">#import</span> <span class="macmade-code-string">&lt;Cocoa/Cocoa.h&gt;</span></code></p>
<p> <code class="macmade-source"><span class="macmade-code-keyword">int</span> main( <span class="macmade-code-keyword">void</span> )</code><br />
 <code class="macmade-source">{</code><br />
 <code class="macmade-source">    <span class="macmade-code-predefined">NSAutoreleasePool</span> * pool;</code><br />
 <code class="macmade-source">    <span class="macmade-code-predefined">NSString</span>          * ( ^myBlock )( <span class="macmade-code-keyword">int</span> );</code></p>
<p> <code class="macmade-source">    pool    = [ [ <span class="macmade-code-predefined">NSAutoreleasePool</span> alloc ] init ];</code><br />
 <code class="macmade-source">    myBlock = ^( <span class="macmade-code-keyword">int</span> number )</code><br />
 <code class="macmade-source">    {</code><br />
 <code class="macmade-source">        <span class="macmade-code-keyword">return</span> [ <span class="macmade-code-predefined">NSString stringWithFormat</span>: <span class="macmade-code-string">@"Passed number: %i"</span>, number ];</code><br />
 <code class="macmade-source">    };</code></p>
<p> <code class="macmade-source">    <span class="macmade-code-predefined">NSLog</span>( <span class="macmade-code-string">@"%@"</span>, myBlock() );</code></p>
<p> <code class="macmade-source">    [ pool release ];</code></p>
<p> <code class="macmade-source">    <span class="macmade-code-keyword">return</span> EXIT_SUCCESS;</code><br />
 <code class="macmade-source">}</code> </div>
<div> Un tel programme peut être compilé à l&#8217;aide de la commande suivante (Terminal): </div>
<div class="macmade-code"> <code class="macmade-source">gcc -Wall -framework Cocoa -o test test.m</code> </div>
<div> Cela générera un fichier exécutable nommé «test», à partir du fichier source «test.m».<br />
 Pour lancer l&#8217;exécutable: </div>
<div class="macmade-code"> <code class="macmade-source">./test</code> </div>
<div> La déclaration du prototype d&#8217;un block peut être omise dans le cas où le block n&#8217;est pas assigné à une variable, comme par exemple s&#8217;il est transmis directement comme argument. </div>
<div> Par exemple: </div>
<div class="macmade-code"> <code class="macmade-source">someFunction( ^ <span class="macmade-code-predefined">NSString</span> * ( <span class="macmade-code-keyword">void</span> ) { <span class="macmade-code-keyword">return</span> <span class="macmade-code-string">@"Hello World!"</span> } );</code> </div>
<div> Il est à noter que dans un tel cas, le type de retour doit être déclaré. Ici, il s&#8217;agit d&#8217;un objet de type «NSString». </div>
<h4>Passage d&#8217;un block comme argument</h4>
<div> Un block peut bien sûr être passé comme argument d&#8217;une fonction C.<br />
 Là aussi, la syntaxe peut être quelque peu déroutante à première vue: </div>
<div class="macmade-code"> <code class="macmade-source"><span class="macmade-code-keyword">void</span> logBlock( <span class="macmade-code-predefined">NSString</span> * ( ^theBlock )( <span class="macmade-code-keyword">int</span> ) )</code><br />
 <code class="macmade-source">{</code><br />
 <code class="macmade-source">    <span class="macmade-code-predefined">NSLog</span>( <span class="macmade-code-string">@"Block returned: %@"</span>, theBlock() );</code><br />
 <code class="macmade-source">}</code> </div>
<div> Evidemment, comme Objective-C est un langage fortement typé, une fonction recevant un block en argument doit également déclarer son type de retour et le type de ses éventuels arguments. </div>
<div> Il en va de même dans le cas d&#8217;une méthode d&#8217;une classe Objective-C: </div>
<div class="macmade-code"> <code class="macmade-source">- ( <span class="macmade-code-keyword">void</span> )logBlock: ( <span class="macmade-code-predefined">NSString</span> * ( ^ )( <span class="macmade-code-keyword">int</span> ) )theBlock;</code> </div>
<h4>Closure</h4>
<div> Le phénomène de closure est également présent en Objective-C, même si son comportement est évidemment différent de celui des langages interprétés. </div>
<div> Imaginons le programme suivant: </div>
<div class="macmade-code"> <code class="macmade-source"><span class="macmade-code-keyword">#import</span> <span class="macmade-code-string">&lt;Cocoa/Cocoa.h&gt;</span></code></p>
<p> <code class="macmade-source"><span class="macmade-code-keyword">void</span> logBlock( <span class="macmade-code-keyword">int</span> ( ^theBlock )( <span class="macmade-code-keyword">void</span> ) )</code><br />
 <code class="macmade-source">{</code><br />
 <code class="macmade-source">    <span class="macmade-code-predefined">NSLog</span>( <span class="macmade-code-string">@"Closure var X: %i"</span>, theBlock() );</code><br />
 <code class="macmade-source">}</code></p>
<p> <code class="macmade-source"><span class="macmade-code-keyword">int</span> main( <span class="macmade-code-keyword">void</span> )</code><br />
 <code class="macmade-source">{</code><br />
 <code class="macmade-source">    <span class="macmade-code-predefined">NSAutoreleasePool</span> * pool;</code><br />
 <code class="macmade-source">    <span class="macmade-code-keyword">int</span> ( ^myBlock )( <span class="macmade-code-keyword">void</span> );</code><br />
 <code class="macmade-source">    <span class="macmade-code-keyword">int</span> x;</code></p>
<p> <code class="macmade-source">    pool = [ [ <span class="macmade-code-predefined">NSAutoreleasePool</span> alloc ] init ];</code><br />
 <code class="macmade-source">    x    = 27;</code></p>
<p> <code class="macmade-source">    myBlock = ^( <span class="macmade-code-keyword">void</span> )</code><br />
 <code class="macmade-source">    {</code><br />
 <code class="macmade-source">        <span class="macmade-code-keyword">return</span> x;</code><br />
 <code class="macmade-source">    };</code></p>
<p> <code class="macmade-source">    logBlock( myBlock );</code></p>
<p> <code class="macmade-source">    [ pool release ];</code></p>
<p> <code class="macmade-source">    <span class="macmade-code-keyword">return</span> EXIT_SUCCESS;</code><br />
 <code class="macmade-source">}</code> </div>
<div> La fonction «main» déclare un entier, de valeur 27, ainsi qu&#8217;un block, retournant cette même variable.<br />
 Le block set ensuite passé à la fonction «logBlock», qui affiche sa valeur de retour. </div>
<div> Même dans le contexte d&#8217;exécution de la fonction «logBlock», le block déclaré dans la fonction «main» continue d&#8217;avoir accès à l&#8217;entier «x», et peut donc sans aucun problème en retourner la valeur. </div>
<div> Il est à noter que les blocks ont également accès aux variable globales, même statiques si elles sont disponibles dans le contexte du block. </div>
<div> Il existe ici une première subtilité. En effet, les variables disponibles depuis un block par le phénomène de closure sont de type «const». Autrement dit, elle ne peuvent être modifiées depuis l&#8217;intérieur du block. </div>
<div> Par exemple, imaginons que notre block incrémente la valeur de x avant de la retourner: </div>
<div class="macmade-code"> <code class="macmade-source">myBlock = ^( <span class="macmade-code-keyword">void</span> )</code><br />
 <code class="macmade-source">{</code><br />
 <code class="macmade-source">    x++</code></p>
<p> <code class="macmade-source">    <span class="macmade-code-keyword">return</span> x;</code><br />
 <code class="macmade-source">};</code> </div>
<div> Le compilateur refusera ici de compiler le programme, puisque la variable «x» n&#8217;est disponible qu&#8217;en lecture pour notre block. </div>
<div> Pour qu&#8217;une variable puisse être modifiée depuis un block, il faut la déclarer avec le mot clé «__block».<br />
 Ainsi, le codé précédent est valide si l&#8217;on déclare la variable x ainsi: </div>
<div class="macmade-code"> <code class="macmade-source"><span class="macmade-code-keyword">__block</span> <span class="macmade-code-keyword">int</span> x;</code> </div>
<h4>Gestion de la mémoire</h4>
<div> Au niveau C, un block est une structure, pouvant être copiée et détruite.<br />
 Deux fonctions C sont à disposition pour cet usage: «Block_copy()» et «Block_destroy».<br />
 En Objective-C, un block peut également recevoir les messages «retain», «release» et «copie», comme un objet. </div>
<div> C&#8217;est aspect peut-être extrêmement important dans le cas où un block doit être conservé pour une utilisation ultérieure (et par exemple stocké dans une propriété d&#8217;une classe). </div>
<div> Ne pas copier le block, ou ne pas effectuer de «retain» dans un tel cas peut en effet induire une erreur de segmentation. </div>
<h4>Exemple d&#8217;utilisation</h4>
<div> Les blocks peuvent être utilisés dans de très nombreux contextes, afin de simplifier le code, et réduire le nombre de fonctions déclarées. </div>
<div> Voici un exemple simple illustrant l&#8217;utilisation des blocks. </div>
<div> Nous allons ajouter à la classe «NSArray» une méthode statique permettant de générer un tableau en filtrant les éléments d&#8217;un autre tableau, à l&#8217;aide d&#8217;un callback. </div>
<div> Pour les amateurs de PHP, il s&#8217;agit ici d&#8217;un équivalent de la fonction «array_filter()». </div>
<div> Nous allons commencer par la déclaration d&#8217;une catégorie de la classe «NSArray». Pour rappel, une catégorie permet l&#8217;ajout de méthodes dans une classe existante, évitant ainsi le besoin de créer une sous-classe.</div>
<div class="macmade-code"> <code class="macmade-source"><span class="macmade-code-keyword">@interface</span> <span class="macmade-code-predefined">NSArray</span>( BlockExample )</code></p>
<p> <code class="macmade-source">+ ( <span class="macmade-code-predefined">NSArray</span> * )arrayByFilteringArray: ( <span class="macmade-code-predefined">NSArray</span> * )source withCallback: ( <span class="macmade-code-keyword">BOOL</span> ( ^ )( <span class="macmade-code-keyword">id</span> ) )callback;</code></p>
<p> <code class="macmade-source"><span class="macmade-code-keyword">@end</span></code> </div>
<div> Nous déclarons ici une méthode retournant un objet de type «NSArray», et prenant en argument un autre objet «NSArray», ainsi qu&#8217;un callback, sous forme de block. </div>
<div> Ce callback sera exécuté pour chaque élément du tableau passé en argument. Il doit retourner une valeur booléenne, afin de savoir si l&#8217;élément courant du tableau source doit être conservé dans le tableau retourné. Le block prend donc comme unique argument l&#8217;objet courant du tableau source. </div>
<div> Voyons maintenant l&#8217;implémentation de cette méthode: </div>
<div class="macmade-code"> <code class="macmade-source"><span class="macmade-code-keyword">@implementation</span> <span class="macmade-code-predefined">NSArray</span>( BlockExample )</code></p>
<p> <code class="macmade-source">+ ( <span class="macmade-code-predefined">NSArray</span> * )arrayByFilteringArray: ( <span class="macmade-code-predefined">NSArray</span> * )source withCallback: ( <span class="macmade-code-keyword">BOOL</span> ( ^ )( <span class="macmade-code-keyword">id</span> ) )callback</code><br />
 <code class="macmade-source">{</code><br />
 <code class="macmade-source">    <span class="macmade-code-predefined">NSMutableArray</span> * result;</code><br />
 <code class="macmade-source">    <span class="macmade-code-keyword">id</span>               element;</code></p>
<p> <code class="macmade-source">    result = [ <span class="macmade-code-predefined">NSMutableArray</span> <span class="macmade-code-predefined">arrayWithCapacity</span>: [ source count ] ];</code></p>
<p> <code class="macmade-source">    <span class="macmade-code-keyword">for</span>( element <span class="macmade-code-keyword">in</span> source ) {</code></p>
<p> <code class="macmade-source">        <span class="macmade-code-keyword">if</span>( callback( element ) == <span class="macmade-code-keyword">YES</span> ) {</code></p>
<p> <code class="macmade-source">            [ result <span class="macmade-code-predefined">addObject</span>: element ];</code><br />
 <code class="macmade-source">        }</code><br />
 <code class="macmade-source">    }</code></p>
<p> <code class="macmade-source">    <span class="macmade-code-keyword">return</span> result;</code><br />
 <code class="macmade-source">}</code></p>
<p> <code class="macmade-source"><span class="macmade-code-keyword">@end</span></code> </div>
<div> Nous créons en premier lieu un tableau de taille dynamique («NSMutableArray»), en lui allouant une capacité initiale correspondant au nombre d&#8217;entrées du tableau source. </div>
<div> Ensuite, nous itérons chaque élément du tableau source, et nous ajoutons l&#8217;élément courant dans le cas où le résultat du callback retourne la valeur booléenne «YES». </div>
<div> Voici un exemple de programme utilisant une telle méthode.<br />
 Ici, nous nous servons du callback pour créer un tableau ne contenant que les objets de type «NSString» du tableau source: </div>
<div class="macmade-code"> <code class="macmade-source"><span class="macmade-code-keyword">#import</span> <span class="macmade-code-string">&lt;Cocoa/Cocoa.h&gt;</span></code></p>
<p> <code class="macmade-source"><span class="macmade-code-keyword">@interface</span> <span class="macmade-code-predefined">NSArray</span>( BlockExample )</code></p>
<p> <code class="macmade-source">+ ( <span class="macmade-code-predefined">NSArray</span> * )arrayByFilteringArray: ( <span class="macmade-code-predefined">NSArray</span> * )source withCallback: ( <span class="macmade-code-keyword">BOOL</span> ( ^ )( <span class="macmade-code-keyword">id</span> ) )callback;</code></p>
<p> <code class="macmade-source"><span class="macmade-code-keyword">@end</span></code></p>
<p> <code class="macmade-source"><span class="macmade-code-keyword">@implementation</span> <span class="macmade-code-predefined">NSArray</span>( BlockExample )</code></p>
<p> <code class="macmade-source">+ ( <span class="macmade-code-predefined">NSArray</span> * )arrayByFilteringArray: ( <span class="macmade-code-predefined">NSArray</span> * )source withCallback: ( <span class="macmade-code-keyword">BOOL</span> ( ^ )( <span class="macmade-code-keyword">id</span> ) )callback</code><br />
 <code class="macmade-source">{</code><br />
 <code class="macmade-source">    <span class="macmade-code-predefined">NSMutableArray</span> * result;</code><br />
 <code class="macmade-source">    <span class="macmade-code-keyword">id</span>               element;</code></p>
<p> <code class="macmade-source">    result = [ <span class="macmade-code-predefined">NSMutableArray</span> <span class="macmade-code-predefined">arrayWithCapacity</span>: [ source count ] ];</code></p>
<p> <code class="macmade-source">    <span class="macmade-code-keyword">for</span>( element <span class="macmade-code-keyword">in</span> source ) {</code></p>
<p> <code class="macmade-source">        <span class="macmade-code-keyword">if</span>( callback( element ) == <span class="macmade-code-keyword">YES</span> ) {</code></p>
<p> <code class="macmade-source">            [ result <span class="macmade-code-predefined">addObject</span>: element ];</code><br />
 <code class="macmade-source">        }</code><br />
 <code class="macmade-source">    }</code></p>
<p> <code class="macmade-source">    <span class="macmade-code-keyword">return</span> result;</code><br />
 <code class="macmade-source">}</code></p>
<p> <code class="macmade-source"><span class="macmade-code-keyword">@end</span></code></p>
<p> <code class="macmade-source"><span class="macmade-code-keyword">int</span> main( <span class="macmade-code-keyword">void</span> )</code><br />
 <code class="macmade-source">{</code><br />
 <code class="macmade-source">    <span class="macmade-code-predefined">NSAutoreleasePool</span> * pool;</code><br />
 <code class="macmade-source">    <span class="macmade-code-predefined">NSArray</span>           * array1;</code><br />
 <code class="macmade-source">    <span class="macmade-code-predefined">NSArray</span>           * array2;</code></p>
<p> <code class="macmade-source">    pool   = [ [ <span class="macmade-code-predefined">NSAutoreleasePool</span> alloc ] init ];</code><br />
 <code class="macmade-source">    array1 = [ <span class="macmade-code-predefined">NSArray</span> <span class="macmade-code-predefined">arrayWithObjects</span>: <span class="macmade-code-string">@"Hello World!"</span>, [ <span class="macmade-code-predefined">NSDate</span> date ], <span class="macmade-code-string">@"Hello Universe!"</span>, <span class="macmade-code-keyword">nil</span> ];</code><br />
 <code class="macmade-source">    array2 = [ <span class="macmade-code-predefined">NSArray</span></code><br />
 <code class="macmade-source">                    <span class="macmade-code-ctag">arrayByFilteringArray</span>: array1</code><br />
 <code class="macmade-source">                    <span class="macmade-code-ctag">withCallback</span>: ^ <span class="macmade-code-keyword">BOOL</span> ( <span class="macmade-code-keyword">id</span> element )</code><br />
 <code class="macmade-source">                    {</code><br />
 <code class="macmade-source">                        <span class="macmade-code-keyword">return</span> [ element <span class="macmade-code-predefined">isKindOfClass</span>: [ <span class="macmade-code-predefined">NSString</span> class ] ];</code><br />
 <code class="macmade-source">                    }</code><br />
 <code class="macmade-source">             ];</code></p>
<p> <code class="macmade-source">    <span class="macmade-code-predefined">NSLog</span>( <span class="macmade-code-string">@"%@"</span>, array2 );</code></p>
<p> <code class="macmade-source">    [ pool release ];</code></p>
<p> <code class="macmade-source">    <span class="macmade-code-keyword">return</span> EXIT_SUCCESS;</code><br />
 <code class="macmade-source">}</code></div>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://blog.netinfluence.com/2009/11/12/closure-et-fonctions-lambda-en-objective-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>HOWTO : Get the best out of twitter</title>
		<link>http://blog.netinfluence.com/2009/06/15/howto-get-the-best-out-of-twitter/</link>
		<comments>http://blog.netinfluence.com/2009/06/15/howto-get-the-best-out-of-twitter/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 21:01:00 +0000</pubDate>
		<dc:creator>sandrine</dc:creator>
				<category><![CDATA[reach]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[socialmedia]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://blog.netinfluence.ch/?p=11</guid>
		<description><![CDATA[You will find lots of best practices and advices to handle your twitter account. I think the basis you should set up, to start with is the following. Encourage your visitors to publicize your content ShareThis is a small plugin to add to your site / blog allowing each of your visitors, who liked the post, to publish it in their favorite social network; like facebook profile, twitter, delicious&#8230; The easier you make it for people to share what they like, the more they tend to do this little additional click to share. I use this one but their are other tools including, for those using wordpres, native plugins. I found this one smart and easy to configure. Since we [...]]]></description>
			<content:encoded><![CDATA[<p>You will find lots of best practices and advices to handle your twitter account. I think the basis you should set up, to start with is the following.</p>
<p><strong>Encourage your visitors to publicize your content</strong><br />
<a href="http://sharethis.com/">ShareThis</a> is a small plugin to add to your site / blog allowing each of your visitors, who liked the post, to publish it in their favorite social network; like facebook profile, twitter, delicious&#8230;  <img style="float: right; margin: 0 0 1em 1em;" title="sharethis-detail.png, juin 2009" src="/public/Sandrine/sharethis-detail.png" alt="sharethis-detail.png" />The easier you make it for people to share what they like, the more they tend to do this little additional click to share.  I use this one but their are other tools including, for those using wordpres, native plugins.  I found this one smart and easy to configure.  Since we added this plugin (last week), we already got 10 sharings. <strong><em>Make life simpler is key for your visitors comfort.</em></strong></p>
<p><strong>Automatically publish a link to your content</strong><br />
<a href="https://twitterfeed.com/"><img style="float: left; margin: 0 1em 1em 0;" title="twitterfeed.png, juin 2009" src="/public/Sandrine/twitterfeed.png" alt="twitterfeed.png" /></a><a href="https://twitterfeed.com/">Twitterfeed</a> is a smart tool which crawls your blog feed for updates and publishes it according to your preferences on your twitter account.  Easy to use, it allows you to automatically get visibility for your content on twitter, without having to think about it.  You can also add a prefix to your post so that people see it comes from your blog : see following screenshot.<br />
<strong><em>You create content, sharing it is important for your followers.</em></strong></p>
<p><img title="twitterfeed-detail.png, juin 2009" src="/public/Sandrine/.twitterfeed-detail_m.jpg" alt="twitterfeed-detail.png" /></p>
<p><strong>Show you have a twitter account they can follow</strong><br />
<a href="http://twitter.com"><img style="float: left; margin: 0 1em 1em 0;" title="followus.png, juin 2009" src="/public/Sandrine/followus.png" alt="followus.png" /></a>By adding a logo mentioning your twitter account, you will encourage those using it and liking it to add themselves to your followers.  You will gain instant followers just by mentioning your presence.  Opt in based, twitter allows people to follow those they like.  But many of us have various accounts and read many sources and don&#8217;t necessary have time to spend to look for if the blog / company has a twitter.<br />
Of course, if you have a linkedin, facebook profile or a corporate website, make a mention of this twitter address too! <strong><em>Make it fast for people to see you have a twitter account.</em></strong></p>
<p><strong>Follow your conversations, replies, comments</strong><br />
Twitter main interest is to generate conversations.  Tracking it is therefore essential. For this item you can use several solutions :</p>
<ol>
<li>Configure the notifications within <a href="http://twitter.com/">your twitter account</a> &gt; This solution can become quite invasive.  If you receive many followers and replies, it can happen you end up spammed by notifications, and miss out replies.  RT (retweets) are not sent via this notification system which may also make you forget some important RT for you.</li>
<li><a href="http://www.twhirl.org"><img style="float: right; margin: 0 0 1em 1em;" title="twhirl.png, juin 2009" src="/public/Sandrine/twhirl.png" alt="twhirl.png" /></a>Use an air application for your desktop tp follow your conversations.  My preference goes out to <a href="http://www.twhirl.org/">twhirl</a>. This application handles multi account (you can open as many windows as you get accounts, alerts you for replies (sound) and gives you a quick access to RT, DM, @s (retweets, direct messages and replies).</li>
<li><a href="https://cotweet.com/"><img style="float: right; margin: 0 0 1em 1em;" title="cotweet.png, juin 2009" src="/public/Sandrine/cotweet.png" alt="cotweet.png" /></a>Last but not least, you may use <a href="https://cotweet.com/">Co-tweet</a>.  This applications, still under beta, allows you to track all this conversation flow for you not to forget any.  I am just testing it and haven&#8217;t found yet how to configure alerts and set a local app, but it is nice and easy to use.</li>
</ol>
<p><strong>&#8220;Make sure you do not miss replies, messages or retweets to take advantage of conversations.&#8221;</strong></p>
<p><strong>Add / find interesting people to follow</strong> <a href="http://wefollow.com"><img style="float: left; margin: 0 1em 1em 0;" title="wefollow.png, juin 2009" src="/public/Sandrine/wefollow.png" alt="wefollow.png" /></a>You will find many directories which will be more or less useful and more or less usable.  My favourite is the <a href="http://wefollow.com/">wefollow</a> directory.  Simple, based on tages it allows you to add your twitter according to your 3 best tags and also find similar twitter.  Very useful for instance to follow tweets in the tech, social media or in regions. The other one I tries is <a href="http://www.twellow.com/">twellow</a>.  More or less working like the yellow pages it allows you to find twitter accounts you might be interested in.</p>
<p><strong>Analyze / follow your stats</strong> Here I found two useful tools :</p>
<ol>
<li><a href="http://twitterholic.com/">Twitterholic</a> crawls your site to provide you with daily / main changes stats and follow your progression trend.  It provides with an historic view of how / when your account gained followers and friends.</li>
<li><a href="http://tweeteffect.com/">Tweeteffect</a> is probably the best tip of this list.  It will not draw any attention nor publicize anything for you, but will help you understanding how your tweets work.  By indicating the number of followers you gained after each of your twitter posts, you will be able to understand what works and what doesn&#8217;t work for your users (what they like).. When planning a &#8220;marketing&#8221; and active content / value driven twitter account, this can be quite important and useful for you to understand what works for you.</li>
</ol>
<p>There are plenty of tools around you may add to this list but I reckon you should concentrate and take advantage of this quick and not really demanding optimization. I hope you enjoyed this post and feel free to add your favorites to this list too, and of course, <a href="http://twitter.com/netinfluence">follow us on twitter</a> <img src='http://blog.netinfluence.com/wp-content/plugins/smilies-themer/Silk/emoticon_wink.png' alt=';-)' class='wp-smiley' /> </p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://blog.netinfluence.com/2009/06/15/howto-get-the-best-out-of-twitter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

