IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Swift Discussion :

Qui peut m'aider à comprendre un article sur Swift?


Sujet :

Swift

  1. #1
    Membre actif
    Homme Profil pro
    infographiste et codeur AS3
    Inscrit en
    Avril 2015
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 56
    Localisation : Belgique

    Informations professionnelles :
    Activité : infographiste et codeur AS3
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2015
    Messages : 45
    Par défaut Qui peut m'aider à comprendre un article sur Swift?
    Bonjour à tous, quelqu'un pourrait traduire et m'expliquer d'une façon la plus simple possible ce qui est écrit ici:

    Initializers can have quite a few keywords associated with them. A designated initializer indicates that it’s one of the primary initializers for a class; any initializer within a class must ultimately call through to a designated initializer. A convenience initializer is a secondary initializer, which adds additional behavior or customization, but must eventually call through to a designated initializer. Designated and convenience initializers are indicated with the designated and convenience keywords, respectively.

    A required keyword next to an initializer indicates that every subclass of the class that has that initializer must implement its own version of the initializer (if it implements any initializer).

    Type casting is a way to check the type of an instance, and to treat that instance as if it’s a different superclass or subclass from somewhere else in its own class hierarchy.

    A constant or variable of a certain class type may actually refer to an instance of a subclass behind the scenes. Where you believe this is the case, you can try to downcast to the subclass type using a type cast operator.

    Because downcasting can fail, the type cast operator comes in two different forms. The optional form, as?, returns an optional value of the type you are trying to downcast to. The forced form, as!, attempts the downcast and force-unwraps the result as a single compound action.

    Use the optional type cast operator (as?) when you’re not sure if the downcast will succeed. This form of the operator will always return an optional value, and the value will be nil if the downcast was not possible. This lets you check for a successful downcast.

    Use the forced type cast operator (as!) only when you’re sure that the downcast will always succeed. This form of the operator will trigger a runtime error if you try to downcast to an incorrect class type.

    This example shows the use of the optional type cast operator (as?) to check whether a shape in an array of shapes is a circle or a square. You increment the count of the squares and triangles variables by one each time the corresponding shape is found, printing the values at the end.

    Voici le code associé:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
     
    class Triangle: NamedShape {
        init(sideLength: Double, name: String) {
            super.init(name: name)
            numberOfSides = 3
        }
    }
     
    let shapesArray = [Triangle(sideLength: 1.5, name: "triangle1"), Triangle(sideLength: 4.2, name: "triangle2"), Square(sideLength: 3.2, name: "square1"), Square(sideLength: 2.7, name: "square2")]
    var squares = 0
    var triangles = 0
    for shape in shapesArray {
        if let square = shape as? Square {
            squares++
        } else if let triangle = shape as? Triangle {
            triangles++
        }
    }
    print("\(squares) squares and \(triangles) triangles.")

  2. #2
    Membre actif
    Homme Profil pro
    infographiste et codeur AS3
    Inscrit en
    Avril 2015
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 56
    Localisation : Belgique

    Informations professionnelles :
    Activité : infographiste et codeur AS3
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2015
    Messages : 45
    Par défaut
    Sinon, bêtement, le casting n'est il pas un transtypage? Par exemple, en AS3, on fait :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    var myBoolean: Boolean = true; 
    var Myint: int = int (myBoolean); // ici le casting
    Trace (Myint); // 1

Discussions similaires

  1. [PDO] Qui peut m'aider sur PDO
    Par yvancoyaud dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 28/03/2012, 15h38
  2. qui peut m'aider sur Apibat Paye
    Par milousse2000 dans le forum Paie
    Réponses: 1
    Dernier message: 29/11/2011, 23h05
  3. qui peut m'aider sur cette petite et simple question
    Par sam102 dans le forum Contrat
    Réponses: 0
    Dernier message: 07/09/2011, 18h42
  4. qui peut m'aider sur un fichier bizarre
    Par etienne.bo dans le forum InterBase
    Réponses: 3
    Dernier message: 17/06/2006, 22h35
  5. hijackthis Qui peut m'aider Merci
    Par winow dans le forum Sécurité
    Réponses: 4
    Dernier message: 21/02/2006, 15h17

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo