Swift (Apple language)


Swift is a multi-paradigm compiled programming language developed by Apple . It is intended for programming applications on the operating systems iOS , macOS , watchOS and tvOS .

It was presented at WWDC 2014 5 . It is designed to coexist with the Objective-C , current programming language for the operating systems of Apple and C . It is compiled with the LLVM compiler . A 500-page manual 6 was unveiled at WWDC 2014.

History

The development of Swift was launched in 2010 by Chris Lattner and takes its sources in many programming languages ​​such as Objective-C, Haskell, Ruby, Python, Caml or C #. The first public version 1.0 was released on June 2, 2014 at WWDC 2014.

The Swift Programming Language is a 500-page manual available 7 on iBookStore for describing language features.

Reception

Swift has been well received by developers accustomed to Apple technologies 8 . In particular, they found it simpler than the Objective-C, allowing them to do the same in fewer lines.

Some developers said they would develop new software entirely in Swift, for example Brent Simmons (developer behind MarsEdit and NetNewsWire) who announced that the Mac version of its Vesper software would be developed entirely With Swift. Facebook also added support for Swift to Parse 9 .

A few days after his presentation, Swift was about to enter the list of the 20 most popular programming languages ​​(TIOBE index) 9 . In July 2014, Swift goes to the 16 th place in the rankings 10 .

The book The Swift Programming Language had already been downloaded over 370,000 times the day after the Swift announcement at WWDC 2014 11 .

Apple’s responsiveness to developer feedback

Apple clearly affirms the feedback from developers to improve Swift 12 .

A criticism of developers about early versions of Swift was the lack of access modifiers (such as those that exist in Objective-C); While Apple is rarely communicating about the future features of its products, an Apple engineer answers the developers, telling them that they will be added in future updates to Swift 13 on July 21, 2014, Apple adds These in Swift.

In mid-July 2014, Apple is opening a blog dedicated to Swift  [ archive ] where Apple engineers will talk about the behind-the-scenes of Swift’s creation, as well as the latest novelties and tips about this language.

License

June 8, 2015, Apple announced that the compiler code and standard libraries will be distributed under a license open source 14 .

Apple says that the community contributions will be accepted and encouraged 15 .

Since 3 December 2015, Swift is now available under the Apache License 2.0 16 . Open code includes the compiler, standard language libraries, and a package manager 17 .

Language Syntax

Variables and constants

Variables and constants are used to store information. Swift supports the Int, Double, Float, Char, String and Bool types.

Variables

Swift inferences the type of variable, ie it is not necessary to specify the type of a variable when it is created. If necessary, it can be specified explicitly.

Var integer = 70
var doubleImplicite = 70.0
var floatExplicite : Float = 70 // The type of the variable

Constants

Constants must be used for any value that will remain unchanged during the program.

Let pi = 3.14159
let twoPi = 2 * pi

Optionals

The optionals are a special type of variable to use when a variable is likely to have no value.

Var companionName : String ? // To create an optional variable, let the variable type be followed by a question mark
var ageAnimalDeCompagnie : Int ? = 4 // It is quite possible to assign a value to an optional as soon as it is created

Here we give our variables an optional type because in our example it is possible that a person does not have a pet and therefore there is no value for these variables.

To use the value of an optional variable, check whether the variable contains a value or not; If it has a value, we must unwrap it (or unpack it) by following it with an exclamation point.

if nomAnimalDeCompagnie ! = nil { // If the variable has a value
 print ( nomAnimalDeCompagnie !) // We unwrap the optional and displays it in the console
} else { // If no value has been attributed to the Variable
 print ( "User did not specify pet" )
}

Names of variables

It is possible to name a variable with just about any unicode character, for example:

var fr é quentationTourEiffelEn2011 = 7100000
let π = 3.14159
let 你好 = "你好世界"
let سعيد "= فرح"
let 😢 = "Sad"

The names of variables, constants, and other Swift elements do not have spaces.

Conditions

There are several types of conditions in Swift, including if , switch and if let .

Condition if

If age > = 18 {
 print ( "You are of age" )
} else {
 print ( "You are a minor" )
}

It is also possible to use else if :

if age > = 18 {
 print ( "You are an adult" )
} else if age > = 11 {
 print ( "You're a teenager" )
} else {
 print ( "You are a child" )
}

Condition switch

Switch nombreDeC o t e s {
case 3 :
 print ( "Triangle" )
box 4 :
 print ( "Quadrangle" )
case 5 :
 print ( "Pentagon" )
box 6 :
 print ( "Hexagon" )
box 7 :
 print ( "Heptagone " )
Case 8 :
 print ( " Octogone " )
case 9 :
 print ( "enneagon" )
box 10 :
 print ( "Decagon" )
box 11 :
 print ( "hendecagon" )
box 12 :
 print ( "dodecagon" )
default :
 print ( "unavailable name for your figure" )
}Hendecagone " ) case 12 : print ( " Dodecagon " ) default : print ( " Name not available for your figure " ) }Hendecagone " ) case 12 : print ( " Dodecagon " ) default : print ( " Name not available for your figure " ) }

In Swift, the switch conditions must be exhaustive, ie it is necessary to list all the values ​​that the variable can take, which makes the code safer because it requires to manage all the possibilities And thus to prevent unexpected results from creating errors when running the program.

It is also possible to test several results at a time:

Switch letter {
case "a" , "e" , "i" , "o" , "u" , "y" :
 print ( "Vowel" )
box "b" , "c" , "d" , "f" , "g" , "h" , "j" , "k" , "l" , "m" , "n" , "p" , "q", "R" , "s" , "t" , "v" , "w" , "x" , "z" :
 print ( "consonant" )
default :
 print ( "Your character is not listed" )
}) Default : print ( "Your character is not listed" ) }) Default : print ( "Your character is not listed" ) }

Condition if let

The if condition is a type of Swift-specific condition to simplify the unwrapping of the optionalals; By means of this condition, if an optional has a value, this value is automatically assigned to a constant which will then be accessible between the curly braces of the if condition.

Var nomPossibleCompanyEntity : String ? = "Capie"
If let computername = companionName companion { // create the constant from the value of optional
 // The companionCustomerName constant is only accessible between the curly braces of the if let
 print condition ( companionName ) // no need to unwrap the companion Optional
} else {
 print ( "No name specified for pet" )
}

Loops

There are several loop types in Swift, including while , repeat while and for in . Like the conditions, loops do not require parentheses if the condition and opening brace are on the same line. The for loops present from Swift 1.0 disappear with Swift 3.0 18 .

Classes

A class could be compared to a special type of variable, allowing to group different properties and functions to process different data.

Here is an example of class creation in Swift, it stores basic information about an animal (name and number of paws) and can return a string describing this animal (with the simpleDescription function):

Class Animal {
 // Example of properties
 var tabs : Int
 var name : String
 // Initialization function
 init ( stringName : String , numberofPatte : Int ) {
 name = patchName
 = tabs = number of Patches
 }
 // Example of
 func method simpleDescription () -> String {
 return "This animal is called \ ( name ) and it has \ ( paws ) paws."
 }
}

The variables / constants of a class are called properties, the functions are called methods. All non-optional properties must have a value at initialization.

Notes and references

  1. ↑ https://swift.org/blog/swift-3-0-released/  [ archive ]
  2. ↑ https://swift.org/blog/swift-2-3/  [ archive ]
  3. a and b ( in ) ” Building assert () in Swift, Part 2: __FILE__ and __LINE__ – Swift Blog – [ archive ] , Apple Developer (accessed 24 October 2014 ) : “Swift borrows a clever feature from the D language : These identifiers expand to the location of the caller when evaluated in a default argument. “
  4. ↑ https://github.com/apple/swift/blob/master/LICENSE.txt  [ archive ]
  5. ↑ ( en ) ” Apple announces Swift, a new programming language for iOS [ archive ] .
  6. ↑ ( en ) The Swift Programming Language [ archive ] , itunes.apple.com.
  7. ↑ ( en ) ” The Swift Programming Language (Swift 3) from Apple Inc. on iBooks [ archive ] , on iBooks (accessed October 21, 2016 ) .
  8. ↑ ” WWDC 2014: the verdict (favorable) of the developers [ archive ] .
  9. a and b Swift, a common language [ archive ] .
  10. ↑ ( en ) ” TIOBE Index for July 2014 [ archive ] .
  11. ↑ ( en ) ” Introduction to Swift [ archive ] .
  12. ↑ ( en ) ” [LLVMdev] [cfe-dev] [ADVERTISEMENT] open positions in Apple’s Swift compiler team [ archive ] .
  13. ↑ ( en ) « Does Swift have access modifiers?  ”  [ Archive ] .
  14. ↑ ( en ) ” Apple open sources Swift [ archive ] , on Ars Technica ,.
  15. ↑ ( en ) ” Swift 2.0 – Swift Blog [ archive ] , on developer.apple.com .
  16. ↑ https://github.com/apple/swift/commit/39dcf401fab5eaab848a0a44fa1b2d4cb8f1d8ea#diff-61e0bdf7e1b43c5c93d9488b22e04170  [ archive ] .
  17. ↑ ” Swift Package Manager [ archive ] , on Swift.org .
  18. ↑ ( en ) Remove C-style for-loops with conditions and incrementers [ archive ] .