Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    Swift Variables

    Swift Tutorials


    A variable provides us with named storage that our programs can operate. Variable names cannot contain mathematical symbols, arrows, private-use (or invalid) Unicode code points, or line- and box-drawing characters nor can they begin with a number, although numbers may be included elsewhere within the name.

    Variable Declaration in Swift

    A variable declaration tells the compiler of which type, where and how much to create the storage for the variable. Declarations of variables are made using the var keyword.

    Example:

    import Cocoa
    
    var firstvar = 68
    
    println(firstvar)

    The output of the above code snippet will be:

    Output:

    68

    Type Annotations

    Programmers can supply a type annotation when they declare a variable, to be clear about the kind of value(s) that the declared variable can store. The general form of using this variable is:

    Syntax:

    var variableName:<data type> = <optional initial value>

    Example:

    import Cocoa
    
    var firstvar = 68
    
    println (firstvar)
    
    var secVar:Float
    
    secVar = 3.14159
    
    println (secVar)
    
    println(" 1st Value \(varA) 2nd Value \(varB) ")


    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.