Skip to content

Instantly share code, notes, and snippets.

@pathawks
Last active February 19, 2016 22:09
Show Gist options
  • Select an option

  • Save pathawks/d639e13e59431c41da6d to your computer and use it in GitHub Desktop.

Select an option

Save pathawks/d639e13e59431c41da6d to your computer and use it in GitHub Desktop.
I ❤️ Swift

Introduction to Swift

About Swift Philosophy

Swift is friendly to new programmers. It is the first industrial-quality systems programming language that is as expressive and enjoyable as a scripting language.

The compiler is optimized for performance, and the language is optimized for development, without compromising on either. It’s designed to scale from “hello, world” to an entire operating system.

Swift Tour

Hello World

print("Hello, world!")

If you have written code in C or Objective-C, this syntax looks familiar to you—in Swift, this line of code is a complete program. You don’t need to import a separate library for functionality like input/output or string handling. Code written at global scope is used as the entry point for the program, so you don’t need a main() function. You also don’t need to write semicolons at the end of every statement.

Variables

var myVariable = 42
myVariable = 50
let myConstant = 42
  • Use var to declare variables and let to declare constants
let implicitInteger = 70
let implicitDouble = 70.0
let explicitDouble: Double = 70
  • Variables cannot change types once declared, but often do not need explicit type information

Loops

Enhanced For Loop in Java
final int[] individualScores  = [75, 43, 103, 87, 12];
int teamScore = 0;
for( int score : individualScores ) {
  if (score > 50) {
    teamScore += 3;
  } else {
    teamScore += 1;
  }
}
System.out.printf("%d\n",teamScore)
Enhanced For Loop in Swift
let individualScores = [75, 43, 103, 87, 12]
var teamScore = 0
for score in individualScores {
  if score > 50 {
    teamScore += 3
  } else {
    teamScore += 1
  }
}
print(teamScore)

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment