Swift is a new programming language for iOS, OS X, watchOS, and tvOS apps that builds on the best of C and Objective-C, without the constraints of C compatibility.

Development on Swift was begun in July 2010 by Chris Lattner, with the eventual collaboration of many other programmers at Apple. Swift took language ideas "from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list".

Swift was introduced at Apple's 2014 Worldwide Developers Conference (WWDC). It underwent an upgrade to version 1.2 during 2014 and a more major upgrade to Swift 2 at WWDC 2015. Initially a proprietary language, version 2.2 was made open source and made available under the Apache License 2.0 on December 3, 2015, for Apple's platforms and Linux. IBM announced its Swift Sandbox website, which allows developers to write Swift code in one pane and display output in another.

Swift adopts safe programming patterns and adds modern features to make programming easier, more flexible, and more fun. Swift’s clean slate, backed by the mature and much-loved Cocoa and Cocoa Touch frameworks, is an opportunity to reimagine how software development works.

Feature of Swift

  • Swift makes use of safe programming patterns.
  • Swift provides modern programming features.
  • Swift provides Objective-C like syntax.
  • Swift is a fantastic way to write iOS and OS X apps.
  • Swift provides seamless access to existing Cocoa frameworks.
  • Swift unifies the procedural and object-oriented portions of the language.
  • Swift does not need a separate library import to support functionalities like input/output or string handling.

Hello World

Tradition suggests that the first program in a new language should print the words "Hello, world!" on the screen. In Swift, this can be done in a single line:

print("Hello, world!")

More about Swift

Swift provides its own versions of all fundamental C and Objective-C types, including Int for integers, Double and Float for floating-point values, Bool for Boolean values, and String for textual data. Swift also provides powerful versions of the three primary collection types, Array, Set, and Dictionary.

In addition to familiar types, Swift introduces advanced types not found in Objective-C, such as tuples. Tuples enable you to create and pass around groupings of values. You can use a tuple to return multiple values from a function as a single compound value.

Swift also introduces optional types, which handle the absence of a value. Optionals say either “there is a value, and it equals x” or “there isn’t a value at all”. Using optionals is similar to using nil with pointers in Objective-C, but they work for any type, not just classes. Not only are optionals safer and more expressive than nil pointers in Objective-C, they are at the heart of many of Swift’s most powerful features.

Swift Playground Example Download

// this is a single line comment using two slashes.

/* this is also a comment,
   but written over multiple lines */

/* multiline comments
   /* can be nested! */
   Therefore you can block out code containing multiline
   comments
*/

// Swift variables are declared with "var"
// this is followed by a name, a type, and a value
var explicitDouble: Double = 70

// If the type is omitted, Swift will infer it from
// the variable's initial value
var implicitInteger = 70  
var implicitDouble = 70.0  
var 國 = "美國"

// Swift constants are declared with "let"
// followed by a name, a type, and a value
let numberOfBananas: Int = 10

// Like variables, if the type of a constant is omitted,
// Swift will infer it from the constant's value
let numberOfApples = 3  
let numberOfOranges = 5

// Values of variables and constants can both be
// interpolated in strings as follows
let appleSummary = "I have \(numberOfApples) apples."  
let fruitSummary = "I have \(numberOfApples + numberOfOranges) pieces of fruit."

// In "playgrounds", code can be placed in the global scope
print("Hello, world")

// This is an array variable
var fruits = ["mango", "kiwi", "avocado"]

// Example of an if statement; .isEmpty, .count
if fruits.isEmpty {  
    print("No fruits in my array.")
} else {
    print("There are \(fruits.count) items in my array")
}

// Define a dictionary with four items:
// Each item has a person's name and age
let people = ["Anna": 67, "Beto": 8, "Jack": 33, "Sam": 25]

// Now we use Swift's flexible enumerator system
// to extract both values in a single loop
for (name, age) in people {  
    print("\(name) is \(age) years old.")
}

// Functions and methods are both declared with the
// "func" syntax, and the return type is specified with ->
func sayHello(personName: String) -> String {  
    let greeting = "Hello, \(personName)!"
    return greeting
}

// prints "Hello, Dilan!"
print(sayHello("Dilan"))

// Parameter names can be made external and required
// for calling.
// The external name can be the same as the parameter
// name by prefixing with an octothorpe (#)
// - or it can be defined separately.

func sayAge(personName personName: String, personAge age: Int) -> String {  
    let result = "\(personName) is \(age) years old."
    return result
}

// We can also specify the name of the parameter

print(sayAge(personName: "Dilan", personAge: 42))