[Livre] Programmation Objective-C (Aaron Hillegass)

Afin d'alimenter la discussion sur les livres récemment ouverte dans le but de partager nos corrections de bugs ; nos solutions aux exercices et de s'entraider dans l'apprentissage j'ouvre un sujet sur le livre d'Aaron Hillegass : Programmation Objective-C (version 2013).



Il est bon de noté que l'éditeur de la version originale de cet ouvrage entretien un forum, en anglais, sur cette ouvrage ici



J'y apparait sous le nom SebRemy.



D'avance merci à  ceux qui alimenteront ce sujet

Réponses

  • iLandesiLandes Membre
    mars 2013 modifié #2
    Chapitre 10 - Struct : Ma solution


    <br />
    //<br />
    //  main.c<br />
    //  time<br />
    //<br />
    //  Created by C3PO on 15/03/13.<br />
    //  Copyright © 2013 Sebastien REMY. All rights reserved.<br />
    //<br />
    #include &lt;stdio.h&gt;<br />
    #include &lt;time.h&gt;<br />
    int main(int argc, const char * argv[])<br />
    {<br />
    	// Seconds since 1970<br />
    	long secondsSince1970 = time(NULL);<br />
    	printf(&quot;%ld seconds since 1970\n\n&quot;,secondsSince1970);<br />
      <br />
    	// Now<br />
    	struct tm now;<br />
    	localtime_r(&amp;secondsSince1970, &amp;now);<br />
    	printf(&quot;Today -&gt; %.2d-%.2d-%.4d &quot;, now.tm_mon + 1, now.tm_mday, now.tm_year + 1900);<br />
      <br />
      <br />
    	// Later<br />
    	long secondsToAdd = 4e6;<br />
    	long secondsLater = secondsSince1970 + secondsToAdd;<br />
    	struct tm later;<br />
    	localtime_r(&amp;secondsLater, &amp;later);<br />
    	printf(&quot;in %ld seconds -&gt; %.2d-%.2d-%.4d\n&quot;, secondsToAdd, later.tm_mday, later.tm_mon + 1, later.tm_year + 1900);<br />
      <br />
    	return 0;<br />
    }<br />
    




    Ce qui donne dans la console :
    <br />
    1363344785 seconds since 1970<br />
    Today -&gt; 03-15-2013 in 4000000 seconds -&gt; 30-04-2013<br />
    




    Qu'en pensez-vous ?
  • Chapitre 15 - NSArray : Ma solution
    <br />
    //<br />
    //  main.m<br />
    //  Lettres<br />
    //<br />
    //  Created by C3PO on 22/02/13.<br />
    //  Copyright (c) 2013 Sebastien REMY. All rights reserved.<br />
    //<br />
    #import &lt;Foundation/Foundation.h&gt;<br />
    int main(int argc, const char * argv[])<br />
    {<br />
        @autoreleasepool {<br />
    	   <br />
    	    // Les Prénoms avec AA<br />
    	   <br />
    	    // Lire un fichier sous forme d&#39;une longue chaà®ne de caractères<br />
    	    // Sans tenir compte des possibles erreurs<br />
    	    NSString *chaineDesPrenoms = [NSString stringWithContentsOfFile:@&quot;/usr/share/dict/propernames&quot;<br />
    														    encoding:NSUTF8StringEncoding<br />
    															   error:NULL];<br />
    	    // Décomposer la chaine en un tableau de chaines de caractères<br />
    	    NSArray *prenoms = [chaineDesPrenoms componentsSeparatedByString:@&quot;\n&quot;];<br />
    	   <br />
    	    // Parcourir le tableau une chaà®ne à  la fois<br />
    	    for (NSString *p in prenoms) {<br />
    		    // Rechercher la séquence &#39;aa&#39; sans tenir compte de la casse<br />
    		    NSRange r = [p rangeOfString:@&quot;AA&quot;<br />
    								  options:NSCaseInsensitiveSearch];<br />
    		    // La recherche a-t-elle aboutie ?<br />
    		    if (r.location &#33;= NSNotFound) {<br />
    			    NSLog(@&quot;%@&quot;,p);<br />
    		    }<br />
    	    }<br />
    	   <br />
    	 <br />
    	   <br />
    	    // Les Prénoms qui sont aussi des Mots<br />
    	    NSUInteger counter = 0;<br />
    	    NSString *chaineDesNoms = [NSString stringWithContentsOfFile:@&quot;/usr/share/dict/words&quot;<br />
    															   encoding:NSUTF8StringEncoding<br />
    																  error:NULL];<br />
    	   <br />
    	    // Décomposer la chaine en un tableau de chaines de caractères<br />
    	    NSArray *mots = [chaineDesNoms componentsSeparatedByString:@&quot;\n&quot;];<br />
    	    for (NSString *p in prenoms) {<br />
    		    for (NSString *m in mots) {<br />
    			    if ( ([p caseInsensitiveCompare:m] == NSOrderedSame)&amp;&amp;&#33;([p isEqualToString:m])){<br />
    				    // Les chaines sont égales mais pas la casse afin d&#39;éviter les doublons<br />
    				    // qui eux s&#39;écrvient pareil. Les prénoms commence par une majuscule<br />
    				    // pas les mots.<br />
    				    NSLog(@&quot;Prenom %@ = %@ (Nom commun)&quot;,p, m);<br />
    				    counter ++;<br />
    				    break;<br />
    				    }<br />
    			    }<br />
    	    }<br />
    	    NSLog(@&quot;Il y a %lu entrées en commun sur un total de %lu éléements dans prénom et %lu éléments dans mots&quot;, counter ,[prenoms count], [mots count]);<br />
        }<br />
        return 0;<br />
    }<br />
    
  • Chapitre 17 - Ma 1ère Class : Ma solution



    Action.h


    <br />
    #import &lt;Foundation/Foundation.h&gt;<br />
    @interface Action : NSObject<br />
    {<br />
        float   prixAchatAction;<br />
        float   prixActuelAction;<br />
        int	 nbActions;<br />
    }@property float prixAchatAction;<br />
    @property float prixActuelAction;<br />
    @property int   nbActions;<br />
    -(float) coutAchatEnEuros;<br />
    -(float) valeurActuelEnEuros;<br />
    @end<br />
    








    Action.m


    <br />
    #import &quot;Action.h&quot;<br />
    @implementation Action@synthesize prixAchatAction;<br />
    @synthesize prixActuelAction;<br />
    @synthesize nbActions;<br />
    -(float) coutAchatEnEuros<br />
    {<br />
        return [self prixAchatAction] * [self nbActions];<br />
    }- (float) valeurActuelEnEuros<br />
    {<br />
        return [self prixActuelAction] * [self nbActions];<br />
    }<br />
    @end<br />
    




    main.m
    <br />
    #import &lt;Foundation/Foundation.h&gt;<br />
    #import &quot;Action.h&quot;<br />
    int main(int argc, const char * argv[])<br />
    {<br />
        @autoreleasepool {<br />
    	   <br />
    	    NSMutableArray *tableauDesActions = [[NSMutableArray alloc]init];<br />
    	    Action *newAction1 = [[Action alloc] init];<br />
    	   <br />
    	    [newAction1 setPrixAchatAction:2.30];<br />
    	    [newAction1 setPrixActuelAction:4.50];<br />
    	    [newAction1 setNbActions:40];<br />
    	    [tableauDesActions addObject:newAction1];<br />
    	    Action *newAction2 = [[Action alloc] init];<br />
    	    [newAction2 setPrixAchatAction:12.19];<br />
    	    [newAction2 setPrixActuelAction:10.56];<br />
    	    [newAction2 setNbActions:90];<br />
    	    [tableauDesActions addObject:newAction2];<br />
    	   <br />
    	    Action *newAction3 = [[Action alloc] init];<br />
    	    [newAction3 setPrixAchatAction:45.10];<br />
    	    [newAction3 setPrixActuelAction:49.51];<br />
    	    [newAction3 setNbActions:210];<br />
    	    [tableauDesActions addObject:newAction3];<br />
    	   <br />
    	   // insert code here...<br />
    	   <br />
    	    for (Action *a in tableauDesActions) {<br />
    		    NSLog(@&quot;Prix d&#39;Achat %.2f, Prix Actuel %.2f, Qté, %u, Cout d&#39;Achat %.2f, Valeur actuelle %.2f&quot;,<br />
    				  [a prixAchatAction], [a prixActuelAction], [a nbActions], [a coutAchatEnEuros], [a valeurActuelEnEuros]);<br />
    	    }<br />
    	   <br />
    	   <br />
        }<br />
        return 0;<br />
    }<br />
    
  • Je m'arrête ici dans mes publications si cette section intéresse je pense qu'il faudrait ouvrir un sujet par chapitre, ou par exercice...



    Personnellement j'ai fini ce livre et j'attaque la (re)lecture de "Progrmmation Objective-C" dans la même collection.
Connectez-vous ou Inscrivez-vous pour répondre.